-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.lua
221 lines (176 loc) · 4.3 KB
/
test.lua
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
-- shell quoting
local function Q(s) --> quoted string
return "'" .. s:gsub("'", "'\\''") .. "'"
end
-- initialise random generator
math.randomseed(os.time())
-- generate random string 'n' bytes long
local charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
local function rand_string(n)
local res = {}
local rand = math.random
for i = 1, n do
local j = rand(1, #charset)
res[i] = charset:sub(j, j)
end
return table.concat(res)
end
-- write a string to a file
local function write_file(name, content)
local out = just(io.open(name, "w"))
local ok, err = out:write(content)
if ok then
just(out:close())
else
out:close()
os.remove(name)
error(err)
end
end
-- create a file with random content
local function create_random_file(fname)
write_file(fname, rand_string(math.random(10, 1000)))
end
-- write a line to STDOUT
local function trace(msg)
just(io.write(msg, "\n"))
end
-- find executable
local function lsch_full_pathname()
-- check if executable is present
if not os.execute("test -x ./lsch -a -f ./lsch") then
error('executable file "lsch" is not found in this directory')
end
-- get full pathname
local cmd = just(io.popen("realpath ./lsch"))
local lsch = cmd:read("a")
just(cmd:close())
return lsch:sub(1, -2) -- cut off trailing newline
end
-- make a function that compares lsch output to the given list
local function make_expect(dir)
local cmd = "cd " .. Q(dir) .. " && " .. Q(lsch) .. " -0"
return function(...)
local exp = {}
for i = 1, select("#", ...) do
exp[select(i, ...)] = true
end
pump(cmd, function(s)
if not exp[s] then
error(string.format("unexpected line: %q", s))
end
exp[s] = nil
end)
local s = next(exp)
if s then
error(string.format("missing line: %q", s))
end
end
end
-- make executor
local function make_exec(dir)
local prefix = "set -e; alias lsch=" .. Q(lsch) .. "; cd " .. Q(dir) .. "\n"
return function(cmd)
just(os.execute(prefix .. cmd))
end
end
-- invoke all tests one by one
local function all_tests(...)
lsch = lsch_full_pathname() -- global variable
local n = select("#", ...)
-- print header
if n == 0 then
error("no tests to run")
elseif n == 1 then
trace("running 1 test...")
else
trace("running " .. n .. " tests...")
end
-- run the tests
for i = 1, n do
trace("[ test " .. i .. " ]")
with_temp_dir(select(i, ...))
trace("+ passed.")
end
-- done
trace("all done.")
end
-- display test step name
local function step(name)
just(io.write(": ", name, "\n"))
end
-- test runner
local function run_tests(...)
return run(all_tests, ...)
end
-- test cases
local function test_base_ops(tmp)
create_random_file(tmp .. "/a")
create_random_file(tmp .. "/b")
create_random_file(tmp .. "/c")
local exec = make_exec(tmp)
local expect = make_expect(tmp)
step("init")
exec("lsch init")
expect("+ ./a", "+ ./b", "+ ./c")
step("reset")
exec("lsch reset")
expect()
step("change file")
write_file(tmp .. "/a", "###")
expect("* ./a")
exec("lsch reset")
step("delete file")
just(os.remove(tmp .. "/a"))
expect("- ./a")
exec("lsch reset")
step("empty file")
exec("touch a")
expect("+ ./a")
exec("lsch reset")
step("change empty file to link")
exec("rm a && ln -s b a")
expect("* ./a")
exec("lsch reset")
step("change link")
exec("rm a && ln -s c a")
expect("* ./a")
end
local function test_exotic_names(tmp)
create_random_file(tmp .. "/with space")
create_random_file(tmp .. "/with\nnewline")
create_random_file(tmp .. "/rockin'")
create_random_file(tmp .. "/rock'n'roll")
create_random_file(tmp .. "/o'ops'''")
create_random_file(tmp .. "/'done'''")
create_random_file(tmp .. "/one more\n")
local exec = make_exec(tmp)
local expect = make_expect(tmp)
step("init")
exec("lsch init")
expect("+ ./with space",
"+ ./with\nnewline",
"+ ./rockin'",
"+ ./rock'n'roll",
"+ ./o'ops'''",
"+ ./'done'''",
"+ ./one more\n")
step("reset")
exec("lsch reset")
expect()
step("change one file")
write_file(tmp .. "/with\nnewline", "###")
expect("* ./with\nnewline")
exec("lsch reset")
step("remove all")
exec("rm ./*")
expect("- ./with space",
"- ./with\nnewline",
"- ./rockin'",
"- ./rock'n'roll",
"- ./o'ops'''",
"- ./'done'''",
"- ./one more\n")
end
-- entry point
run_tests(test_base_ops, test_exotic_names)