Skip to content

Commit e127ff9

Browse files
authored
Handle syntax errors more informatively (#11)
* Handle syntax errors more informatively * Also need to strip off the path to "base.lua"
1 parent 6f22286 commit e127ff9

6 files changed

Lines changed: 55 additions & 2 deletions

File tree

bin/run.moon

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,25 @@ run_tests = (slug, dir) ->
6868
data = json.decode json_output
6969

7070
if not data
71+
output = json_output
72+
73+
if output\match "^Failed to encode test results to json"
74+
-- This is a syntax error: moon can't compile it.
75+
-- Busted cannot output JSON results.
76+
-- Grab the output from vanilla busted.
77+
fh = io.popen 'busted', 'r'
78+
output = fh\read 'a'
79+
fh\close!
80+
-- trim off some non-determinant output
81+
output = output\gsub " : [%d.]+ seconds", ""
82+
output = output\gsub "(%s)/[%w./-]-/(base.lua)", "%1%2"
83+
7184
return {
7285
status: 'error',
73-
message: json_output
86+
message: output
7487
}
7588

89+
7690
if exit_status != 0 and #data.successes == 0 and #data.failures == 0 and #data.errors > 0
7791
return {
7892
status: 'error',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"tests":[],"version":2,"message":"Failed to encode test results to json: type 'function' is not supported by JSON.\n","status":"error"}
1+
{"message":"*\n0 successes / 0 failures / 1 error / 0 pending\n\nError -> base.lua @ 3\nsuite ./example_syntax_error_spec.moon\nbase.lua:81: ./example_syntax_error.moon: Failed to parse:\n [1] >> leap_year =\n","version":2,"status":"error","tests":[]}

tests/failed-to-parse/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"tests":[],"status":"error","message":"*\n0 successes / 0 failures / 1 error / 0 pending\n\nError -> base.lua @ 3\nsuite ./failed_to_parse_spec.moon\nbase.lua:81: ./failed_to_parse.moon: Failed to parse:\n [2] >> (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)\n","version":2}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
leap_year = (year) ->
2+
(year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
3+
4+
return leap_year
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
is_leap_year = require 'failed_to_parse'
2+
3+
describe 'leap', ->
4+
it 'year not divisible by 4 in common year', ->
5+
assert.is_false is_leap_year 2015
6+
7+
it 'year divisible by 2, not divisible by 4 in common year', ->
8+
assert.is_false is_leap_year 1970
9+
10+
it 'year divisible by 4, not divisible by 100 in leap year', ->
11+
assert.is_true is_leap_year 1996
12+
13+
it 'year divisible by 4 and 5 is still a leap year', ->
14+
assert.is_true is_leap_year 1960
15+
16+
it 'year divisible by 100, not divisible by 400 in common year', ->
17+
assert.is_false is_leap_year 2100
18+
19+
it 'year divisible by 100 but not by 3 is still not a leap year', ->
20+
assert.is_false is_leap_year 1900
21+
22+
it 'year divisible by 400 is leap year', ->
23+
assert.is_true is_leap_year 2000
24+
25+
it 'year divisible by 400 but not by 125 is still a leap year', ->
26+
assert.is_true is_leap_year 2400
27+
28+
it 'year divisible by 200, not divisible by 400 in common year', ->
29+
assert.is_false is_leap_year 1800

0 commit comments

Comments
 (0)