Skip to content

Commit

Permalink
Add codes to errors
Browse files Browse the repository at this point in the history
Consider that elements of a file report are not warnings, but events,
each can be a warning or an error. An error's code starts with 0.

Fatal errors are now errors that prevent checking a file: I/O errors and
loading errors in rockspecs, but not syntax errors in checked files.

Default formatter now shows how many warnings and errors are for each file
instead of 'Failure'. Fatal errors are shown separately.

When --codes is used, error codes are prefixed with "E", unlike warnings
with "W".

TODO: move event counting and event type detection functions into core_utils.
  • Loading branch information
mpeterv committed Jul 13, 2015
1 parent 676b440 commit 0425bea
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 347 deletions.
8 changes: 4 additions & 4 deletions spec/cache_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe("cache", function()
end)

it("handles error result", function()
assert.same('return {2,4,10,"message"}', cache.serialize({error = "syntax", line = 2, column = 4, offset = 10, msg = "message"}))
assert.same('return {{"011",[3]=2,[4]=4,[24]="message"}}', cache.serialize({{code = "011", line = 2, column = 4, msg = "message"}}))
end)
end)

Expand Down Expand Up @@ -159,7 +159,7 @@ return {{"111"},{"122"}}
cache.update(tmpname,
{"foo", "bar"},
{1, 2},
{{{code="111"}}, {error = "syntax", line = 2, column = 4, offset = 10, msg = "message"}})
{{{code="111"}}, {{code = "011", line = 2, column = 4, msg = "message"}}})
end)

after_each(function()
Expand All @@ -173,7 +173,7 @@ return {{"111"},{"122"}}
it("loads cached results", function()
assert.same({
foo = {{code="111"}},
bar = {error = "syntax", line = 2, column = 4, offset = 10, msg = "message"}
bar = {{code = "011", line = 2, column = 4, msg = "message"}}
}, cache.load(tmpname, {"foo", "bar"}, {1, 2}))
end)

Expand All @@ -183,7 +183,7 @@ return {{"111"},{"122"}}

it("does not load outdated results", function()
assert.same(
{bar = {error = "syntax", line = 2, column = 4, offset = 10, msg = "message"}},
{bar = {{code = "011", line = 2, column = 4, msg = "message"}}},
cache.load(tmpname, {"foo", "bar", "baz"}, {2, 2}))
end)
end)
Expand Down
259 changes: 131 additions & 128 deletions spec/cli_spec.lua

Large diffs are not rendered by default.

62 changes: 37 additions & 25 deletions spec/format_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ end

describe("format", function()
it("returns formatted report", function()
assert.equal([[Checking stdin Failure
assert.equal([[Checking stdin 1 warning
stdin:2:7: unused global variable foo
Checking foo.lua Failure
Checking foo.lua 1 warning
foo.lua:2:7: unused global variable foo
Checking bar.lua OK
Checking baz.lua Syntax error
Checking baz.lua 1 error
baz.lua:4:3: something went wrong
Total: 2 warnings / 1 error in 4 files]], remove_color(format({
warnings = 2,
errors = 1,
fatals = 0,
{
{
code = "131",
Expand All @@ -40,29 +41,32 @@ Total: 2 warnings / 1 error in 4 files]], remove_color(format({
},
{},
{
error = "syntax",
line = 4,
column = 3,
offset = 20,
msg = "something went wrong"
{
code = "011",
line = 4,
column = 3,
msg = "something went wrong"
}
}
}, {"stdin", "foo.lua", "bar.lua", "baz.lua"}, {})))
end)

it("does not output OK messages with options.quiet >= 1", function()
assert.equal([[Checking stdin Failure
assert.equal([[Checking stdin 1 warning
stdin:2:7: unused global variable foo
Checking foo.lua Failure
Checking foo.lua 1 warning / 1 error
foo.lua:2:7: unused global variable foo
foo.lua:3:10: invalid inline option
Checking baz.lua Syntax error
Total: 2 warnings / 1 error in 4 files]], remove_color(format({
Total: 2 warnings / 1 error in 3 files, couldn't check 1 file]], remove_color(format({
warnings = 2,
errors = 1,
fatals = 1,
{
{
code = "131",
Expand All @@ -77,23 +81,29 @@ Total: 2 warnings / 1 error in 4 files]], remove_color(format({
name = "foo",
line = 2,
column = 7
},
{
code = "021",
line = 3,
column = 10
}
},
{},
{
error = "syntax"
fatal = "syntax"
}
}, {"stdin", "foo.lua", "bar.lua", "baz.lua"}, {quiet = 1})))
end)

it("does not output warnings with options.quiet >= 2", function()
assert.equal([[Checking stdin Failure
Checking foo.lua Failure
assert.equal([[Checking stdin 1 warning
Checking foo.lua 1 warning
Checking baz.lua Syntax error
Total: 2 warnings / 1 error in 4 files]], remove_color(format({
Total: 2 warnings / 0 errors in 3 files, couldn't check 1 file]], remove_color(format({
warnings = 2,
errors = 1,
errors = 0,
fatals = 1,
{
{
code = "131",
Expand All @@ -112,15 +122,16 @@ Total: 2 warnings / 1 error in 4 files]], remove_color(format({
},
{},
{
error = "syntax"
fatal = "syntax"
}
}, {"stdin", "foo.lua", "bar.lua", "baz.lua"}, {quiet = 2})))
end)

it("does not output file info with options.quiet == 3", function()
assert.equal("Total: 2 warnings / 1 error in 4 files", remove_color(format({
assert.equal("Total: 2 warnings / 0 errors in 3 files, couldn't check 1 file", remove_color(format({
warnings = 2,
errors = 1,
errors = 0,
fatals = 1,
{
{
code = "131",
Expand All @@ -139,26 +150,27 @@ Total: 2 warnings / 1 error in 4 files]], remove_color(format({
},
{},
{
error = "syntax"
fatal = "syntax"
}
}, {"stdin", "foo.lua", "bar.lua", "baz.lua"}, {quiet = 3})))
end)

it("does not color output if options.color == false", function()
assert.equal([[Checking stdin Failure
assert.equal([[Checking stdin 1 warning
stdin:2:7: unused global variable 'foo'
Checking foo.lua Failure
Checking foo.lua 1 warning
foo.lua:2:7: unused global variable 'foo'
Checking bar.lua OK
Checking baz.lua Syntax error
Total: 2 warnings / 1 error in 4 files]], format({
Total: 2 warnings / 0 errors in 3 files, couldn't check 1 file]], format({
warnings = 2,
errors = 1,
errors = 0,
fatals = 1,
{
{
code = "131",
Expand All @@ -177,7 +189,7 @@ Total: 2 warnings / 1 error in 4 files]], format({
},
{},
{
error = "syntax"
fatal = "syntax"
}
}, {"stdin", "foo.lua", "bar.lua", "baz.lua"}, {color = false}))
end)
Expand Down
99 changes: 57 additions & 42 deletions spec/luacheck_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe("luacheck", function()
it("works on empty list", function()
assert.same({
warnings = 0,
errors = 0
errors = 0,
fatals = 0
}, luacheck({}))
end)

Expand Down Expand Up @@ -76,14 +77,16 @@ describe("luacheck", function()
}
},
{
error = "syntax",
line = 1,
column = 6,
offset = 6,
msg = "expected '=' near '__future__'"
{
code = "011",
line = 1,
column = 6,
msg = "expected '=' near '__future__'"
}
},
warnings = 5,
errors = 1
errors = 1,
fatals = 0
}, luacheck({
"spec/samples/good_code.lua",
"spec/samples/bad_code.lua",
Expand Down Expand Up @@ -118,14 +121,16 @@ describe("luacheck", function()
}
},
{
error = "syntax",
line = 1,
column = 6,
offset = 6,
msg = "expected '=' near '__future__'"
{
code = "011",
line = 1,
column = 6,
msg = "expected '=' near '__future__'"
}
},
warnings = 3,
errors = 1
errors = 1,
fatals = 0
}, luacheck({
"spec/samples/good_code.lua",
"spec/samples/bad_code.lua",
Expand Down Expand Up @@ -154,14 +159,16 @@ describe("luacheck", function()
}
},
{
error = "syntax",
line = 1,
column = 6,
offset = 6,
msg = "expected '=' near '__future__'"
{
code = "011",
line = 1,
column = 6,
msg = "expected '=' near '__future__'"
}
},
warnings = 2,
errors = 1
errors = 1,
fatals = 0
}, luacheck({
"spec/samples/good_code.lua",
"spec/samples/bad_code.lua",
Expand Down Expand Up @@ -198,7 +205,8 @@ describe("check_strings", function()
it("works on empty list", function()
assert.same({
warnings = 0,
errors = 0
errors = 0,
fatals = 0
}, luacheck.check_strings({}))
end)

Expand All @@ -213,33 +221,37 @@ describe("check_strings", function()
}
},
{
error = "syntax",
line = 1,
column = 8,
offset = 8,
msg = "unexpected symbol near 'return'"
{
code = "011",
line = 1,
column = 8,
msg = "unexpected symbol near 'return'"
}
},
warnings = 1,
errors = 1
errors = 1,
fatals = 0
}, luacheck.check_strings({"return foo", "return return"}))
end)

it("uses options", function()
assert.same({
{},
{
error = "syntax",
line = 1,
column = 8,
offset = 8,
msg = "unexpected symbol near 'return'"
{
code = "011",
line = 1,
column = 8,
msg = "unexpected symbol near 'return'"
}
},
warnings = 0,
errors = 1
errors = 1,
fatals = 0
}, luacheck.check_strings({"return foo", "return return"}, {ignore = {"113"}}))
end)

it("ignores tables", function()
it("ignores tables with .fatal field", function()
assert.same({
{
{
Expand All @@ -250,11 +262,12 @@ describe("check_strings", function()
}
},
{
error = "I/O"
fatal = "I/O"
},
warnings = 1,
errors = 1
}, luacheck.check_strings({"return foo", {error = "I/O"}}))
errors = 0,
fatals = 1
}, luacheck.check_strings({"return foo", {fatal = "I/O"}}))
end)
end)

Expand All @@ -268,10 +281,10 @@ describe("get_report", function()
assert.is_table(luacheck.get_report("return foo"))
end)

it("returns nil, error on syntax error", function()
local res, err = luacheck.get_report("return return")
assert.is_nil(res)
assert.same({line = 1, column = 8, offset = 8, msg = "unexpected symbol near 'return'"}, err)
it("returns a table with single error event on syntax error", function()
local report = luacheck.get_report("return return")
assert.is_table(report)
assert.same({code = "011", line = 1, column = 8, msg = "unexpected symbol near 'return'"}, report[1])
end)
end)

Expand Down Expand Up @@ -302,7 +315,8 @@ describe("process_reports", function()
},
{},
warnings = 1,
errors = 0
errors = 0,
fatals = 0
}, luacheck.process_reports({luacheck.get_report("return foo"), luacheck.get_report("return math")}))
end)

Expand All @@ -325,7 +339,8 @@ describe("process_reports", function()
}
},
warnings = 2,
errors = 0
errors = 0,
fatals = 0
}, luacheck.process_reports({luacheck.get_report("return foo"), luacheck.get_report("return math")}, {
std = "none"
}))
Expand Down
Loading

0 comments on commit 0425bea

Please sign in to comment.