diff options
| author | Elena Vilchik | 2018-06-22 09:55:16 +0200 | 
|---|---|---|
| committer | GitHub | 2018-06-22 09:55:16 +0200 | 
| commit | 3922c3cd97c13f08bd2ec833e84ac167432d7de8 (patch) | |
| tree | 6727599afcb79c2c3a554471280c96a765cd3fc1 /sonar-css-plugin/src | |
| parent | bd7c66b25b4f6779e622df81908ae5c11be5798b (diff) | |
| download | sonar-css-3922c3cd97c13f08bd2ec833e84ac167432d7de8.tar.bz2 | |
Fix analysis of project with non-css files (#58)
Diffstat (limited to 'sonar-css-plugin/src')
6 files changed, 46 insertions, 12 deletions
| 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 eda2b5d..0601839 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 @@ -20,6 +20,7 @@  package org.sonar.css.plugin;  import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException;  import java.io.File;  import java.io.IOException;  import java.io.InputStreamReader; @@ -63,10 +64,9 @@ public class CssRuleSensor implements Sensor {      File deployDestination = context.fileSystem().workDir();      bundleHandler.deployBundle(deployDestination); -    File projectBaseDir = context.fileSystem().baseDir(); - -    String[] commandParts = linterCommandProvider.commandParts(deployDestination, projectBaseDir); +    String[] commandParts = linterCommandProvider.commandParts(deployDestination, context);      ProcessBuilder processBuilder = new ProcessBuilder(commandParts); +    String command = String.join(" ", commandParts);      try {        createConfig(deployDestination); @@ -78,8 +78,10 @@ public class CssRuleSensor implements Sensor {        }      } catch (IOException e) { -      String command = String.join(" ", commandParts); -      throw new IllegalStateException(String.format("Failed to run external process '%s'. Re-run analysis with debug option for more information.", command), e); +      throw new IllegalStateException(String.format("Failed to run external process '%s'", command), e); + +    } catch (JsonSyntaxException e) { +      throw new IllegalStateException(String.format("Failed to parse json result of external process execution '%s'. To diagnose, try to run it manually.", command), e);      }    } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/LinterCommandProvider.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/LinterCommandProvider.java index 142762c..3ca334a 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/LinterCommandProvider.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/LinterCommandProvider.java @@ -20,10 +20,11 @@  package org.sonar.css.plugin;  import java.io.File; +import org.sonar.api.batch.sensor.SensorContext;  public interface LinterCommandProvider { -  String[] commandParts(File deployDestination, File projectBaseDir); +  String[] commandParts(File deployDestination, SensorContext context);    String configPath(File deployDestination);  } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/StylelintCommandProvider.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/StylelintCommandProvider.java index fe05ad6..d97293d 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/StylelintCommandProvider.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/StylelintCommandProvider.java @@ -20,7 +20,9 @@  package org.sonar.css.plugin;  import java.io.File; +import java.nio.file.Paths;  import org.sonar.api.batch.ScannerSide; +import org.sonar.api.batch.sensor.SensorContext;  @ScannerSide  public class StylelintCommandProvider implements LinterCommandProvider { @@ -28,11 +30,17 @@ public class StylelintCommandProvider implements LinterCommandProvider {    private static final String CONFIG_PATH = "css-bundle/stylelintconfig.json";    @Override -  public String[] commandParts(File deployDestination, File projectBaseDir) { +  public String[] commandParts(File deployDestination, SensorContext context) { +    String projectBaseDir = context.fileSystem().baseDir().getAbsolutePath(); +    String[] suffixes = context.config().getStringArray(CssPlugin.FILE_SUFFIXES_KEY); +    String filesGlob = "**" + File.separator + "*{" + String.join(",", suffixes) + "}"; +    String filesToAnalyze = Paths.get(projectBaseDir, "TOREPLACE").toString(); +    filesToAnalyze = filesToAnalyze.replace("TOREPLACE", filesGlob); +      return new String[]{        "node",        new File(deployDestination, "css-bundle/node_modules/stylelint/bin/stylelint").getAbsolutePath(), -      projectBaseDir.getAbsolutePath(), +      filesToAnalyze,        "--config", new File(deployDestination, CONFIG_PATH).getAbsolutePath(),        "-f", "json"      }; 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 d176688..f86e24f 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 @@ -28,11 +28,13 @@ import java.nio.file.Path;  import java.nio.file.Paths;  import org.junit.Rule;  import org.junit.Test; +import org.junit.rules.ExpectedException;  import org.junit.rules.TemporaryFolder;  import org.sonar.api.batch.fs.InputFile.Type;  import org.sonar.api.batch.fs.internal.DefaultInputFile;  import org.sonar.api.batch.fs.internal.TestInputFileBuilder;  import org.sonar.api.batch.rule.CheckFactory; +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.css.plugin.bundle.BundleHandler; @@ -42,6 +44,9 @@ import static org.assertj.core.api.Assertions.assertThat;  public class CssRuleSensorTest { +  @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(); @@ -73,6 +78,19 @@ public class CssRuleSensorTest {      assertThat(Files.readAllLines(configPath)).containsOnly("{\"rules\":{\"color-no-invalid-hex\":true}}");    } +  @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); +    sensor.execute(context); +  } +    private static DefaultInputFile createInputFile(SensorContextTester sensorContext, String content, String relativePath) {      DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", relativePath)        .setModuleBaseDir(sensorContext.fileSystem().baseDirPath()) @@ -117,7 +135,7 @@ public class CssRuleSensorTest {      }      @Override -    public String[] commandParts(File deployDestination, File projectBaseDir) { +    public String[] commandParts(File deployDestination, SensorContext context) {        return elements;      } diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintCommandProviderTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintCommandProviderTest.java index d201258..7244cce 100644 --- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintCommandProviderTest.java +++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/StylelintCommandProviderTest.java @@ -21,6 +21,7 @@ package org.sonar.css.plugin;  import java.io.File;  import org.junit.Test; +import org.sonar.api.batch.sensor.internal.SensorContextTester;  import static org.assertj.core.api.Assertions.assertThat; @@ -30,11 +31,13 @@ public class StylelintCommandProviderTest {    public void test() throws Exception {      StylelintCommandProvider stylelintCommandProvider = new StylelintCommandProvider();      File deployDestination = new File("deploy_destination"); -    File baseDir = new File("base_dir"); -    assertThat(stylelintCommandProvider.commandParts(deployDestination, baseDir)).containsExactly( +    File baseDir = new File("src/test/resources").getAbsoluteFile(); +    SensorContextTester context = SensorContextTester.create(baseDir); +    context.settings().setProperty(CssPlugin.FILE_SUFFIXES_KEY, ".foo,.bar"); +    assertThat(stylelintCommandProvider.commandParts(deployDestination, context)).containsExactly(        "node",        new File(deployDestination, "css-bundle/node_modules/stylelint/bin/stylelint").getAbsolutePath(), -      baseDir.getAbsolutePath(), +      baseDir.getAbsolutePath() + File.separator + "**" + File.separator + "*{.foo,.bar}",        "--config",        new File(deployDestination, "css-bundle/stylelintconfig.json").getAbsolutePath(),        "-f", diff --git a/sonar-css-plugin/src/test/resources/executables/mockError.js b/sonar-css-plugin/src/test/resources/executables/mockError.js new file mode 100644 index 0000000..23849a4 --- /dev/null +++ b/sonar-css-plugin/src/test/resources/executables/mockError.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log("Incorrect json might appear if exception thrown during analysis") | 
