-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_system_test.go
113 lines (102 loc) · 3.37 KB
/
file_system_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bytes"
"fmt"
"os"
"testing"
)
type createDirHashTest struct {
dir string
ignore string
}
var testsExpectedDifferent = []createDirHashTest{
//Folder with one file (empty)
{"./test/filesystem/test_single_file", ""},
//Folder with one file (not empty)
{"./test/filesystem/test_single_file_nonempty", ""},
//Folder with multiple files
{"./test/filesystem/test_multiple_files", ""},
//Test presence of subfolder (one file)
{"./test/filesystem/test_subfolder", ""},
//Test the ignore feature (pt1)
{"./test/filesystem/test_ignored_folder", ""},
//Test the ignore feature (pt2)
{"./test/filesystem/test_ignored_folder", "ignored"},
//Test the ignore feature where ignored folder is in subfolder (pt1)
{"./test/filesystem/test_ignored_subfolder", ""},
//Test the ignore feature where ignored folder is in subfolder (pt2)
{"./test/filesystem/test_ignored_subfolder", "ignored"},
}
var testsExpectedSame = [][]createDirHashTest{
{ //Test different paths to same folders have the same hash
{"./test/filesystem/test_different_paths/a", ""},
{"./test/filesystem/test_different_paths/b", ""},
},
{ //Test changes inside ignored folder
{"./test/filesystem/test_ignored_folder_changes/a", "ignored"},
{"./test/filesystem/test_ignored_folder_changes/b", "ignored"},
},
}
func TestCreateDirHashNonExistent(t *testing.T) {
if _, err := CreateDirHash("some non existent file", ""); !os.IsNotExist(err) {
t.Errorf("CreateDirHash: Expected error on non-existent file")
}
}
func TestCreateDirHashExpectDifferent(t *testing.T) {
seen := make(map[string]string)
for _, test := range testsExpectedDifferent {
hash, err := CreateDirHash(test.dir, test.ignore)
if err != nil {
t.Errorf("CreateDirHash: %q [%q]: Error: %v", test.dir, test.ignore, err)
}
strHash := fmt.Sprintf("%x", hash)
if _, present := seen[strHash]; present {
t.Errorf("CreateDirHash: %q [%q]: Got same hash as %q (%q), expected a difference",
test.dir, test.ignore, seen[strHash], strHash)
}
seen[strHash] = test.dir
}
}
func TestCreateDirHashExpectSame(t *testing.T) {
for _, tests := range testsExpectedSame {
var expected []byte
for _, test := range tests {
hash, err := CreateDirHash(test.dir, test.ignore)
if err != nil {
t.Errorf("CreateDirHash: %q [%q]: Error: %v", test.dir, test.ignore, err)
}
if expected == nil {
expected = hash
} else if !bytes.Equal(hash, expected) {
t.Errorf("CreateDirHash: %q [%q]: Got hash %x, expected same as %x",
test.dir, test.ignore, hash, expected)
}
}
}
}
func TestCreateDirHashPermissions(t *testing.T) {
infoNoX, err := os.Stat("./test/filesystem/test-x/file1")
if err != nil {
t.Errorf("CreateDirHash: %v", err)
}
infoWithX, err := os.Stat("./test/filesystem/test+x/file1")
if err != nil {
t.Errorf("CreateDirHash: %v", err)
}
//Check for the execution bit
if infoNoX.Mode() == infoWithX.Mode() {
t.Logf("Detected that execution bits are not supported; skipping test.")
} else {
hashNoX, err := CreateDirHash("./test/filesystem/test-x", "")
if err != nil {
t.Errorf("CreateDirHash: %q: Error: %v", "test-x", err)
}
hashWithX, err := CreateDirHash("./test/filesystem/test+x", "")
if err != nil {
t.Errorf("CreateDirHash: %q: Error: %v", "test+x", err)
}
if bytes.Equal(hashNoX, hashWithX) {
t.Errorf("CreateDirHash: Expected differing hashes due to change in permissions bit")
}
}
}