diff options
| author | Elena Vilchik | 2019-12-27 11:49:52 +0100 |
|---|---|---|
| committer | GitHub | 2019-12-27 11:49:52 +0100 |
| commit | 0c7fadc03cca985bfc5fbae3a29f29cc71866bac (patch) | |
| tree | a68977c79532e0064f75c2657c2d3e76cbc49ccc /sonar-css-plugin/src/test | |
| parent | e65fb9a64ef2b81c3741a63d6d4a046b37659628 (diff) | |
| download | sonar-css-0c7fadc03cca985bfc5fbae3a29f29cc71866bac.tar.bz2 | |
Avoid error level logs when on project without CSS files (#231)
Diffstat (limited to 'sonar-css-plugin/src/test')
3 files changed, 60 insertions, 45 deletions
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 0b37c92..429722d 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 @@ -27,7 +27,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Collections; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -149,28 +148,13 @@ public class CssRuleSensorTest { } @Test - public void bridge_server_fail_to_start() { - CssAnalyzerBridgeServer badServer = createCssAnalyzerBridgeServer("throw.js"); - sensor = new CssRuleSensor(CHECK_FACTORY, badServer, analysisWarnings); - addInputFile("file.css"); - sensor.execute(context); - assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to start server (1s timeout)"); - - assertThat(logTester.logs(LoggerLevel.DEBUG)) - .doesNotContain("Skipping start of css-bundle server due to the failure during first analysis"); - sensor.execute(context); - assertThat(logTester.logs(LoggerLevel.DEBUG)) - .contains("Skipping start of css-bundle server due to the failure during first analysis"); - } - - @Test public void should_log_when_bridge_server_receives_invalid_response() { addInputFile("invalid-json-response.css"); - addInputFile("file.css"); sensor.execute(context); - assertThat(String.join("\n", logTester.logs(LoggerLevel.ERROR))) + assertThat(String.join("\n", logTester.logs(LoggerLevel.DEBUG))) .contains("Failed to parse response"); - assertThat(context.allIssues()).hasSize(1); + assertThat(String.join("\n", logTester.logs(LoggerLevel.ERROR))) + .contains("Failure during CSS analysis"); } @Test @@ -183,6 +167,32 @@ public class CssRuleSensorTest { assertThatThrownBy(() -> sensor.execute(context)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Analysis failed"); + assertThat(logTester.logs(LoggerLevel.ERROR)).contains("CSS rules were not executed. Failed to start server (1s timeout)"); + } + + @Test + public void should_fail_fast_when_server_fail_to_start_no_css() { + context.settings().setProperty("sonar.internal.analysis.failFast", "true"); + CssAnalyzerBridgeServer badServer = createCssAnalyzerBridgeServer("throw.js"); + sensor = new CssRuleSensor(CHECK_FACTORY, badServer, analysisWarnings); + addInputFile("file.web"); + + assertThatThrownBy(() -> sensor.execute(context)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Analysis failed"); + assertThat(logTester.logs(LoggerLevel.WARN)).contains("CSS rules were not executed. Failed to start server (1s timeout)"); + } + + @Test + public void should_not_fail_fast_when_server_fail_to_start_without_property() { + context.settings().setProperty("sonar.internal.analysis.failFast", "false"); + CssAnalyzerBridgeServer badServer = createCssAnalyzerBridgeServer("throw.js"); + sensor = new CssRuleSensor(CHECK_FACTORY, badServer, analysisWarnings); + addInputFile("file.web"); + + sensor.execute(context); + // log as Warning level is there is no CSS files + assertThat(logTester.logs(LoggerLevel.WARN)).contains("CSS rules were not executed. Failed to start server (1s timeout)"); } @Test @@ -207,13 +217,13 @@ public class CssRuleSensorTest { } @Test - public void analysis_stop_when_server_is_not_anymore_alive() throws IOException, InterruptedException { + public void analysis_stop_when_server_is_not_anymore_alive() { File configFile = new File("config.json"); DefaultInputFile inputFile = addInputFile("dir/file.css"); sensor.execute(context); cssAnalyzerBridgeServer.setPort(43); - assertThatThrownBy(() -> sensor.analyzeFiles(context, Collections.singletonList(inputFile), configFile)) + assertThatThrownBy(() -> sensor.analyzeFileWithContextCheck(inputFile, context, configFile)) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("css-bundle server is not answering"); } @@ -247,9 +257,11 @@ public class CssRuleSensorTest { @Test public void test_syntax_error() { InputFile inputFile = addInputFile("syntax-error.css"); + InputFile inputFileNotCss = addInputFile("syntax-error.web"); sensor.execute(context); assertThat(context.allIssues()).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to parse " + inputFile.uri() + ", line 2, Missed semicolon"); + assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Failed to parse " + inputFileNotCss.uri() + ", line 2, Missed semicolon"); } @Test diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java index 99b14ff..3d42261 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/server/CssAnalyzerBridgeServerTest.java @@ -28,12 +28,12 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.internal.JUnitTempFolder; import org.sonar.api.utils.log.LogTester; +import org.sonar.css.plugin.server.CssAnalyzerBridgeServer.Issue; +import org.sonar.css.plugin.server.CssAnalyzerBridgeServer.Request; import org.sonar.css.plugin.server.bundle.Bundle; -import org.sonar.css.plugin.server.AnalyzerBridgeServer.Issue; -import org.sonar.css.plugin.server.AnalyzerBridgeServer.Request; -import org.sonar.css.plugin.server.exception.ServerAlreadyFailedException; import org.sonarsource.nodejs.NodeCommand; import org.sonarsource.nodejs.NodeCommandBuilder; import org.sonarsource.nodejs.NodeCommandException; @@ -41,6 +41,7 @@ import org.sonarsource.nodejs.NodeCommandException; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.sonar.api.utils.log.LoggerLevel.DEBUG; import static org.sonar.api.utils.log.LoggerLevel.ERROR; import static org.sonar.api.utils.log.LoggerLevel.INFO; @@ -79,7 +80,7 @@ public class CssAnalyzerBridgeServerTest { @Test public void default_timeout() { - CssAnalyzerBridgeServer server = new CssAnalyzerBridgeServer(mock(Bundle.class)); + CssAnalyzerBridgeServer server = new CssAnalyzerBridgeServer(mock(Bundle.class), null); assertThat(server.timeoutSeconds).isEqualTo(60); } @@ -102,7 +103,11 @@ public class CssAnalyzerBridgeServerTest { } @Test - public void should_throw_if_failed_to_build_node_command() throws Exception { + public void should_return_false_if_failed_to_build_node_command() throws Exception { + context.fileSystem().add(new TestInputFileBuilder("moduleKey", "file.css") + .setLanguage("css") + .build()); + NodeCommandBuilder nodeCommandBuilder = mock(NodeCommandBuilder.class, invocation -> { if (NodeCommandBuilder.class.equals(invocation.getMethod().getReturnType())) { return invocation.getMock(); @@ -111,12 +116,11 @@ public class CssAnalyzerBridgeServerTest { } }); - cssAnalyzerBridgeServer = new CssAnalyzerBridgeServer(nodeCommandBuilder, TEST_TIMEOUT_SECONDS, new TestBundle(START_SERVER_SCRIPT)); - - thrown.expect(NodeCommandException.class); - thrown.expectMessage("msg"); - - cssAnalyzerBridgeServer.startServerLazily(context); + AnalysisWarnings analysisWarnings = mock(AnalysisWarnings.class); + cssAnalyzerBridgeServer = new CssAnalyzerBridgeServer(nodeCommandBuilder, TEST_TIMEOUT_SECONDS, new TestBundle(START_SERVER_SCRIPT), analysisWarnings); + assertThat(cssAnalyzerBridgeServer.startServerLazily(context)).isFalse(); + assertThat(logTester.logs(ERROR)).contains("CSS rules were not executed. msg"); + verify(analysisWarnings).addUnique("CSS rules were not executed. msg"); } @Test @@ -150,11 +154,8 @@ public class CssAnalyzerBridgeServerTest { @Test public void should_throw_if_failed_to_start() throws Exception { cssAnalyzerBridgeServer = createCssAnalyzerBridgeServer("throw.js"); - - thrown.expect(NodeCommandException.class); - thrown.expectMessage("Failed to start server (" + TEST_TIMEOUT_SECONDS + "s timeout)"); - - cssAnalyzerBridgeServer.startServerLazily(context); + assertThat(cssAnalyzerBridgeServer.startServerLazily(context)).isFalse(); + assertThat(logTester.logs(WARN)).contains("CSS rules were not executed. Failed to start server (" + TEST_TIMEOUT_SECONDS + "s timeout)"); } @Test @@ -200,15 +201,16 @@ public class CssAnalyzerBridgeServerTest { } @Test - public void should_throw_special_exception_when_failed_already() throws Exception { + public void should_return_false_and_log_when_failed_already() throws Exception { cssAnalyzerBridgeServer = createCssAnalyzerBridgeServer("throw.js"); - String failedToStartExceptionMessage = "Failed to start server (" + TEST_TIMEOUT_SECONDS + "s timeout)"; - assertThatThrownBy(() -> cssAnalyzerBridgeServer.startServerLazily(context)) - .isInstanceOf(NodeCommandException.class) - .hasMessage(failedToStartExceptionMessage); + String failedToStartExceptionMessage = "CSS rules were not executed. Failed to start server (" + TEST_TIMEOUT_SECONDS + "s timeout)"; + + assertThat(cssAnalyzerBridgeServer.startServerLazily(context)).isFalse(); + assertThat(logTester.logs(WARN)).contains(failedToStartExceptionMessage); - assertThatThrownBy(() -> cssAnalyzerBridgeServer.startServerLazily(context)) - .isInstanceOf(ServerAlreadyFailedException.class); + assertThat(cssAnalyzerBridgeServer.startServerLazily(context)).isFalse(); + assertThat(logTester.logs(DEBUG)).contains("Skipping start of css-bundle server due to the failure during first analysis", + "Skipping execution of CSS rules due to the problems with css-bundle server"); } @Test @@ -226,7 +228,7 @@ public class CssAnalyzerBridgeServerTest { public static CssAnalyzerBridgeServer createCssAnalyzerBridgeServer(String startServerScript) { - CssAnalyzerBridgeServer server = new CssAnalyzerBridgeServer(NodeCommand.builder(), TEST_TIMEOUT_SECONDS, new TestBundle(startServerScript)); + CssAnalyzerBridgeServer server = new CssAnalyzerBridgeServer(NodeCommand.builder(), TEST_TIMEOUT_SECONDS, new TestBundle(startServerScript), null); server.start(); return server; } diff --git a/sonar-css-plugin/src/test/resources/mock-start-server/startServer.js b/sonar-css-plugin/src/test/resources/mock-start-server/startServer.js index f7f40c5..87298a9 100644 --- a/sonar-css-plugin/src/test/resources/mock-start-server/startServer.js +++ b/sonar-css-plugin/src/test/resources/mock-start-server/startServer.js @@ -38,6 +38,7 @@ const requestHandler = (request, response) => { response.end(JSON.stringify([])); break; case "syntax-error.css": + case "syntax-error.web": response.end(JSON.stringify([ {line: 2, rule: "CssSyntaxError", text: "Missed semicolon (CssSyntaxError)"} ])); |
