Skip to content

Commit adf24f7

Browse files
committed
Add coverage using luacov with pause/resume patch
1 parent d6ad8cf commit adf24f7

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ docsrc/_build
44
docsrc/_static
55
docsrc/_templates
66
.luacheckcache
7+
luacov.stats.out
8+
luacov.report.out

.luacov

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require "spec.helper".luacov_config("")

spec/cli_spec.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
local utils = require "luacheck.utils"
22
local multithreading = require "luacheck.multithreading"
3+
local helper = require "spec.helper"
4+
local luacheck_cmd = helper.luacheck_command()
5+
6+
setup(helper.before_command)
7+
teardown(helper.after_command)
38

49
local function get_output(command, color, config)
5-
local handler = io.popen("luacheck "..(config or "--no-config").." "..command.." 2>&1")
10+
local handler = io.popen(luacheck_cmd.." "..(config or "--no-config").." "..command.." 2>&1")
611
local output = handler:read("*a"):gsub("\27.-\109", color and "#" or "")
712
handler:close()
813
return output
914
end
1015

1116
local function get_exitcode(command)
12-
local code51, _, code52 = os.execute("luacheck --no-config "..command.." > /dev/null 2>&1")
17+
local code51, _, code52 = os.execute(luacheck_cmd.." --no-config "..command.." > /dev/null 2>&1")
1318
return _VERSION:find "5.1" and code51/256 or code52
1419
end
1520

spec/config_spec.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
local function get_output(command, wd)
2-
command = ("luacheck %s 2>&1"):format(command)
1+
local helper = require "spec.helper"
2+
local luacheck_cmd = helper.luacheck_command()
3+
4+
setup(helper.before_command)
5+
teardown(helper.after_command)
36

4-
if wd then
5-
command = ("cd %s && %s"):format(wd, command)
6-
end
7+
local function get_output(command, wd)
8+
command = ("%s %s 2>&1"):format(helper.luacheck_command(wd), command)
79

810
local handler = io.popen(command)
911
local output = handler:read("*a"):gsub("\27.-\109", "")
@@ -12,7 +14,7 @@ local function get_output(command, wd)
1214
end
1315

1416
local function get_exitcode(command)
15-
local code51, _, code52 = os.execute("luacheck "..command.." > /dev/null 2>&1")
17+
local code51, _, code52 = os.execute(luacheck_cmd.." "..command.." > /dev/null 2>&1")
1618
return _VERSION:find "5.1" and code51/256 or code52
1719
end
1820

@@ -30,7 +32,7 @@ Checking nested/nested/abc.lua Failure
3032
nested/nested/abc.lua:1:13: accessing undefined variable c
3133
3234
Total: 3 warnings / 0 errors in 2 files
33-
]], get_output("nested", "spec/configs/project"))
35+
]], get_output("nested", "spec/configs/project/"))
3436
end)
3537

3638
it("does not use .luacheckrc in current directory with --no-config", function()
@@ -47,7 +49,7 @@ Checking nested/nested/abc.lua Failure
4749
nested/nested/abc.lua:1:13: accessing undefined variable c
4850
4951
Total: 5 warnings / 0 errors in 2 files
50-
]], get_output("nested --no-config", "spec/configs/project"))
52+
]], get_output("nested --no-config", "spec/configs/project/"))
5153
end)
5254

5355
it("uses .luacheckrc in upper directory", function()
@@ -62,7 +64,7 @@ Checking nested/abc.lua Failure
6264
nested/abc.lua:1:13: accessing undefined variable c
6365
6466
Total: 3 warnings / 0 errors in 2 files
65-
]], get_output("ab.lua nested", "spec/configs/project/nested"))
67+
]], get_output("ab.lua nested", "spec/configs/project/nested/"))
6668
end)
6769

6870
it("uses config provided with --config=path", function()

spec/helper.lua

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
local helper = {}
2+
3+
local dir_sep = package.config:sub(1, 1)
4+
5+
-- Return path to root directory when run from `path`.
6+
local function antipath(path)
7+
local _, level = path:gsub(dir_sep, "")
8+
return (".."..dir_sep):rep(level)
9+
end
10+
11+
-- Try to load a lua module from `file_path` when run from `loc_path`.
12+
local function try_load(loc_path, file_path)
13+
local real_path = antipath(loc_path)..file_path
14+
local fd = io.open(real_path, "rb")
15+
16+
if not fd then
17+
return
18+
end
19+
20+
local src = fd:read("*a")
21+
fd:close()
22+
local func, err = (loadstring or load)(src, "@"..file_path) -- luacheck: compat
23+
return err or func
24+
end
25+
26+
-- Return a package searcher which, when run from `loc_path`,
27+
-- loads lua libraries from `lib_path`.
28+
function helper.prefix_searcher(loc_path, lib_path)
29+
return function(package_name)
30+
local package_path = lib_path..package_name:gsub("%.", dir_sep)
31+
return try_load(loc_path, package_path..".lua") or
32+
try_load(loc_path, package_path..dir_sep.."init.lua"), package_name
33+
end
34+
end
35+
36+
function helper.luacov_config(prefix)
37+
return {
38+
statsfile = prefix.."luacov.stats.out",
39+
reportfile = prefix.."luacov.report.out",
40+
deletestats = false,
41+
runreport = false,
42+
include = {"luacheck/.+$"},
43+
exclude = {}
44+
}
45+
end
46+
47+
local luacov = package.loaded.luacov or package.loaded["luacov.runner"]
48+
49+
-- Returns command that runs `luacheck` executable from `loc_path`.
50+
function helper.luacheck_command(loc_path)
51+
loc_path = loc_path or "."
52+
local prefix = antipath(loc_path)
53+
local cmd = ("cd %s && lua"):format(loc_path)
54+
55+
-- Extend package.path to allow loading this helper.
56+
cmd = cmd..(" -e 'package.path=[[%s?.lua;]]..package.path'"):format(prefix)
57+
-- Add searcher for luacheck modules.
58+
cmd = cmd..(" -e 'table.insert(package.loaders or package.searchers,1,"..
59+
"require[[spec.helper]].prefix_searcher([[%s]],[[./src%s]]))'"):format(loc_path, dir_sep)
60+
61+
if luacov then
62+
-- Launch luacov.
63+
cmd = cmd..(" -e 'require[[luacov.runner]](require[[spec.helper]].luacov_config([[%s]]))'"):format(prefix)
64+
end
65+
66+
return ("%s %sbin%sluacheck.lua"):format(cmd, prefix, dir_sep)
67+
end
68+
69+
function helper.before_command()
70+
if luacov then
71+
luacov.pause()
72+
end
73+
end
74+
75+
function helper.after_command()
76+
if luacov then
77+
luacov.resume()
78+
end
79+
end
80+
81+
return helper

0 commit comments

Comments
 (0)