-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
filesystem.go
55 lines (51 loc) · 1.55 KB
/
filesystem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package test
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// CopyDirectory copies all files in the given src directory into the given dst directory.
// Both the source and the destination directory must exist.
func CopyDirectory(src, dst string) error {
return filepath.Walk(src, func(srcPath string, fileInfo os.FileInfo, e error) error {
dstPath := strings.Replace(srcPath, src, dst, 1)
if fileInfo.IsDir() {
err := os.Mkdir(dstPath, fileInfo.Mode())
if err != nil {
return fmt.Errorf("cannot create target directory: %w", err)
}
return nil
}
sourceFile, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("cannot open source file %q: %w", srcPath, err)
}
destFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY, fileInfo.Mode())
if err != nil {
return fmt.Errorf("cannot create target file %q: %w", srcPath, err)
}
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return fmt.Errorf("cannot copy %q into %q: %w", srcPath, dstPath, err)
}
err = sourceFile.Close()
if err != nil {
return fmt.Errorf("cannot close source file %q: %w", srcPath, err)
}
err = destFile.Close()
return err
})
}
// createFile creates a file with the given filename in the given directory.
func createFile(t *testing.T, dir, filename string) {
t.Helper()
filePath := filepath.Join(dir, filename)
err := os.MkdirAll(filepath.Dir(filePath), 0o744)
assert.NoError(t, err)
err = os.WriteFile(filePath, []byte(filename+" content"), 0o500)
assert.NoError(t, err)
}