| 1 |
// Copyright 2023 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package blob_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"io/fs" |
| 20 |
"sort" |
| 21 |
"testing" |
| 22 |
"testing/fstest" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
"gocloud.dev/blob" |
| 26 |
"gocloud.dev/blob/memblob" |
| 27 |
) |
| 28 |
|
| 29 |
var fsFiles = []string{ |
| 30 |
"a/very/deeply/nested/sub/dir/with/a/file.txt", |
| 31 |
"baz.txt", |
| 32 |
"bazfoo.txt", |
| 33 |
"dir/foo.txt", |
| 34 |
"dir/subdir/foo.txt", |
| 35 |
"foo.txt", |
| 36 |
"foobar.txt", |
| 37 |
} |
| 38 |
|
| 39 |
func initBucket(t *testing.T, files []string) *blob.Bucket { |
| 40 |
t.Helper() |
| 41 |
|
| 42 |
ctx := context.Background() |
| 43 |
|
| 44 |
b := memblob.OpenBucket(nil) |
| 45 |
for _, f := range files { |
| 46 |
if err := b.WriteAll(ctx, f, []byte("data"), nil); err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
} |
| 50 |
return b |
| 51 |
} |
| 52 |
|
| 53 |
// TestIOFS runs the test/fstest test suite for fs.FS. |
| 54 |
func TestIOFS(t *testing.T) { |
| 55 |
tests := []struct { |
| 56 |
Description string |
| 57 |
Files []string |
| 58 |
}{ |
| 59 |
{ |
| 60 |
Description: "empty bucket", |
| 61 |
}, |
| 62 |
{ |
| 63 |
Description: "non-empty bucket", |
| 64 |
Files: fsFiles, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
for _, test := range tests { |
| 69 |
t.Run(test.Description, func(t *testing.T) { |
| 70 |
b := initBucket(t, test.Files) |
| 71 |
defer b.Close() |
| 72 |
if err := fstest.TestFS(b, test.Files...); err != nil { |
| 73 |
t.Error(err) |
| 74 |
} |
| 75 |
}) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// TestGlob does some basic verification that fs.Glob works as expected |
| 80 |
// when given a blob.Bucket. |
| 81 |
func TestGlob(t *testing.T) { |
| 82 |
b := initBucket(t, fsFiles) |
| 83 |
defer b.Close() |
| 84 |
|
| 85 |
tests := []struct { |
| 86 |
Pattern string |
| 87 |
Want []string |
| 88 |
}{ |
| 89 |
{ |
| 90 |
Pattern: "*", |
| 91 |
Want: []string{"a", "baz.txt", "bazfoo.txt", "dir", "foo.txt", "foobar.txt"}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
Pattern: "foo*", |
| 95 |
Want: []string{"foo.txt", "foobar.txt"}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
Pattern: "*foo*", |
| 99 |
Want: []string{"bazfoo.txt", "foo.txt", "foobar.txt"}, |
| 100 |
}, |
| 101 |
} |
| 102 |
for _, test := range tests { |
| 103 |
t.Run(test.Pattern, func(t *testing.T) { |
| 104 |
if got, err := fs.Glob(b, test.Pattern); err != nil { |
| 105 |
t.Fatalf("Failed to glob: %v", err) |
| 106 |
} else if diff := cmp.Diff(got, test.Want); diff != "" { |
| 107 |
t.Error(diff) |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// TestWalkDir does some basic verification that fs.WalkDir works as expected |
| 114 |
// when given a blob.Bucket. |
| 115 |
func TestWalkDir(t *testing.T) { |
| 116 |
b := initBucket(t, fsFiles) |
| 117 |
defer b.Close() |
| 118 |
|
| 119 |
var got []string |
| 120 |
fn := func(path string, _ fs.DirEntry, err error) error { |
| 121 |
if err != nil { |
| 122 |
t.Errorf("WalkFunc with path %s got error: %v", path, err) |
| 123 |
return err |
| 124 |
} |
| 125 |
got = append(got, path) |
| 126 |
return nil |
| 127 |
} |
| 128 |
if err := fs.WalkDir(b, ".", fn); err != nil { |
| 129 |
t.Fatalf("WalkDir got an unexpected error: %v", err) |
| 130 |
} |
| 131 |
// We want all of the files, plus the directories. |
| 132 |
want := append(fsFiles, |
| 133 |
".", |
| 134 |
"a", |
| 135 |
"a/very", |
| 136 |
"a/very/deeply", |
| 137 |
"a/very/deeply/nested", |
| 138 |
"a/very/deeply/nested/sub", |
| 139 |
"a/very/deeply/nested/sub/dir", |
| 140 |
"a/very/deeply/nested/sub/dir/with", |
| 141 |
"a/very/deeply/nested/sub/dir/with/a", |
| 142 |
"dir", |
| 143 |
"dir/subdir", |
| 144 |
) |
| 145 |
sort.Strings(want) |
| 146 |
if diff := cmp.Diff(got, want); diff != "" { |
| 147 |
t.Error(diff) |
| 148 |
} |
| 149 |
} |