use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class TestExecutionMediumTest method unitTests.
@Test
public void unitTests() throws IOException {
File baseDir = temp.getRoot();
File srcDir = new File(baseDir, "src");
srcDir.mkdir();
File testDir = new File(baseDir, "test");
testDir.mkdir();
File xooFile = new File(srcDir, "sample.xoo");
FileUtils.write(xooFile, "foo");
File xooTestFile = new File(testDir, "sampleTest.xoo");
FileUtils.write(xooTestFile, "failure\nerror\nok\nskipped");
File xooTestExecutionFile = new File(testDir, "sampleTest.xoo.test");
FileUtils.write(xooTestExecutionFile, "skipped::::SKIPPED:UNIT\n" + "failure:2:Failure::FAILURE:UNIT\n" + "error:2:Error:The stack:ERROR:UNIT\n" + "success:4:::OK:INTEGRATION");
TaskResult result = tester.newTask().properties(ImmutableMap.<String, String>builder().put("sonar.task", "scan").put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.projectName", "Foo Project").put("sonar.projectVersion", "1.0-SNAPSHOT").put("sonar.projectDescription", "Description of Foo Project").put("sonar.sources", "src").put("sonar.tests", "test").build()).start();
InputFile file = result.inputFile("test/sampleTest.xoo");
org.sonar.scanner.protocol.output.ScannerReport.Test success = result.firstTestExecutionForName(file, "success");
assertThat(success.getDurationInMs()).isEqualTo(4);
assertThat(success.getStatus()).isEqualTo(TestStatus.OK);
org.sonar.scanner.protocol.output.ScannerReport.Test error = result.firstTestExecutionForName(file, "error");
assertThat(error.getDurationInMs()).isEqualTo(2);
assertThat(error.getStatus()).isEqualTo(TestStatus.ERROR);
assertThat(error.getMsg()).isEqualTo("Error");
assertThat(error.getStacktrace()).isEqualTo("The stack");
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class JSONReportTest method before.
@Before
public void before() throws Exception {
moduleHierarchy = mock(InputModuleHierarchy.class);
userRepository = mock(UserRepositoryLoader.class);
File projectBaseDir = temp.newFolder();
fs = new DefaultFileSystem(projectBaseDir.toPath());
SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00"));
when(server.getVersion()).thenReturn("3.6");
InputComponentStore inputComponentStore = new InputComponentStore(new PathResolver());
DefaultComponentTree inputComponentTree = new DefaultComponentTree();
DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(projectBaseDir).setKey("struts"), 1);
inputComponentStore.put(rootModule);
DefaultInputModule moduleA = new DefaultInputModule("struts-core");
inputComponentTree.index(moduleA, rootModule);
DefaultInputModule moduleB = new DefaultInputModule("struts-ui");
inputComponentTree.index(moduleB, rootModule);
DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts", TestInputFileBuilder.nextBatchId()).setModuleBaseDir(projectBaseDir.toPath());
DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java").setModuleBaseDir(projectBaseDir.toPath()).build();
inputFile.setStatus(InputFile.Status.CHANGED);
inputFile.setPublish(true);
inputComponentStore.put(inputFile);
inputComponentStore.put(inputDir);
inputComponentTree.index(inputDir, rootModule);
inputComponentTree.index(inputFile, inputDir);
when(moduleHierarchy.children(rootModule)).thenReturn(Arrays.asList(moduleA, moduleB));
when(moduleHierarchy.parent(moduleA)).thenReturn(rootModule);
when(moduleHierarchy.parent(moduleB)).thenReturn(rootModule);
when(moduleHierarchy.relativePath(moduleA)).thenReturn("core");
when(moduleHierarchy.relativePath(moduleB)).thenReturn("ui");
RulesBuilder builder = new RulesBuilder();
builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles");
rules = builder.build();
jsonReport = new JSONReport(moduleHierarchy, settings, fs, server, rules, issueCache, rootModule, inputComponentStore, userRepository, inputComponentTree);
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class DefaultSensorStorageTest method shouldValidateStrictlyPositiveLine.
@Test
public void shouldValidateStrictlyPositiveLine() throws Exception {
InputFile file = new TestInputFileBuilder("module", "testfile").setModuleBaseDir(temp.newFolder().toPath()).build();
Map<Integer, Integer> map = ImmutableMap.of(0, 3);
String data = KeyValueFormat.format(map);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("must be > 0");
underTest.validateCoverageMeasure(data, file);
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class CoverageExclusionsTest method shouldExcludeFileBasedOnPattern.
@Test
public void shouldExcludeFileBasedOnPattern() {
InputFile file = new TestInputFileBuilder("foo", "src/org/polop/File.php").build();
settings.setProperty("sonar.coverage.exclusions", "src/org/polop/*");
coverageExclusions.initPatterns();
assertThat(coverageExclusions.isExcluded(file)).isTrue();
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class CoveragePerTestSensor method processTestFile.
private void processTestFile(InputFile inputFile, SensorContext context) {
File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
if (testExecutionFile.exists()) {
LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
int lineNumber = 0;
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(";").split(line).iterator();
String name = split.next();
while (split.hasNext()) {
String coveredBlockStr = split.next();
Iterator<String> splitCoveredBlock = Splitter.on(",").split(coveredBlockStr).iterator();
String componentPath = splitCoveredBlock.next();
InputFile coveredFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(componentPath));
MutableTestable testable = perspectives.as(MutableTestable.class, coveredFile);
List<Integer> coveredLines = new ArrayList<>();
while (splitCoveredBlock.hasNext()) {
coveredLines.add(Integer.parseInt(splitCoveredBlock.next()));
}
for (MutableTestCase testCase : testPlan.testCasesByName(name)) {
testCase.setCoverageBlock(testable, coveredLines);
}
}
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Aggregations