-
Notifications
You must be signed in to change notification settings - Fork 8
/
testing.go
80 lines (67 loc) · 2.72 KB
/
testing.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package filesystem
import (
"fmt"
"math"
"math/rand"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// GenerateTestFileTree generates a file tree for testing purposes and returns a list of all the files and filesystem items created.
// testDir corresponds to the folder where the tree is createed
// basePath corresponds to the base path for sym links
// fileModTime, fileAccessTime are for specifying created files ch times
func GenerateTestFileTree(t *testing.T, fs FS, testDir, basePath string, withLinks bool, fileModTime time.Time, fileAccessTime time.Time) []string {
err := fs.MkDir(testDir)
require.NoError(t, err)
var sLinks []string
random := rand.New(rand.NewSource(time.Now().Unix())) //nolint:gosec //causes G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec), So disable gosec
for i := 0; i < int(math.Max(float64(1), float64(random.Intn(10)))); i++ { //nolint:gosec //causes G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec), So disable gosec
c := fmt.Sprint("test", i+1)
path := filepath.Join(testDir, c)
err := fs.MkDir(path)
require.NoError(t, err)
for j := 0; j < int(math.Max(float64(1), float64(random.Intn(10)))); j++ { //nolint:gosec //causes G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec), So disable gosec
c := fmt.Sprint("test", j+1)
path := filepath.Join(path, c)
err := fs.MkDir(path)
require.NoError(t, err)
if withLinks {
if len(sLinks) > 0 {
c1 := fmt.Sprint("link", j+1)
c2 := filepath.Join(path, c1)
err = fs.Symlink(sLinks[0], c2)
require.NoError(t, err)
if len(sLinks) > 1 {
sLinks = sLinks[1:]
} else {
sLinks = nil
}
}
}
for k := 0; k < int(math.Max(float64(1), float64(random.Intn(10)))); k++ { //nolint:gosec //causes G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec), So disable gosec
c := fmt.Sprint("test", k+1, ".txt")
finalPath := filepath.Join(path, c)
// pick a couple of files to make symlinks (1 in 10)
r := random.Intn(10) //nolint:gosec //causes G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec), So disable gosec
if r == 4 {
fPath := filepath.Join(basePath, path, c)
sLinks = append(sLinks, fPath)
}
s := fmt.Sprint("file ", i+1, j+1, k+1)
err = fs.WriteFile(finalPath, []byte(s), 0755)
require.NoError(t, err)
}
}
}
var tree []string
err = fs.ListDirTree(testDir, &tree)
require.NoError(t, err)
// unifying timestamps
for _, path := range tree {
err = fs.Chtimes(path, fileAccessTime, fileModTime)
require.NoError(t, err)
}
return tree
}