-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: testing/fstest: add MapFS.CopyFrom(fs.FS) #69595
Comments
Related Issues and Documentation (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
What is an example of a test that would want to use that method? |
I actually think that mapfs := fstest.MapFS{}
mapfs.CopyFrom(os.DirFS("path/to/dir"))
// manipulate mapfs without need for err != nil
os.CopyFS("path/to/other/dir", mapfs) Code that needs to deal with filesystems can generally benefit by consolidating all of their In the case of tests, here are some examples of code that would benefit from //go:embed *
var embedFiles embed.FS
func TestHello(t *testing.T) {
assert := assert.New(t)
bucket := memblob.OpenBucket(nil)
defer bucket.Close()
// bucket operations...
mapfs := fstest.MapFS{}
mapfs.CopyFrom(bucket)
mapfs.CopyFrom(os.DirFS("path/to/testing/tmpdir"))
mapfs.CopyFrom(embedFiles)
//// simple smoke test when filenames are non-deterministic
// MapFS
assert.Equal(3, len(mapfs))
// fs.FS - more awkward and requires a must()
assert.Equal(3, len(must(fs.Glob(bucket, "*"))))
//// deterministic filenames
// MapFS
filenames := strings.Join(slices.Sorted(maps.Keys(mapfs)), " ")
assert.Equal("a.txt b/b.txt c/c/c.json", filenames)
// fs.FS - no shorthand for this due to the subdirectories
fs.WalkDir(bucket, ".", func(path string, d fs.DirEntry, err error) error {
// ...
return nil
})
//// specific file
// MapFS
assert.NotNil(mapfs["a.txt"])
// fs.FS - not exactly elegant, panics instead of failing gracefully
must(fs.ReadFile(bucket, "a.txt"))
//// nonexistent file
// MapFS
assert.Nil(mapfs["q.txt"])
// fs.FS
_, err := fs.ReadFile(bucket, "q.txt")
assert.NotNil(err)
} |
2cents: once an FS is loaded in a MapFS, it is fairly easy to edit the MapFS. Say there was a function |
Yes, that strikes me as the correct API. FWIW, I wrote |
Proposal Details
Since it was concluded that
fs.FS
is necessarily read-only, the only alternative is for every writable filesystem to implement its own form ofos.CopyFS
. This proposal is about doing so forMapFS
.For details, see:
The text was updated successfully, but these errors were encountered: