/
cmd_test.go
61 lines (46 loc) 路 1.34 KB
/
cmd_test.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
package main
import (
"math/rand"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
var testSeededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
type testSuite struct {
suite.Suite
testDir string
rootDir string
}
func (ts *testSuite) SetupTest() {
dir, err := os.MkdirTemp(os.TempDir(), "fsdiff-test-*")
ts.Require().NoError(err)
ts.testDir = dir
ts.rootDir = filepath.Join(ts.testDir, "root")
ts.Require().NoError(os.Mkdir(ts.rootDir, 0o755))
}
func (ts *testSuite) TearDownTest() {
ts.Require().NoError(os.RemoveAll(ts.testDir))
}
func (ts *testSuite) randomStringWithCharset(length int, charset string) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[testSeededRand.Intn(len(charset))]
}
return string(b)
}
func (ts *testSuite) randomString(length int) string {
const defaultCharset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return ts.randomStringWithCharset(length, defaultCharset)
}
func (ts *testSuite) createDummyFile(path string, data []byte, mode os.FileMode) string {
ts.Require().NoError(os.MkdirAll(filepath.Join(ts.rootDir, filepath.Dir(path)), 0o755))
path = filepath.Join(ts.rootDir, path)
ts.Require().NoError(os.WriteFile(path, data, mode))
return path
}
func TestFsdiffTestSuite(t *testing.T) {
suite.Run(t, new(testSuite))
}