Skip to content

Commit 7d784df

Browse files
committed
Fix bug in multiple calls to file applier
Ensure that file appliers create the same content every time they are called. Signed-off-by: Derek McGowan <[email protected]>
1 parent 2d3749b commit 7d784df

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

fs/fstest/file.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,24 @@ func (a applyFn) Apply(root string) error {
2424
// CreateFile returns a file applier which creates a file as the
2525
// provided name with the given content and permission.
2626
func CreateFile(name string, content []byte, perm os.FileMode) Applier {
27-
return writeFileStream(name, bytes.NewReader(content), perm)
27+
f := func() io.Reader {
28+
return bytes.NewReader(content)
29+
}
30+
return writeFileStream(name, f, perm)
2831
}
2932

3033
// CreateRandomFile returns a file applier which creates a file with random
3134
// content of the given size using the given seed and permission.
3235
func CreateRandomFile(name string, seed, size int64, perm os.FileMode) Applier {
33-
return writeFileStream(name, io.LimitReader(rand.New(rand.NewSource(seed)), size), perm)
36+
f := func() io.Reader {
37+
return io.LimitReader(rand.New(rand.NewSource(seed)), size)
38+
}
39+
return writeFileStream(name, f, perm)
3440
}
3541

3642
// writeFileStream returns a file applier which creates a file as the
3743
// provided name with the given content from the provided i/o stream and permission.
38-
func writeFileStream(name string, stream io.Reader, perm os.FileMode) Applier {
44+
func writeFileStream(name string, stream func() io.Reader, perm os.FileMode) Applier {
3945
return applyFn(func(root string) (retErr error) {
4046
fullPath := filepath.Join(root, name)
4147
f, err := os.OpenFile(fullPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
@@ -48,7 +54,7 @@ func writeFileStream(name string, stream io.Reader, perm os.FileMode) Applier {
4854
retErr = err
4955
}
5056
}()
51-
_, err = io.Copy(f, stream)
57+
_, err = io.Copy(f, stream())
5258
if err != nil {
5359
return err
5460
}

0 commit comments

Comments
 (0)