|
| 1 | +local json_ok, json = pcall(require, "dkjson") |
| 2 | +json = json_ok and json or require("json") |
| 3 | + |
| 4 | +local get_output = require("spec.helper").get_output |
| 5 | + |
| 6 | +assert:set_parameter("TableFormatLevel", 6) |
| 7 | + |
| 8 | +local function get_json_output(...) |
| 9 | + local output = get_output(...) |
| 10 | + local ok, result = pcall(json.decode, output) |
| 11 | + if ok then |
| 12 | + return result |
| 13 | + else |
| 14 | + return output |
| 15 | + end |
| 16 | +end |
| 17 | + |
| 18 | +local function build_issue(path, line, col, code, severity, issue_type, message, secondaryLocations) |
| 19 | + return { |
| 20 | + effortMinutes = 2, |
| 21 | + engineId = "luacheck", |
| 22 | + primaryLocation = { |
| 23 | + message = message, |
| 24 | + filePath = path, |
| 25 | + textRange = { |
| 26 | + startLine = tonumber(line), |
| 27 | + startColumn = col and (tonumber(col) - 1), |
| 28 | + } |
| 29 | + }, |
| 30 | + ruleId = code, |
| 31 | + secondaryLocations = secondaryLocations, |
| 32 | + severity = severity, |
| 33 | + type = issue_type, |
| 34 | + } |
| 35 | +end |
| 36 | + |
| 37 | +-- luacheck: max line length 180 |
| 38 | +describe("Sonar formatter", function() |
| 39 | + it("renders empty array when there is no issues", function() |
| 40 | + -- don't use get_json_output to ensure that there is array in json |
| 41 | + assert.equal('{"issues":[]}\n', get_output "spec/samples/good_code.lua --std=lua52 --formatter sonar --no-config") |
| 42 | + end) |
| 43 | + |
| 44 | + it("renders issues", function() |
| 45 | + local expected = { |
| 46 | + build_issue("spec/samples/bad_code.lua", 3, 16, "211", "MAJOR", "CODE_SMELL", "unused function 'helper'"), |
| 47 | + build_issue("spec/samples/bad_code.lua", 3, 23, "212", "MAJOR", "CODE_SMELL", "unused variable length argument"), |
| 48 | + build_issue("spec/samples/bad_code.lua", 7, 10, "111", "MAJOR", "CODE_SMELL", "setting non-standard global variable 'embrace'"), |
| 49 | + build_issue("spec/samples/bad_code.lua", 8, 10, "412", "MAJOR", "CODE_SMELL", "variable 'opt' was previously defined as an argument on line 7", {{ |
| 50 | + filePath = "spec/samples/bad_code.lua", |
| 51 | + message = "opt", |
| 52 | + textRange = { |
| 53 | + startLine = 7, |
| 54 | + startColumn = 17, |
| 55 | + endColumn = 19, |
| 56 | + }, |
| 57 | + }}), |
| 58 | + build_issue("spec/samples/bad_code.lua", 9, 11, "113", "MAJOR", "CODE_SMELL", "accessing undefined variable 'hepler'"), |
| 59 | + build_issue("spec/samples/python_code.lua", 1, 6, "011", "BLOCKER", "BUG", "expected '=' near '__future__'"), |
| 60 | + } |
| 61 | + local output = get_json_output "spec/samples/good_code.lua spec/samples/bad_code.lua spec/samples/python_code.lua --std=lua52 --formatter sonar --no-config" |
| 62 | + for i, val in ipairs(output.issues) do |
| 63 | + assert.same(expected[i], val) |
| 64 | + end |
| 65 | + end) |
| 66 | + |
| 67 | + it("renders fatal errors", function() |
| 68 | + assert.same({issues = { |
| 69 | + build_issue("spec/samples/404.lua", 1, nil, "FATAL", "BLOCKER", "BUG", "I/O error") |
| 70 | + }}, get_json_output "spec/samples/404.lua --formatter sonar --no-config") |
| 71 | + end) |
| 72 | +end) |
0 commit comments