From 4fc865cac084522cd37000a5abbf95acf00f7977 Mon Sep 17 00:00:00 2001 From: Elena Vilchik Date: Tue, 26 Jun 2018 15:11:24 +0200 Subject: Not fail analysis when syntax error (#80) --- .../java/org/sonar/css/plugin/CssRuleSensor.java | 45 ++++++++++++++-------- .../org/sonar/css/plugin/CssRuleSensorTest.java | 24 ++++++++++++ .../test/resources/executables/mockSyntaxError.js | 19 +++++++++ .../test/resources/executables/mockUnknownRule.js | 19 +++++++++ 4 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 sonar-css-plugin/src/test/resources/executables/mockSyntaxError.js create mode 100644 sonar-css-plugin/src/test/resources/executables/mockUnknownRule.js (limited to 'sonar-css-plugin/src') diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java index da781aa..ce98ede 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java @@ -93,7 +93,7 @@ public class CssRuleSensor implements Sensor { try (InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)) { IssuesPerFile[] issues = new Gson().fromJson(inputStreamReader, IssuesPerFile[].class); - saveIssues(context, cssRules, issues); + saveIssues(context, issues); } } catch (IOException e) { @@ -155,7 +155,7 @@ public class CssRuleSensor implements Sensor { } } - private static void saveIssues(SensorContext context, CssRules cssRules, IssuesPerFile[] issues) { + private void saveIssues(SensorContext context, IssuesPerFile[] issues) { FileSystem fileSystem = context.fileSystem(); for (IssuesPerFile issuesPerFile : issues) { @@ -163,24 +163,35 @@ public class CssRuleSensor implements Sensor { if (inputFile != null) { for (Issue issue : issuesPerFile.warnings) { - NewIssue sonarIssue = context.newIssue(); - - NewIssueLocation location = sonarIssue.newLocation() - .on(inputFile) - .at(inputFile.selectLine(issue.line)) - .message(normalizeMessage(issue.text)); - - RuleKey ruleKey = cssRules.getActiveSonarKey(issue.rule); - if (ruleKey == null) { - throw new IllegalStateException("Unknown stylelint rule or rule not enabled " + issue.rule); - } - sonarIssue - .at(location) - .forRule(ruleKey) - .save(); + saveIssue(context, inputFile, issue); } } } } + private void saveIssue(SensorContext context, InputFile inputFile, Issue issue) { + NewIssue sonarIssue = context.newIssue(); + + RuleKey ruleKey = cssRules.getActiveSonarKey(issue.rule); + + if (ruleKey == null) { + if ("CssSyntaxError".equals(issue.rule)) { + LOG.error("Failed to parse " + inputFile.uri()); + } else { + LOG.error("Unknown stylelint rule or rule not enabled: '" + issue.rule + "'"); + } + + } else { + NewIssueLocation location = sonarIssue.newLocation() + .on(inputFile) + .at(inputFile.selectLine(issue.line)) + .message(normalizeMessage(issue.text)); + + sonarIssue + .at(location) + .forRule(ruleKey) + .save(); + } + } + } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java index 0b4acd2..dfe7ff2 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/CssRuleSensorTest.java @@ -145,6 +145,30 @@ public class CssRuleSensorTest { assertThat(logTester.logs(LoggerLevel.WARN)).contains("No rules are activated in CSS Quality Profile"); } + @Test + public void test_syntax_error() throws IOException { + SensorContextTester context = SensorContextTester.create(BASE_DIR); + context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); + DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); + TestLinterCommandProvider rulesExecution = new TestLinterCommandProvider().nodeScript("/executables/mockSyntaxError.js", inputFile.absolutePath()); + CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, rulesExecution); + sensor.execute(context); + + assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to parse " + inputFile.uri()); + } + + @Test + public void test_unknown_rule() throws IOException { + SensorContextTester context = SensorContextTester.create(BASE_DIR); + context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); + DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); + TestLinterCommandProvider rulesExecution = new TestLinterCommandProvider().nodeScript("/executables/mockUnknownRule.js", inputFile.absolutePath()); + CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, rulesExecution); + sensor.execute(context); + + assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Unknown stylelint rule or rule not enabled: 'unknown-rule-key'"); + } + private static DefaultInputFile createInputFile(SensorContextTester sensorContext, String content, String relativePath) { DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", relativePath) .setModuleBaseDir(sensorContext.fileSystem().baseDirPath()) diff --git a/sonar-css-plugin/src/test/resources/executables/mockSyntaxError.js b/sonar-css-plugin/src/test/resources/executables/mockSyntaxError.js new file mode 100644 index 0000000..cd32af2 --- /dev/null +++ b/sonar-css-plugin/src/test/resources/executables/mockSyntaxError.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node +var testFile = process.argv[2]; + +var result = [ + { + source: testFile, + + warnings: [ + { + text: "Missed semicolon (CssSyntaxError)", + line: 2, + rule: "CssSyntaxError" + } + ] + } +]; + +var json = JSON.stringify(result); +console.log(json); diff --git a/sonar-css-plugin/src/test/resources/executables/mockUnknownRule.js b/sonar-css-plugin/src/test/resources/executables/mockUnknownRule.js new file mode 100644 index 0000000..844b38b --- /dev/null +++ b/sonar-css-plugin/src/test/resources/executables/mockUnknownRule.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node +var testFile = process.argv[2]; + +var result = [ + { + source: testFile, + + warnings: [ + { + text: "some message", + line: 2, + rule: "unknown-rule-key" + } + ] + } +]; + +var json = JSON.stringify(result); +console.log(json); -- cgit v1.2.3