-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinput_test.go
99 lines (88 loc) · 2.33 KB
/
input_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
package main
import (
"io"
"io/fs"
"reflect"
"sort"
"strings"
"testing"
"testing/fstest"
)
func Test_inputFromArgs(t *testing.T) {
xwant := [][]string{
{"xhash", "/etc/passwd"},
{"xhash", "/etc/passwd", "/etc/services"},
}
for _, want := range xwant {
var got []string
sort.Strings(want)
for input := range inputFromArgs(want) {
if input.checksums != nil {
panic(input.checksums)
}
got = append(got, input.file)
}
sort.Strings(got)
if !reflect.DeepEqual(got, want) {
t.Errorf("inputFromArgs(%q) got %v, want %v", want, got, want)
}
}
}
func Test_inputFromDir(t *testing.T) {
fsys = fstest.MapFS{
"file.go": &fstest.MapFile{},
"folder/file1.go": {},
"folder2/file2.go": {},
"folder2/file3.go": {},
"folder3/file4.go": {Mode: fs.ModeSymlink},
}
defer func() { fsys = nil }()
var want []string
for f := range fsys {
want = append(want, f)
}
sort.Strings(want)
// Test without -L option
var got []string
for input := range inputFromDir([]string{"."}, false) {
got = append(got, input.file)
}
sort.Strings(got)
if !reflect.DeepEqual(got, want[:len(want)-1]) {
t.Errorf("#1 inputFromDir() got %v; want %v", got, want)
}
// Test with -L option
got = nil
for input := range inputFromDir([]string{"."}, true) {
got = append(got, input.file)
}
sort.Strings(got)
if !reflect.DeepEqual(got, want) {
t.Errorf("#2 inputFromDir() got %v; want %v", got, want)
}
}
func Test_inputFromFile(t *testing.T) {
xwant := map[string][]string{
"/etc/passwd": {"/etc/passwd"},
"/etc/passwd\n/etc/services": {"/etc/passwd", "/etc/services"},
"/etc/passwd\n/etc/services\n": {"/etc/passwd", "/etc/services"},
"\n/etc/passwd\n/etc/services\n\n": {"/etc/passwd", "/etc/services"},
"/etc/passwd\r\n/etc/services\r\n": {"/etc/passwd", "/etc/services"},
}
for input, want := range xwant {
for _, str := range []string{input, strings.ReplaceAll(strings.ReplaceAll(input, "\r\n", "\x00"), "\n", "\x00")} {
reader := io.NopCloser(strings.NewReader(str))
var got []string
for input := range inputFromFile(reader, false) {
if input.checksums != nil {
panic(input.checksums)
}
got = append(got, input.file)
}
sort.Strings(got)
if !reflect.DeepEqual(got, want) {
t.Errorf("inputFromFile(%q) got %v; want %v", str, got, want)
}
}
}
}