/
test_luacheck.lua
109 lines (97 loc) · 2.97 KB
/
test_luacheck.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
local test = require "tests.lib"
local luacheck = require "luacheck"
local lousy = { util = require("lousy.util") }
local T = {}
function T.test_luacheck ()
local lua_dirs = {"lib", "config", "tests", "build-utils"}
local exclude_files = {
"lib/markdown.lua",
}
local options = {
std = "luajit",
}
local shared_globals = {
"luakit",
"soup",
"msg",
"ipc_channel",
"string.wlen",
"regex",
"utf8",
}
local ui_globals = {
"sqlite3",
"lfs",
"xdg",
"timer",
"download",
"stylesheet",
"unique",
"widget",
"uris",
"require_web_module",
"os",
}
local wm_globals = {
"extension",
"dom_document",
"dom_element",
"page",
}
local file_options = {
["config/rc.lua"] = {
ignore = { "211" } -- 211: Unused variable
},
["lib/adblock.lua"] = {
ignore = { "542" }, -- 542: Empty if branch
},
["tests/run_test.lua"] = {
ignore = { "311/.*_prx" }, -- 311: Value assigned to variable is unused
},
}
wm_globals = lousy.util.table.join(shared_globals, wm_globals)
ui_globals = lousy.util.table.join(shared_globals, ui_globals)
local file_list = test.find_files(lua_dirs, "%.lua$", exclude_files)
local warnings, errors, fatals = 0, 0, 0
local issues = {}
for _, file in ipairs(file_list) do
-- Build options table for file
local opts = lousy.util.table.clone(options)
if string.match(file, ".*_wm%.lua$") then
opts.globals = wm_globals
else
opts.globals = ui_globals
end
for pattern, subopts in pairs(file_options) do
if string.match(file, pattern) then
for k, v in pairs(subopts) do
opts[k] = v
end
end
end
-- Check file, collate any warning messages
local report = luacheck.check_files({file}, opts)
local file_report = report[1]
for _, issue in ipairs(file_report) do
local src = ("%s:%d: (%d:%d):"):format(file, issue.line, issue.column, issue.end_column)
local msg = luacheck.get_message(issue)
issues[#issues+1] = { src = src, msg = msg }
end
warnings = warnings + report.warnings
errors = errors + report.errors
fatals = fatals + report.fatals
end
if warnings + errors + fatals > 0 then
local align = 0
for _, issue in ipairs(issues) do
align = math.max(align, issue.src:len())
end
local output = {}
for _, issue in ipairs(issues) do
output[#output + 1] = string.format(" %-" .. tostring(align+10) .. "s %s", issue.src, issue.msg)
end
error("Luacheck messages:\n" .. table.concat(output, "\n"))
end
end
return T
-- vim: et:sw=4:ts=8:sts=4:tw=80