aboutsummaryrefslogtreecommitdiff
path: root/spec/util/quick.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util/quick.lua')
-rw-r--r--spec/util/quick.lua45
1 files changed, 33 insertions, 12 deletions
diff --git a/spec/util/quick.lua b/spec/util/quick.lua
index 1fcf2b36..d7df7bb1 100644
--- a/spec/util/quick.lua
+++ b/spec/util/quick.lua
@@ -46,6 +46,7 @@ local function parse(filename)
46 local tests = {} 46 local tests = {}
47 47
48 local cur_line = 0 48 local cur_line = 0
49 local cur_suite = ""
49 local cur_test 50 local cur_test
50 local cur_op 51 local cur_op
51 local cur_block 52 local cur_block
@@ -54,7 +55,7 @@ local function parse(filename)
54 55
55 local function start_test(arg) 56 local function start_test(arg)
56 cur_test = { 57 cur_test = {
57 name = arg, 58 name = cur_suite .. arg,
58 ops = {}, 59 ops = {},
59 } 60 }
60 cur_op = nil 61 cur_op = nil
@@ -137,6 +138,8 @@ local function parse(filename)
137 local cmd, arg = parse_cmd(line) 138 local cmd, arg = parse_cmd(line)
138 if cmd == "TEST" then 139 if cmd == "TEST" then
139 start_test(arg) 140 start_test(arg)
141 elseif cmd == "SUITE" then
142 cur_suite = arg .. ": "
140 elseif cmd then 143 elseif cmd then
141 fail("expected TEST, got " .. cmd) 144 fail("expected TEST, got " .. cmd)
142 elseif is_blank(line) then 145 elseif is_blank(line) then
@@ -175,12 +178,14 @@ local function parse(filename)
175 cur_op = { 178 cur_op = {
176 op = "EXISTS", 179 op = "EXISTS",
177 file = dir.normalize(arg), 180 file = dir.normalize(arg),
181 line = cur_line,
178 } 182 }
179 table.insert(cur_test.ops, cur_op) 183 table.insert(cur_test.ops, cur_op)
180 elseif cmd == "NOT_EXISTS" then 184 elseif cmd == "NOT_EXISTS" then
181 cur_op = { 185 cur_op = {
182 op = "NOT_EXISTS", 186 op = "NOT_EXISTS",
183 file = dir.normalize(arg), 187 file = dir.normalize(arg),
188 line = cur_line,
184 } 189 }
185 table.insert(cur_test.ops, cur_op) 190 table.insert(cur_test.ops, cur_op)
186 elseif cmd == "MKDIR" then 191 elseif cmd == "MKDIR" then
@@ -271,6 +276,12 @@ function quick.compile(filename, env)
271 table.insert(code, table.concat({...})) 276 table.insert(code, table.concat({...}))
272 end 277 end
273 278
279 write(([=[ ]=]))
280 write(([=[ -- **************************************** ]=]))
281 write(([=[ -- %s ]=]):format(t.name))
282 write(([=[ -- **************************************** ]=]))
283 write(([=[ ]=]))
284
274 write([=[ local test_env = require("spec.util.test_env") ]=]) 285 write([=[ local test_env = require("spec.util.test_env") ]=])
275 write([=[ local lfs = require("lfs") ]=]) 286 write([=[ local lfs = require("lfs") ]=])
276 write([=[ local fs = require("lfs") ]=]) 287 write([=[ local fs = require("lfs") ]=])
@@ -278,6 +289,18 @@ function quick.compile(filename, env)
278 write([=[ local luarocks_cmd = test_env.execute_helper(test_env.Q(test_env.testing_paths.lua) .. " " .. test_env.testing_paths.src_dir .. "/bin/luarocks", false, test_env.env_variables):sub(1, -5) ]=]) 289 write([=[ local luarocks_cmd = test_env.execute_helper(test_env.Q(test_env.testing_paths.lua) .. " " .. test_env.testing_paths.src_dir .. "/bin/luarocks", false, test_env.env_variables):sub(1, -5) ]=])
279 write([=[ local luarocks_admin_cmd = test_env.execute_helper(test_env.Q(test_env.testing_paths.lua) .. " " .. test_env.testing_paths.src_dir .. "/bin/luarocks-admin", false, test_env.env_variables):sub(1, -5) ]=]) 290 write([=[ local luarocks_admin_cmd = test_env.execute_helper(test_env.Q(test_env.testing_paths.lua) .. " " .. test_env.testing_paths.src_dir .. "/bin/luarocks-admin", false, test_env.env_variables):sub(1, -5) ]=])
280 291
292 write([=[ local function make_dir(dirname) ]=])
293 write([=[ local bits = {} ]=])
294 write([=[ if dirname:sub(1, 1) == dir_sep then bits[1] = "" end ]=])
295 write([=[ local ok, err ]=])
296 write([=[ for p in dirname:gmatch("[^" .. dir_sep .. "]+") do ]=])
297 write([=[ table.insert(bits, p) ]=])
298 write([=[ ok, err = lfs.mkdir(table.concat(bits, dir_sep)) ]=])
299 write([=[ end ]=])
300 write([=[ local exists = (lfs.attributes(dirname) or {}).mode == "directory" ]=])
301 write([=[ return exists, (not exists) and err ]=])
302 write([=[ end ]=])
303
281 write(([=[ local function error_message(line, msg, input) ]=])) 304 write(([=[ local function error_message(line, msg, input) ]=]))
282 write(([=[ local out = {"\n\n", %q, ":", line, ": ", msg} ]=]):format(filename)) 305 write(([=[ local out = {"\n\n", %q, ":", line, ": ", msg} ]=]):format(filename))
283 write(([=[ if input then ]=])) 306 write(([=[ if input then ]=]))
@@ -302,27 +325,25 @@ function quick.compile(filename, env)
302 write([=[ return (s:gsub("%%{url%(tmpdir%)}", (tmpdir:gsub("\\", "/"))) ]=]) 325 write([=[ return (s:gsub("%%{url%(tmpdir%)}", (tmpdir:gsub("\\", "/"))) ]=])
303 write([=[ :gsub("%%{tmpdir}", (tmpdir:gsub("[\\/]", dir_sep)))) ]=]) 326 write([=[ :gsub("%%{tmpdir}", (tmpdir:gsub("[\\/]", dir_sep)))) ]=])
304 write([=[ end ]=]) 327 write([=[ end ]=])
328 write([=[ local ok, err ]=])
305 for _, op in ipairs(t.ops) do 329 for _, op in ipairs(t.ops) do
306 if op.op == "FILE" then 330 if op.op == "FILE" then
331 if op.name:match("[\\/]") then
332 write(([=[ make_dir(%q) ]=]):format(dir.dir_name(op.name)))
333 end
307 write(([=[ test_env.write_file(handle_tmpdir(%q), handle_tmpdir([=====[ ]=]):format(op.name)) 334 write(([=[ test_env.write_file(handle_tmpdir(%q), handle_tmpdir([=====[ ]=]):format(op.name))
308 for _, line in ipairs(op.data) do 335 for _, line in ipairs(op.data) do
309 write(line) 336 write(line)
310 end 337 end
311 write([=[ ]=====]), finally) ]=]) 338 write([=[ ]=====]), finally) ]=])
312 elseif op.op == "EXISTS" then 339 elseif op.op == "EXISTS" then
313 write(([=[ assert.truthy(lfs.attributes(%q)) ]=]):format(op.file)) 340 write(([=[ ok, err = lfs.attributes(%q) ]=]):format(op.file))
341 write(([=[ assert.truthy(ok, error_message(%d, "EXISTS failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.line, op.file))
314 elseif op.op == "NOT_EXISTS" then 342 elseif op.op == "NOT_EXISTS" then
315 write(([=[ assert.falsy(lfs.attributes(%q)) ]=]):format(op.file)) 343 write(([=[ assert.falsy(lfs.attributes(%q), error_message(%d, "NOT_EXISTS failed: " .. %q .. " exists" )) ]=]):format(op.file, op.line, op.file))
316 elseif op.op == "MKDIR" then 344 elseif op.op == "MKDIR" then
317 local bits = {}
318 op.file = native_slash(op.file) 345 op.file = native_slash(op.file)
319 if op.file:sub(1, 1) == dir_sep then bits[1] = "" end 346 write(([=[ ok, err = make_dir(%q) ]=]):format(op.file))
320 write([=[ local ok, err ]=])
321 for p in op.file:gmatch("[^" .. dir_sep .. "]+") do
322 table.insert(bits, p)
323 local d = table.concat(bits, dir_sep)
324 write(([=[ ok, err = lfs.mkdir(%q) ]=]):format(d, d))
325 end
326 write(([=[ assert.truthy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file)) 347 write(([=[ assert.truthy((lfs.attributes(%q) or {}).mode == "directory", error_message(%d, "MKDIR failed: " .. %q .. " - " .. (err or "") )) ]=]):format(op.file, op.line, op.file))
327 elseif op.op == "RUN" then 348 elseif op.op == "RUN" then
328 local cmd_helper = cmd_helpers[op.program] or op.program 349 local cmd_helper = cmd_helpers[op.program] or op.program
@@ -411,7 +432,7 @@ function quick.compile(filename, env)
411 write([=[ end ]=]) 432 write([=[ end ]=])
412 433
413 local program = table.concat(code, "\n") 434 local program = table.concat(code, "\n")
414 local chunk = assert(load(program, "@" .. filename .. ": test " .. tn, "t", env or _ENV)) 435 local chunk = assert(load(program, "@" .. filename .. ":[TEST " .. tn .. "]", "t", env or _ENV))
415 if env and setfenv then 436 if env and setfenv then
416 setfenv(chunk, env) 437 setfenv(chunk, env)
417 end 438 end