@@ -24,18 +24,24 @@ func (a applyFn) Apply(root string) error {
24
24
// CreateFile returns a file applier which creates a file as the
25
25
// provided name with the given content and permission.
26
26
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 )
28
31
}
29
32
30
33
// CreateRandomFile returns a file applier which creates a file with random
31
34
// content of the given size using the given seed and permission.
32
35
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 )
34
40
}
35
41
36
42
// writeFileStream returns a file applier which creates a file as the
37
43
// 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 {
39
45
return applyFn (func (root string ) (retErr error ) {
40
46
fullPath := filepath .Join (root , name )
41
47
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 {
48
54
retErr = err
49
55
}
50
56
}()
51
- _ , err = io .Copy (f , stream )
57
+ _ , err = io .Copy (f , stream () )
52
58
if err != nil {
53
59
return err
54
60
}
0 commit comments