Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Commit

Permalink
Fix issue #20 - custom rules dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablissimo committed Mar 26, 2016
1 parent 3b5061b commit ff8faa0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/pablissimo/sonar/TsLintExecutor.java
@@ -1,5 +1,5 @@
package com.pablissimo.sonar;

public interface TsLintExecutor {
String execute(String pathToTsLint, String configFile, String file, Integer timeoutMs);
String execute(String pathToTsLint, String configFile, String rulesDir, String file, Integer timeoutMs);
}
8 changes: 7 additions & 1 deletion src/main/java/com/pablissimo/sonar/TsLintExecutorImpl.java
Expand Up @@ -12,12 +12,18 @@ public class TsLintExecutorImpl implements TsLintExecutor {
private StringBuilder stdOut;
private StringBuilder stdErr;

public String execute(String pathToTsLint, String configFile, String file, Integer timeoutMs) {
public String execute(String pathToTsLint, String configFile, String rulesDir, String file, Integer timeoutMs) {
LOG.info("TsLint executing for " + file);
Command command = Command.create("node");
command.addArgument(pathToTsLint);
command.addArgument("--format");
command.addArgument("json");

if (rulesDir != null && rulesDir.length() > 0) {
command.addArgument("--rules-dir");
command.addArgument(rulesDir);
}

command.addArgument("--config");
command.addArgument(configFile);
command.addArgument(file.trim());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/pablissimo/sonar/TsLintSensor.java
Expand Up @@ -66,6 +66,7 @@ private boolean hasFilesToAnalyze() {
public void analyse(Project project, SensorContext context) {
String pathToTsLint = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_PATH);
String pathToTsLintConfig = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH);
String rulesDir = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR);
Integer tsLintTimeoutMs = Math.max(5000, settings.getInt(TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT));

if (pathToTsLint == null) {
Expand Down Expand Up @@ -97,7 +98,7 @@ else if (pathToTsLintConfig == null) {
Resource resource = this.getFileFromIOFile(file, project);
Issuable issuable = perspectives.as(Issuable.class, resource);

String jsonResult = executor.execute(pathToTsLint, pathToTsLintConfig, file.getAbsolutePath(), tsLintTimeoutMs);
String jsonResult = executor.execute(pathToTsLint, pathToTsLintConfig, rulesDir, file.getAbsolutePath(), tsLintTimeoutMs);

TsLintIssue[] issues = parser.parse(jsonResult);

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/pablissimo/sonar/TypeScriptPlugin.java
Expand Up @@ -52,6 +52,15 @@
project = true,
global = false
),
@Property(
key = TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR,
defaultValue = "",
type = PropertyType.STRING,
name = "Custom rules dir",
description = "Path to any custom rules directory to be supplied to TsLint",
project = true,
global = false
),
@Property(
key = TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT,
defaultValue = "60000",
Expand All @@ -68,6 +77,7 @@ public class TypeScriptPlugin extends SonarPlugin {
public static final String SETTING_TS_LINT_PATH = "sonar.ts.tslintpath";
public static final String SETTING_TS_LINT_CONFIG_PATH = "sonar.ts.tslintconfigpath";
public static final String SETTING_TS_LINT_TIMEOUT = "sonar.ts.tslinttimeout";
public static final String SETTING_TS_LINT_RULES_DIR = "sonar.ts.tslintrulesdir";
public static final String SETTING_LCOV_REPORT_PATH = "sonar.ts.lcov.reportpath";

public List getExtensions() {
Expand Down
Expand Up @@ -38,14 +38,14 @@ public Integer answer(InvocationOnMock invocation) throws Throwable {
};

when(this.commandExecutor.execute(any(Command.class), any(StreamConsumer.class), any(StreamConsumer.class), any(long.class))).then(captureCommand);
this.executorImpl.execute("path/to/tslint", "path/to/config", "path/to/file", 40000);
this.executorImpl.execute("path/to/tslint", "path/to/config", "path/to/rules", "path/to/file", 40000);

assertEquals(1, capturedCommands.size());

Command theCommand = capturedCommands.get(0);
long theTimeout = capturedTimeouts.get(0);

assertEquals("node path/to/tslint --format json --config path/to/config path/to/file", theCommand.toCommandLine());
assertEquals("node path/to/tslint --format json --rules-dir path/to/rules --config path/to/config path/to/file", theCommand.toCommandLine());
assertEquals(40000, theTimeout);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/pablissimo/sonar/TsLintSensorTest.java
Expand Up @@ -155,7 +155,7 @@ public void analyse_doesNothingWhenNoConfigPathset() throws IOException {
public void analyse_callsExecutorWithSuppliedTimeout() throws IOException {
this.sensor.analyse(mock(Project.class), mock(SensorContext.class));

verify(this.executor, times(1)).execute(any(String.class), any(String.class), any(String.class), eq(45000));
verify(this.executor, times(1)).execute(any(String.class), any(String.class), any(String.class), any(String.class), eq(45000));
}

@Test
Expand All @@ -164,6 +164,6 @@ public void analyze_callsExecutorWithAtLeast5000msTimeout() throws IOException {

this.sensor.analyse(mock(Project.class), mock(SensorContext.class));

verify(this.executor, times(1)).execute(any(String.class), any(String.class), any(String.class), eq(5000));
verify(this.executor, times(1)).execute(any(String.class), any(String.class), any(String.class), any(String.class), eq(5000));
}
}
14 changes: 13 additions & 1 deletion src/test/java/com/pablissimo/sonar/TypeScriptPluginTest.java
Expand Up @@ -53,7 +53,7 @@ public void definesExpectedProperties() {
Annotation annotation = plugin.getClass().getAnnotations()[0];
Properties propertiesAnnotation = (Properties) annotation;

assertEquals(6, propertiesAnnotation.value().length);
assertEquals(7, propertiesAnnotation.value().length);

Property[] properties = propertiesAnnotation.value();
assertNotNull(findPropertyByName(properties,
Expand All @@ -68,6 +68,8 @@ public void definesExpectedProperties() {
TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH));
assertNotNull(findPropertyByName(properties,
TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT));
assertNotNull(findPropertyByName(properties,
TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR));
}

@Test
Expand Down Expand Up @@ -120,6 +122,16 @@ public void tsLintTimeoutSettings_definedAppropriately() {
assertEquals(false, property.global());
}

@Test
public void rulesDirSetting_definedAppropriately() {
Property property = findPropertyByName(TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR);

assertEquals(PropertyType.STRING, property.type());
assertEquals("", property.defaultValue());
assertEquals(true, property.project());
assertEquals(false, property.global());
}

private Property findPropertyByName(String property) {
return findPropertyByName(((Properties) plugin.getClass()
.getAnnotations()[0]).value(), property);
Expand Down

0 comments on commit ff8faa0

Please sign in to comment.