Search in sources :

Example 11 with InputFile

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");
}
Also used : TaskResult(org.sonar.scanner.mediumtest.TaskResult) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 12 with InputFile

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);
}
Also used : PathResolver(org.sonar.api.scan.filesystem.PathResolver) RulesBuilder(org.sonar.api.batch.rule.internal.RulesBuilder) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputModuleHierarchy(org.sonar.api.batch.fs.internal.InputModuleHierarchy) UserRepositoryLoader(org.sonar.scanner.repository.user.UserRepositoryLoader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) DefaultComponentTree(org.sonar.scanner.scan.DefaultComponentTree) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultInputDir(org.sonar.api.batch.fs.internal.DefaultInputDir) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Before(org.junit.Before)

Example 13 with InputFile

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);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 14 with InputFile

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();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 15 with InputFile

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);
        }
    }
}
Also used : MutableTestCase(org.sonar.api.test.MutableTestCase) ArrayList(java.util.ArrayList) MutableTestable(org.sonar.api.test.MutableTestable) IOException(java.io.IOException) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) MutableTestPlan(org.sonar.api.test.MutableTestPlan) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8