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

Commit

Permalink
Merge 505d895 into 54f23e6
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablissimo committed Sep 8, 2016
2 parents 54f23e6 + 505d895 commit 2e1e345
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/pablissimo/sonar/TsCoverageSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ protected void saveMeasureFromLCOVFile(Project project, SensorContext context) {

LCOVParser parser = getParser(moduleFileSystem.baseDir());
Map<String, CoverageMeasuresBuilder> coveredFiles = parser.parseFile(lcovFile);

LOG.debug("Found coverage information for " + coveredFiles.size() + " files:");
for (String key : coveredFiles.keySet()) {
LOG.debug(" " + key);
}

final boolean ignoreNotFound = isIgnoreNotFoundActivated();

Expand All @@ -92,6 +97,9 @@ protected void saveMeasureFromLCOVFile(Project project, SensorContext context) {
CoverageMeasuresBuilder fileCoverage = coveredFiles.get(file.getAbsolutePath());
org.sonar.api.resources.File resource = this.fileFromIoFile(file, project);

if (fileCoverage == null) {
LOG.debug("No coverage found for '" + file.getAbsolutePath() + "'");
}
if (fileCoverage != null) {
for (Measure measure : fileCoverage.createMeasures()) {
context.saveMeasure(resource, measure);
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/com/pablissimo/sonar/TsLintExecutorImpl.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.pablissimo.sonar;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.command.Command;
import org.sonar.api.utils.command.CommandExecutor;
import org.sonar.api.utils.command.StreamConsumer;
Expand All @@ -15,24 +18,38 @@ public class TsLintExecutorImpl implements TsLintExecutor {

private StringBuilder stdOut;
private StringBuilder stdErr;

private boolean mustQuoteSpaceContainingPaths = false;

public TsLintExecutorImpl(System2 system) {
this.mustQuoteSpaceContainingPaths = system.isOsWindows();
}

private String preparePath(String path) {
if (path.contains(" ") && this.mustQuoteSpaceContainingPaths) {
return '"' + path + '"';
}

return path;
}

private static Command getBaseCommand(String pathToTsLint, String configFile, String rulesDir) {
private Command getBaseCommand(String pathToTsLint, String configFile, String rulesDir) {
Command command =
Command
.create("node")
.addArgument('"' + pathToTsLint + '"')
.addArgument(this.preparePath(pathToTsLint))
.addArgument("--format")
.addArgument("json");

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

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

return command;
Expand All @@ -50,7 +67,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 = this.preparePath(files.get(i).trim());

// +1 for the space we'll be adding between filenames
if (currentBatchLength + nextPath.length() + 1 > availableForBatching) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/pablissimo/sonar/TsLintSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.utils.System2;

import java.io.File;
import java.util.*;
Expand All @@ -31,13 +32,15 @@ public class TsLintSensor implements Sensor {
private FilePredicates filePredicates;
private ResourcePerspectives perspectives;
private RuleFinder ruleFinder;

public TsLintSensor(Settings settings, FileSystem fileSystem, ResourcePerspectives perspectives, RuleFinder ruleFinder) {
private System2 system;

public TsLintSensor(Settings settings, FileSystem fileSystem, ResourcePerspectives perspectives, RuleFinder ruleFinder, System2 system) {
this.settings = settings;
this.fileSystem = fileSystem;
this.filePredicates = fileSystem.predicates();
this.perspectives = perspectives;
this.ruleFinder = ruleFinder;
this.system = system;
}

public boolean shouldExecuteOnProject(Project project) {
Expand Down Expand Up @@ -178,7 +181,7 @@ protected org.sonar.api.resources.File getFileFromIOFile(File file, Project proj
}

protected TsLintExecutor getTsLintExecutor() {
return new TsLintExecutorImpl();
return new TsLintExecutorImpl(this.system);
}

protected TsLintParser getTsLintParser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.command.Command;
import org.sonar.api.utils.command.CommandExecutor;
import org.sonar.api.utils.command.StreamConsumer;
Expand All @@ -19,11 +20,13 @@
public class TsLintExecutorImplTest {
TsLintExecutorImpl executorImpl;
CommandExecutor commandExecutor;
System2 system;

@Before
public void setUp() throws Exception {
this.system = mock(System2.class);
this.commandExecutor = mock(CommandExecutor.class);
this.executorImpl = spy(new TsLintExecutorImpl());
this.executorImpl = spy(new TsLintExecutorImpl(this.system));
when(this.executorImpl.createExecutor()).thenReturn(this.commandExecutor);
}

Expand All @@ -49,7 +52,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 +108,7 @@ public void BatchesExecutions_IfTooManyFilesForCommandLine() {
String firstBatch = "first batch";
while (currentLength + 12 < TsLintExecutorImpl.MAX_COMMAND_LENGTH - standardCmdLength) {
filenames.add(firstBatch);
currentLength += firstBatch.length() + 3; // 1 for the space, 2 for the quotes
currentLength += firstBatch.length() + 1; // 1 for the space
}
filenames.add("second batch");

Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/pablissimo/sonar/TsLintSensorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.pablissimo.sonar.model.TsLintPosition;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.utils.System2;

public class TsLintSensorTest {
Settings settings;
Expand All @@ -39,6 +40,7 @@ public class TsLintSensorTest {
FilePredicate predicate;
Issuable issuable;
IssueBuilder issueBuilder;
System2 system;

List<File> files;
File file;
Expand All @@ -51,6 +53,7 @@ public class TsLintSensorTest {
@Before
public void setUp() throws Exception {
this.settings = mock(Settings.class);
this.system = mock(System2.class);
when(this.settings.getString(TypeScriptPlugin.SETTING_TS_LINT_PATH)).thenReturn("/path/to/tslint");
when(this.settings.getString(TypeScriptPlugin.SETTING_TS_LINT_CONFIG_PATH)).thenReturn("/path/to/tslint.json");
when(this.settings.getInt(TypeScriptPlugin.SETTING_TS_LINT_TIMEOUT)).thenReturn(45000);
Expand Down Expand Up @@ -143,7 +146,7 @@ public void setUp() throws Exception {

this.executor = mock(TsLintExecutor.class);
this.parser = mock(TsLintParser.class);
this.sensor = spy(new TsLintSensor(settings, fileSystem, perspectives, ruleFinder));
this.sensor = spy(new TsLintSensor(settings, fileSystem, perspectives, ruleFinder, system));
doReturn(this.sonarFile).when(this.sensor).getFileFromIOFile(eq(this.file), any(Project.class));
doReturn(this.executor).when(this.sensor).getTsLintExecutor();
doReturn(this.parser).when(this.sensor).getTsLintParser();
Expand Down Expand Up @@ -243,23 +246,23 @@ public void analyze_callsExecutorWithAtLeast5000msTimeout() throws IOException {
@Test
public void check_getExecutor()
{
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder);
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder, system);
TsLintExecutor executor = sensor.getTsLintExecutor();
assertNotNull(executor);
}

@Test
public void check_getParser()
{
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder);
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder, system);
TsLintParser parser = sensor.getTsLintParser();
assertNotNull(parser);
}

@Test
public void check_getTsRulesDefinition()
{
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder);
TsLintSensor sensor = new TsLintSensor(settings, fileSystem, perspectives, ruleFinder, system);
TsRulesDefinition rulesDef = sensor.getTsRulesDefinition();
assertNotNull(rulesDef);
}
Expand Down

0 comments on commit 2e1e345

Please sign in to comment.