Search in sources :

Example 91 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class SurefireUtilsTest method should_get_reports_path_from_deprecated_property.

@Test
public void should_get_reports_path_from_deprecated_property() {
    MapSettings settings = new MapSettings();
    settings.setProperty("sonar.junit.reportsPath", "target/surefire");
    DefaultFileSystem fs = new DefaultFileSystem(new File("src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsPathFromDeprecatedProperty"));
    PathResolver pathResolver = new PathResolver();
    assertThat(logTester.logs(LoggerLevel.INFO)).isEmpty();
    List<File> directories = SurefireUtils.getReportsDirectories(settings.asConfig(), fs, pathResolver);
    assertThat(directories).hasSize(1);
    File directory = directories.get(0);
    assertThat(directory.exists()).isTrue();
    assertThat(directory.isDirectory()).isTrue();
    assertThat(logTester.logs(LoggerLevel.INFO)).contains("Property 'sonar.junit.reportsPath' is deprecated. Use property 'sonar.junit.reportPaths' instead.");
}
Also used : MapSettings(org.sonar.api.config.internal.MapSettings) PathResolver(org.sonar.api.scan.filesystem.PathResolver) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 92 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-java by SonarSource.

the class RulesSanityTest method sonarComponents.

private static SonarComponents sonarComponents(File moduleBaseDir, List<InputFile> inputFiles) {
    SensorContextTester context = SensorContextTester.create(moduleBaseDir).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    context.setSettings(new MapSettings().setProperty(SonarComponents.FAIL_ON_EXCEPTION_KEY, true));
    DefaultFileSystem fileSystem = context.fileSystem();
    SonarComponents sonarComponents = new SonarComponents(null, fileSystem, null, null, null) {

        @Override
        public boolean reportAnalysisError(RecognitionException re, InputFile inputFile) {
            return false;
        }
    };
    sonarComponents.setSensorContext(context);
    inputFiles.forEach(inputFile -> fileSystem.add(inputFile));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 93 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-go by SonarSource.

the class GoTestSensorTest method relative_package_path_in_report.

@Test
void relative_package_path_in_report() throws IOException {
    GoTestSensor goTestSensor = new GoTestSensor();
    goTestSensor.goPathContext = new GoPathContext(File.separatorChar, File.pathSeparator, goPath.toString());
    TestInfo testInfo = new TestInfo("pass", packagePath.toString(), "TestFoo", 42.);
    Path baseDir = goPath.resolve("src").resolve(packagePath);
    SensorContextTester contextTester = SensorContextTester.create(baseDir);
    DefaultFileSystem fs = contextTester.fileSystem();
    DefaultInputFile testFile = getTestInputFile(fs, "func TestFoo(", "foo_test.go");
    InputFile foundTestFile = goTestSensor.findTestFile(fs, testInfo);
    assertThat(foundTestFile).isEqualTo(testFile);
}
Also used : Path(java.nio.file.Path) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInfo(org.sonar.go.plugin.GoTestSensor.TestInfo) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.jupiter.api.Test)

Example 94 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-go by SonarSource.

the class GoTestSensorTest method import_report.

@Test
void import_report() {
    GoTestSensor goTestSensor = new GoTestSensor();
    goTestSensor.goPathContext = new GoPathContext(File.separatorChar, File.pathSeparator, goPath.toString());
    Path baseDir = goPath.resolve("src").resolve(packagePath);
    SensorContextTester context = SensorContextTester.create(baseDir);
    DefaultFileSystem fs = context.fileSystem();
    DefaultInputFile fooTestFile = getTestInputFile(fs, "something  \nfunc TestFoo1( \nfunc TestFoo2(  ", "foo_test.go");
    DefaultInputFile barTestFile = getTestInputFile(fs, "func TestBar(", "bar_test.go");
    MapSettings settings = new MapSettings();
    String absoluteReportPath = baseDir.resolve("report1.out").toString();
    settings.setProperty(GoTestSensor.REPORT_PATH_KEY, "report.out,invilid/report/path," + absoluteReportPath);
    context.setSettings(settings);
    goTestSensor.execute(context);
    // one test comes from report1.out
    assertThat(context.measure(fooTestFile.key(), CoreMetrics.TESTS).value()).isEqualTo(3);
    assertThat(context.measure(fooTestFile.key(), CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(0);
    assertThat(context.measure(fooTestFile.key(), CoreMetrics.TEST_FAILURES).value()).isEqualTo(1);
    assertThat(context.measure(fooTestFile.key(), CoreMetrics.TEST_ERRORS)).isNull();
    assertThat(context.measure(fooTestFile.key(), CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(4);
    assertThat(context.measure(barTestFile.key(), CoreMetrics.TESTS).value()).isEqualTo(1);
    assertThat(context.measure(barTestFile.key(), CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(1);
    assertThat(context.measure(barTestFile.key(), CoreMetrics.TEST_FAILURES).value()).isEqualTo(0);
    assertThat(context.measure(barTestFile.key(), CoreMetrics.TEST_ERRORS)).isNull();
    assertThat(context.measure(barTestFile.key(), CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(7);
    assertThat(String.join("\n", logTester.logs(LoggerLevel.ERROR))).contains("Test report can't be loaded, file not found");
}
Also used : Path(java.nio.file.Path) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.jupiter.api.Test)

Example 95 with DefaultFileSystem

use of org.sonar.api.batch.fs.internal.DefaultFileSystem in project sonar-go by SonarSource.

the class GoTestSensorTest method subtests.

@Test
void subtests() throws Exception {
    GoTestSensor goTestSensor = new GoTestSensor();
    goTestSensor.goPathContext = new GoPathContext(File.separatorChar, File.pathSeparator, goPath.toString());
    Path baseDir = goPath.resolve("src").resolve(packagePath);
    SensorContextTester context = SensorContextTester.create(baseDir);
    DefaultFileSystem fs = context.fileSystem();
    DefaultInputFile mulTestFile = getTestInputFile(fs, new String(Files.readAllBytes(baseDir.resolve("mul_test.go"))), "mul_test.go");
    MapSettings settings = new MapSettings();
    String absoluteReportPath = baseDir.resolve("subtest_report.json").toString();
    settings.setProperty(GoTestSensor.REPORT_PATH_KEY, absoluteReportPath);
    context.setSettings(settings);
    goTestSensor.execute(context);
    assertThat(context.measure(mulTestFile.key(), CoreMetrics.TESTS).value()).isEqualTo(4);
}
Also used : Path(java.nio.file.Path) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)126 File (java.io.File)79 InputFile (org.sonar.api.batch.fs.InputFile)70 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)55 Test (org.junit.Test)54 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)52 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)39 Test (org.junit.jupiter.api.Test)27 SonarComponents (org.sonar.java.SonarComponents)25 Before (org.junit.Before)23 MapSettings (org.sonar.api.config.internal.MapSettings)22 PathResolver (org.sonar.api.scan.filesystem.PathResolver)20 JavaCheck (org.sonar.plugins.java.api.JavaCheck)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)13 AnalyzerMessage (org.sonar.java.AnalyzerMessage)11 Path (java.nio.file.Path)10 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)10 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)8 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)8 FileLinesContext (org.sonar.api.measures.FileLinesContext)7