diff options
| author | Elena Vilchik | 2018-06-25 15:20:12 +0200 | 
|---|---|---|
| committer | GitHub | 2018-06-25 15:20:12 +0200 | 
| commit | 7a31cb18a718601f31766cf8e02c6ad85f3916f0 (patch) | |
| tree | 3f8c71f48b8e23c1af06a53ab405e5bb23357900 /sonar-css-plugin/src/test | |
| parent | b45e6c9923f9ab8a27a81430dfea81d5302922d3 (diff) | |
| download | sonar-css-7a31cb18a718601f31766cf8e02c6ad85f3916f0.tar.bz2 | |
Not fail analysis if Node is not available (#73)
Diffstat (limited to 'sonar-css-plugin/src/test')
3 files changed, 71 insertions, 18 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 06295d7..030a987 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 @@ -26,6 +26,7 @@ import java.nio.charset.StandardCharsets;  import java.nio.file.Files;  import java.nio.file.Path;  import java.nio.file.Paths; +import org.junit.Before;  import org.junit.Rule;  import org.junit.Test;  import org.junit.rules.ExpectedException; @@ -38,6 +39,9 @@ import org.sonar.api.batch.sensor.SensorContext;  import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;  import org.sonar.api.batch.sensor.internal.SensorContextTester;  import org.sonar.api.batch.sensor.issue.Issue; +import org.sonar.api.config.Configuration; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel;  import org.sonar.css.plugin.bundle.BundleHandler;  import org.sonar.css.plugin.bundle.CssBundleHandler; @@ -46,13 +50,25 @@ import static org.assertj.core.api.Assertions.assertThat;  public class CssRuleSensorTest {    @Rule +  public final LogTester logTester = new LogTester(); + +  @Rule +  public TemporaryFolder tmpDir = new TemporaryFolder(); + +  @Rule    public ExpectedException thrown= ExpectedException.none();    private static CheckFactory checkFactory = new CheckFactory(new TestActiveRules("S4647")); -  private File BASE_DIR = new File("src/test/resources").getAbsoluteFile(); -  @Rule -  public TemporaryFolder tmpDir = new TemporaryFolder(); +  private static final File BASE_DIR = new File("src/test/resources").getAbsoluteFile(); + +  private SensorContextTester context = SensorContextTester.create(BASE_DIR); +  private DefaultInputFile inputFile = createInputFile(context, "some css content\n on 2 lines", "dir/file.css"); + +  @Before +  public void setUp() throws Exception { +    context.fileSystem().setWorkDir(tmpDir.getRoot().toPath()); +  }    @Test    public void test_descriptor() { @@ -65,11 +81,8 @@ public class CssRuleSensorTest {    @Test    public void test_execute() 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 = TestLinterCommandProvider.nodeScript("/executables/mockStylelint.js", inputFile.absolutePath()); -    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, rulesExecution); +    TestLinterCommandProvider commandProvider = getCommandProvider(); +    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider);      sensor.execute(context);      assertThat(context.allIssues()).hasSize(1); @@ -81,15 +94,45 @@ public class CssRuleSensorTest {    }    @Test +  public void test_invalid_node() throws IOException { +    TestLinterCommandProvider commandProvider = getCommandProvider(); +    commandProvider.nodeExecutable += " " + TestLinterCommandProvider.resourceScript("/executables/invalidNodeVersion.js"); +    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); +    sensor.execute(context); + +    assertThat(context.allIssues()).hasSize(0); +    assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to parse Node.js version, got 'Invalid version'. No CSS files will be analyzed."); +  } + +  @Test +  public void test_no_node() throws IOException { +    TestLinterCommandProvider commandProvider = getCommandProvider(); +    commandProvider.nodeExecutable = TestLinterCommandProvider.resourceScript("/executables/invalidNodeVersion.js"); +    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); +    sensor.execute(context); + +    assertThat(context.allIssues()).hasSize(0); +    assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to get Node.js version. No CSS files will be analyzed."); +  } + +  @Test +  public void test_old_node() throws IOException { +    TestLinterCommandProvider commandProvider = getCommandProvider(); +    commandProvider.nodeExecutable += " " + TestLinterCommandProvider.resourceScript("/executables/oldNodeVersion.js"); +    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider); +    sensor.execute(context); + +    assertThat(context.allIssues()).hasSize(0); +    assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Only Node.js v6 or later is supported, got 3.2.1. No CSS files will be analyzed."); +  } + +  @Test    public void test_error() throws IOException {      thrown.expect(IllegalStateException.class);      thrown.expectMessage("Failed to parse json result of external process execution"); -    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 = TestLinterCommandProvider.nodeScript("/executables/mockError.js", inputFile.absolutePath()); -    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, rulesExecution); +    TestLinterCommandProvider commandProvider = new TestLinterCommandProvider().nodeScript("/executables/mockError.js", inputFile.absolutePath()); +    CssRuleSensor sensor = new CssRuleSensor(new TestBundleHandler(), checkFactory, commandProvider);      sensor.execute(context);    } @@ -106,9 +149,13 @@ public class CssRuleSensorTest {      return inputFile;    } +  private TestLinterCommandProvider getCommandProvider() { +    return new TestLinterCommandProvider().nodeScript("/executables/mockStylelint.js", inputFile.absolutePath()); +  } +    private static class TestLinterCommandProvider implements LinterCommandProvider { -    private static String nodeExecutable = findNodeExecutable(); +    String nodeExecutable = findNodeExecutable();      private String[] elements; @@ -130,10 +177,9 @@ public class CssRuleSensorTest {        }      } -    static TestLinterCommandProvider nodeScript(String script, String args) { -      TestLinterCommandProvider testRulesExecution = new TestLinterCommandProvider(); -      testRulesExecution.elements = new String[]{ nodeExecutable, resourceScript(script), args}; -      return testRulesExecution; +    TestLinterCommandProvider nodeScript(String script, String args) { +      this.elements = new String[]{ nodeExecutable, resourceScript(script), args}; +      return this;      }      @Override @@ -145,6 +191,11 @@ public class CssRuleSensorTest {      public String configPath(File deployDestination) {        return new File(deployDestination, "testconfig.json").getAbsolutePath();      } + +    @Override +    public String nodeExecutable(Configuration configuration) { +      return nodeExecutable; +    }    }    private static class TestBundleHandler implements BundleHandler { diff --git a/sonar-css-plugin/src/test/resources/executables/invalidNodeVersion.js b/sonar-css-plugin/src/test/resources/executables/invalidNodeVersion.js new file mode 100644 index 0000000..772b9f3 --- /dev/null +++ b/sonar-css-plugin/src/test/resources/executables/invalidNodeVersion.js @@ -0,0 +1 @@ +console.log("Invalid version"); diff --git a/sonar-css-plugin/src/test/resources/executables/oldNodeVersion.js b/sonar-css-plugin/src/test/resources/executables/oldNodeVersion.js new file mode 100644 index 0000000..817b98a --- /dev/null +++ b/sonar-css-plugin/src/test/resources/executables/oldNodeVersion.js @@ -0,0 +1 @@ +console.log("3.2.1"); | 
