Search in sources :

Example 36 with AnalysisResult

use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.

the class CoverageMediumTest method twoReports.

@Test
public void twoReports() throws IOException {
    File baseDir = temp.getRoot();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, "function foo() {\n  if (a && b) {\nalert('hello');\n}\n}", StandardCharsets.UTF_8);
    File xooUtCoverageFile = new File(srcDir, "sample.xoo.coverage");
    FileUtils.write(xooUtCoverageFile, "2:2:2:2\n4:0", StandardCharsets.UTF_8);
    File xooItCoverageFile = new File(srcDir, "sample.xoo.itcoverage");
    FileUtils.write(xooItCoverageFile, "2:0:2:1\n3:1\n5:0", StandardCharsets.UTF_8);
    AnalysisResult result = tester.newAnalysis().properties(ImmutableMap.<String, String>builder().put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.sources", "src").build()).execute();
    InputFile file = result.inputFile("src/sample.xoo");
    assertThat(result.coverageFor(file, 2).getHits()).isTrue();
    assertThat(result.coverageFor(file, 2).getConditions()).isEqualTo(2);
    assertThat(result.coverageFor(file, 2).getCoveredConditions()).isEqualTo(2);
    assertThat(result.coverageFor(file, 3).getHits()).isTrue();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 37 with AnalysisResult

use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.

the class CoverageMediumTest method module_level_exclusions_override_parent_for_multi_module_project.

@Test
public void module_level_exclusions_override_parent_for_multi_module_project() throws IOException {
    File baseDir = temp.getRoot();
    File baseDirModuleA = new File(baseDir, "moduleA");
    File baseDirModuleB = new File(baseDir, "moduleB");
    File srcDirA = new File(baseDirModuleA, "src");
    srcDirA.mkdirs();
    File srcDirB = new File(baseDirModuleB, "src");
    srcDirB.mkdirs();
    File xooFileA = new File(srcDirA, "sampleA.xoo");
    File xooUtCoverageFileA = new File(srcDirA, "sampleA.xoo.coverage");
    FileUtils.write(xooFileA, "function foo() {\n  if (a && b) {\nalert('hello');\n}\n}", StandardCharsets.UTF_8);
    FileUtils.write(xooUtCoverageFileA, "2:2:2:1\n3:1", StandardCharsets.UTF_8);
    File xooFileB = new File(srcDirB, "sampleB.xoo");
    File xooUtCoverageFileB = new File(srcDirB, "sampleB.xoo.coverage");
    FileUtils.write(xooFileB, "function foo() {\n  if (a && b) {\nalert('hello');\n}\n}", StandardCharsets.UTF_8);
    FileUtils.write(xooUtCoverageFileB, "2:2:2:1\n3:1", StandardCharsets.UTF_8);
    AnalysisResult result = tester.newAnalysis().properties(ImmutableMap.<String, String>builder().put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.sources", "src").put("sonar.modules", "moduleA,moduleB").put("sonar.coverage.exclusions", "**/*.xoo").put("moduleA.sonar.coverage.exclusions", "**/*.nothing").build()).execute();
    InputFile fileA = result.inputFile("moduleA/src/sampleA.xoo");
    assertThat(result.coverageFor(fileA, 2)).isNotNull();
    InputFile fileB = result.inputFile("moduleB/src/sampleB.xoo");
    assertThat(result.coverageFor(fileB, 2)).isNull();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 38 with AnalysisResult

use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.

the class CoverageMediumTest method fallbackOnExecutableLines.

@Test
public void fallbackOnExecutableLines() throws IOException {
    File baseDir = temp.getRoot();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    File measuresFile = new File(srcDir, "sample.xoo.measures");
    FileUtils.write(xooFile, "function foo() {\n  if (a && b) {\nalert('hello');\n}\n}", StandardCharsets.UTF_8);
    FileUtils.write(measuresFile, "executable_lines_data:2=1;3=1;4=0", StandardCharsets.UTF_8);
    AnalysisResult result = tester.newAnalysis().properties(ImmutableMap.<String, String>builder().put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.sources", "src").build()).execute();
    InputFile file = result.inputFile("src/sample.xoo");
    assertThat(result.coverageFor(file, 1)).isNull();
    assertThat(result.coverageFor(file, 2).getHits()).isFalse();
    assertThat(result.coverageFor(file, 2).getConditions()).isZero();
    assertThat(result.coverageFor(file, 2).getCoveredConditions()).isZero();
    assertThat(result.coverageFor(file, 3).getHits()).isFalse();
    assertThat(result.coverageFor(file, 4)).isNull();
    assertThat(result.allMeasures().get(file.key())).extracting(ScannerReport.Measure::getMetricKey, m -> m.getStringValue().getValue()).contains(tuple("executable_lines_data", "2=1;3=1;4=0"));
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) XooPlugin(org.sonar.xoo.XooPlugin) ImmutableMap(com.google.common.collect.ImmutableMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) AssertionsForClassTypes.tuple(org.assertj.core.api.AssertionsForClassTypes.tuple) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) ScannerMediumTester(org.sonar.scanner.mediumtest.ScannerMediumTester) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) TemporaryFolder(org.junit.rules.TemporaryFolder) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 39 with AnalysisResult

use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.

the class ChecksMediumTest method testCheckWithTemplate.

@Test
public void testCheckWithTemplate() throws IOException {
    File baseDir = temp.getRoot();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, "foo\nbar");
    AnalysisResult result = tester.newAnalysis().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").build()).execute();
    List<Issue> issues = result.issuesFor(result.inputFile("src/sample.xoo"));
    // If the message is the rule name. it's set by the CE. See SONAR-11531
    assertThat(issues).extracting("msg", "textRange.startLine").containsOnly(tuple("", 1), tuple("", 2));
}
Also used : Issue(org.sonar.scanner.protocol.output.ScannerReport.Issue) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) Test(org.junit.Test)

Example 40 with AnalysisResult

use of org.sonar.scanner.mediumtest.AnalysisResult in project sonarqube by SonarSource.

the class IssuesMediumTest method testOneIssuePerLine.

@Test
public void testOneIssuePerLine() throws Exception {
    File projectDir = new File("test-resources/mediumtest/xoo/sample");
    File tmpDir = temp.newFolder();
    FileUtils.copyDirectory(projectDir, tmpDir);
    AnalysisResult result = tester.newAnalysis(new File(tmpDir, "sonar-project.properties")).execute();
    List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(issues).hasSize(8);
    List<ExternalIssue> externalIssues = result.externalIssuesFor(result.inputFile("xources/hello/HelloJava.xoo"));
    assertThat(externalIssues).isEmpty();
}
Also used : ExternalIssue(org.sonar.scanner.protocol.output.ScannerReport.ExternalIssue) ExternalIssue(org.sonar.scanner.protocol.output.ScannerReport.ExternalIssue) Issue(org.sonar.scanner.protocol.output.ScannerReport.Issue) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)86 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)86 File (java.io.File)80 InputFile (org.sonar.api.batch.fs.InputFile)54 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)33 ExternalIssue (org.sonar.scanner.protocol.output.ScannerReport.ExternalIssue)15 Issue (org.sonar.scanner.protocol.output.ScannerReport.Issue)15 List (java.util.List)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Rule (org.junit.Rule)6 ScannerMediumTester (org.sonar.scanner.mediumtest.ScannerMediumTester)6 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)6 XooPlugin (org.sonar.xoo.XooPlugin)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 Assertions.tuple (org.assertj.core.api.Assertions.tuple)3 AnalysisBuilder (org.sonar.scanner.mediumtest.ScannerMediumTester.AnalysisBuilder)3 XooRulesDefinition (org.sonar.xoo.rule.XooRulesDefinition)3 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2