forked from rakutentech/parfind
/
parfind_test.go
149 lines (133 loc) · 3.53 KB
/
parfind_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
/*
parfind
=======
A parallel, simplified version of find(1).
See README.md for usage and licensing information.
*/
import (
"bytes"
"os"
"regexp"
"strconv"
"testing"
)
const (
DefVersion = false
DefWorkers = DEFAULT_WORKERS
DefRoot = "."
DefPrint0 = false
NonExistingDirectory = "./non_existing_directory"
TestingDirectory = "./test_dir"
TestingSubDirectory = TestingDirectory + "/subdir"
TestingSubSubDirectory = TestingSubDirectory + "/subdir"
TestingFile = TestingDirectory + "/test_file.txt"
TestingFileUnicode = TestingDirectory + "/変.txt"
TestingFileQuoting = TestingDirectory + "/\r\n\"'`.txt"
TestingFileSymlink = TestingDirectory + "/symlink"
TestingFileSubDir = TestingSubDirectory + "/test_file.txt"
TestingFileSubSubDir = TestingSubSubDirectory + "/test_file.txt"
)
var TestDirs = []string{
TestingDirectory,
TestingSubDirectory,
TestingSubSubDirectory,
}
var TestFiles = []string{
TestingFile,
TestingFileUnicode,
TestingFileQuoting,
TestingFileSubDir,
TestingFileSubSubDir,
}
var TestParams = []struct {
version bool
workers int
root string
print0 bool
}{
{DefVersion, DefWorkers, TestingDirectory, false},
{DefVersion, DefWorkers, TestingDirectory, true},
{DefVersion, -1, TestingDirectory, false},
{DefVersion, -1, TestingDirectory, true},
{DefVersion, MAX_WORKERS + 1, TestingDirectory, false},
{DefVersion, MAX_WORKERS + 1, TestingDirectory, true},
{DefVersion, DefWorkers, NonExistingDirectory, false},
{DefVersion, DefWorkers, NonExistingDirectory, true},
}
var (
FmtDefault = regexp.MustCompile("[dflspCDu] [-0-9]+ [0-9]+ (\"[\x20-\x7E]+\")\r?\n")
FmtPrint0 = regexp.MustCompile("[dflspCDu]\x00[-0-9]+\x00[0-9]+\x00([^\x00]+)\x00")
RequiredResults = 0
)
func createTestingDirectory() {
RequiredResults = 0
removeTestingDirectory()
for _, d := range TestDirs {
if err := os.Mkdir(d, 0600); err == nil {
RequiredResults++
}
}
for _, f := range TestFiles {
if _, err := os.Create(f); err == nil {
RequiredResults++
}
}
if err := os.Symlink(TestingFile, TestingFileSymlink); err == nil {
RequiredResults++
}
}
func removeTestingDirectory() {
os.RemoveAll(TestingDirectory)
os.RemoveAll(TestingDirectory)
os.RemoveAll(TestingDirectory)
}
func runParfind(version bool, workers int, root string, print0 bool, t *testing.T) (sout string, serr string) {
out := new(bytes.Buffer)
err := new(bytes.Buffer)
defer func() {
recover()
if out.Len() > 0 {
t.Logf("STDOUT\n%s\n", out)
}
if err.Len() > 0 {
t.Logf("STDERR\n%s\n", err)
}
sout = out.String()
serr = err.String()
}()
parfind(version, workers, root, print0, out, err)
return
}
func TestVersion(t *testing.T) {
out, err := runParfind(true, DefWorkers, DefRoot, DefPrint0, t)
if out != VERSION+"\n" || err != "" {
t.Fail()
}
}
func TestParfind(t *testing.T) {
createTestingDirectory()
for _, params := range TestParams {
out, err := runParfind(params.version, params.workers, params.root, params.print0, t)
if params.root == TestingDirectory && err != "" {
t.Fatalf("Unexpected output on stderr")
}
f := FmtDefault
if params.print0 {
f = FmtPrint0
}
res := f.FindAllStringSubmatch(out, -1)
if params.root == TestingDirectory && len(res) != RequiredResults {
t.Fatalf("Expected %d results, got %d", RequiredResults, len(res))
}
for _, v := range res {
f := v[1]
if params.print0 == false {
f, _ = strconv.Unquote(f)
}
if _, e := os.Stat(f); e != nil {
t.Errorf("Can't stat result %s", f)
}
}
}
}