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

Commit

Permalink
Fix #45 and make compatible with VSTS without further configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablissimo committed Aug 5, 2016
1 parent 34df3cc commit b34e86a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.pablissimo.sonar</groupId>
<artifactId>sonar-typescript-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>0.8-SNAPSHOT</version>
<version>0.9-SNAPSHOT</version>

<name>TypeScript</name>
<description>Analyse TypeScript projects</description>
Expand All @@ -29,7 +29,7 @@
<connection>scm:git:git@github.com:Pablissimo/SonarTsPlugin.git</connection>
<developerConnection>scm:git:git@github.com:Pablissimo/SonarTsPlugin.git</developerConnection>
<url>https://github.com/Pablissimo/SonarTsPlugin</url>
<tag>0.8</tag>
<tag>0.9</tag>
</scm>
<issueManagement>
<system>Github</system>
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/pablissimo/sonar/TsLintExecutorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ private static Command getBaseCommand(String pathToTsLint, String configFile, St
Command command =
Command
.create("node")
.addArgument(pathToTsLint)
.addArgument('"' + pathToTsLint + '"')
.addArgument("--format")
.addArgument("json");

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

command
.addArgument("--config")
.addArgument(configFile)
.addArgument('"' + configFile + '"')
.setNewShell(false);

return command;
Expand All @@ -50,7 +50,7 @@ public String execute(String pathToTsLint, String configFile, String rulesDir, L

int currentBatchLength = 0;
for (int i = 0; i < files.size(); i++) {
String nextPath = files.get(i).trim();
String nextPath = '"' + files.get(i).trim() + '"';

// +1 for the space we'll be adding between filenames
if (currentBatchLength + nextPath.length() + 1 > availableForBatching) {
Expand All @@ -71,7 +71,6 @@ public String execute(String pathToTsLint, String configFile, String rulesDir, L

StreamConsumer stdOutConsumer = new StreamConsumer() {
public void consumeLine(String line) {
LOG.trace("TsLint Out: " + line);
stdOut.append(line);
}
};
Expand Down Expand Up @@ -99,7 +98,7 @@ public void consumeLine(String line) {
}

String rawOutput = stdOut.toString();

// TsLint returns nonsense for its JSON output when faced with multiple files
// so we need to fix it up before we do anything else
return "[" + rawOutput.replaceAll("\\]\\[", "],[") + "]";
Expand Down
49 changes: 29 additions & 20 deletions src/main/java/com/pablissimo/sonar/TsLintSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,47 +48,56 @@ private boolean hasFilesToAnalyze() {
return fileSystem.files(this.filePredicates.hasLanguage(TypeScriptLanguage.LANGUAGE_KEY)).iterator().hasNext();
}

protected String getTsLintPath() {
private String getPath(String settingKey, String defaultValue) {
// Prefer the specified path
String toReturn = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_PATH);
String toReturn = settings.getString(settingKey);

// Fall back to a file system search if null or doesn't exist
if (toReturn == null || toReturn.isEmpty()) {
LOG.debug("Path to TsLint not specified, falling back to node_modules");
toReturn = TSLINT_FALLBACK_PATH;
LOG.debug("Path " + settingKey + " not specified, falling back to " + defaultValue);
toReturn = defaultValue;
}
else {
LOG.debug("Found TsLint path to be '" + toReturn + "'");
}

File candidateFile = new java.io.File(toReturn);
if (!candidateFile.isAbsolute()) {
candidateFile = new java.io.File(this.fileSystem.baseDir().getAbsolutePath(), toReturn);
LOG.debug("Found " + settingKey + " Lint path to be '" + toReturn + "'");
}

if (!doesFileExist(candidateFile)) {
LOG.warn("Could not find tslint at path '" + toReturn + "' - skipping tslint analysis");
toReturn = null;
return getAbsolutePath(toReturn);
}

protected String getAbsolutePath(String toReturn) {
if (toReturn != null) {
File candidateFile = new java.io.File(toReturn);
if (!candidateFile.isAbsolute()) {
candidateFile = new java.io.File(this.fileSystem.baseDir().getAbsolutePath(), toReturn);
}

if (!doesFileExist(candidateFile)) {
return null;
}

return candidateFile.getAbsolutePath();
}

return toReturn;
return null;
}

protected boolean doesFileExist(File f) {
return f.exists();
}

public void analyse(Project project, SensorContext context) {
String pathToTsLint = getTsLintPath();
String pathToTsLintConfig = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH);
String rulesDir = settings.getString(TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR);
String pathToTsLint = this.getPath(TypeScriptPlugin.SETTING_TS_LINT_PATH, TSLINT_FALLBACK_PATH);
String pathToTsLintConfig = this.getPath(TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH, CONFIG_FILENAME);
String rulesDir = this.getPath(TypeScriptPlugin.SETTING_TS_LINT_RULES_DIR, null);

Integer tsLintTimeoutMs = Math.max(5000, settings.getInt(TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT));

if (pathToTsLint == null) {
LOG.warn("Path to tslint not defined or not found. Skipping tslint analysis.");
return;
}
else if (pathToTsLintConfig == null) {
LOG.warn("Path to tslint.json configuration file (" + TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH + ") is not defined. Skipping tslint analysis.");
LOG.warn("Path to tslint.json configuration file not defined or not found. Skipping tslint analysis.");
return;
}

Expand Down Expand Up @@ -133,7 +142,7 @@ else if (pathToTsLintConfig == null) {
}

String filePath = batchIssues[0].getName();

if (!fileMap.containsKey(filePath)) {
LOG.warn("TsLint reported issues against a file that wasn't sent to it - will be ignored: " + filePath);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Integer answer(InvocationOnMock invocation) throws Throwable {
Command theCommand = capturedCommands.get(0);
long theTimeout = capturedTimeouts.get(0);

assertEquals("node path/to/tslint --format json --rules-dir path/to/rules --config path/to/config path/to/file path/to/another", theCommand.toCommandLine());
assertEquals("node \"path/to/tslint\" --format json --rules-dir \"path/to/rules\" --config \"path/to/config\" \"path/to/file\" \"path/to/another\"", theCommand.toCommandLine());
// Expect one timeout period per file processed
assertEquals(2 * 40000, theTimeout);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public void BatchesExecutions_IfTooManyFilesForCommandLine() {
String firstBatch = "first batch";
while (currentLength + 12 < TsLintExecutorImpl.MAX_COMMAND_LENGTH - standardCmdLength) {
filenames.add(firstBatch);
currentLength += firstBatch.length() + 1;
currentLength += firstBatch.length() + 3; // 1 for the space, 2 for the quotes
}
filenames.add("second batch");

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/pablissimo/sonar/TsLintSensorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ public void setUp() throws Exception {
doReturn(this.executor).when(this.sensor).getTsLintExecutor();
doReturn(this.parser).when(this.sensor).getTsLintParser();
doReturn(true).when(this.sensor).doesFileExist(any(File.class));

// For now, pretend all paths are absolute
when(this.sensor.getAbsolutePath(any(String.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return (String) invocation.getArguments()[0];
}
});
}

@After
Expand Down

0 comments on commit b34e86a

Please sign in to comment.