| 1 |
package blob_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"gocloud.dev/blob" |
| 11 |
"gocloud.dev/blob/fileblob" |
| 12 |
) |
| 13 |
|
| 14 |
func TestPrefixedBucket(t *testing.T) { |
| 15 |
dir := t.TempDir() |
| 16 |
|
| 17 |
bucket, err := fileblob.OpenBucket(dir, nil) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
|
| 22 |
const contents = "contents" |
| 23 |
ctx := context.Background() |
| 24 |
if err := bucket.WriteAll(ctx, "foo/bar/baz.txt", []byte(contents), nil); err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
|
| 28 |
wrapped := blob.PrefixedBucket(bucket, "foo/bar/") |
| 29 |
defer wrapped.Close() |
| 30 |
|
| 31 |
got, err := wrapped.ReadAll(ctx, "baz.txt") |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
if string(got) != contents { |
| 36 |
t.Errorf("got %q want %q", string(got), contents) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func TestSingleKeyBucket(t *testing.T) { |
| 41 |
dir := t.TempDir() |
| 42 |
|
| 43 |
bucket, err := fileblob.OpenBucket(dir, nil) |
| 44 |
if err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
|
| 48 |
const contents = "contents" |
| 49 |
ctx := context.Background() |
| 50 |
if err := bucket.WriteAll(ctx, "foo/bar.txt", []byte(contents), nil); err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
|
| 54 |
dirpath := filepath.ToSlash(dir) |
| 55 |
if os.PathSeparator != '/' && !strings.HasPrefix(dirpath, "/") { |
| 56 |
dirpath = "/" + dirpath |
| 57 |
} |
| 58 |
|
| 59 |
wrapped, err := blob.OpenBucket(ctx, "file://"+dirpath+"?key=foo/bar.txt") |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
defer wrapped.Close() |
| 64 |
got, err := wrapped.ReadAll(ctx, "") |
| 65 |
if err != nil { |
| 66 |
t.Fatal(err) |
| 67 |
} |
| 68 |
if string(got) != contents { |
| 69 |
t.Errorf("got %q want %q", string(got), contents) |
| 70 |
} |
| 71 |
} |