@Test public void testSettings() { Settings settings = new MapSettings(); settings.setProperty("foo", "bar"); tester.setSettings(settings); assertThat(tester.settings().getString("foo")).isEqualTo("bar"); }
@Test public void testForceSeverity() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR"); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR); } }
@Test public void testProvideGap() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.gap()).isEqualTo(1.2d); } }
@Test public void testProvideGap_before_5_5() throws IOException { DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo") .setLanguage(Xoo.KEY) .initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n") .build(); SensorContextTester context = SensorContextTester.create(temp.newFolder()); context.fileSystem().add(inputFile); context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); context.setRuntime(SonarRuntimeImpl.forSonarQube(Version.parse("5.4"), SonarQubeSide.SCANNER)); sensor.execute(context); assertThat(context.allIssues()).hasSize(10); // One issue per line for (Issue issue : context.allIssues()) { assertThat(issue.gap()).isEqualTo(1.2d); } }
private PerlCriticIssuesLoaderSensor createSensor() { context.settings().setProperty(PerlCriticProperties.PERLCRITIC_REPORT_PATH_KEY, "src/test/resources/basic/perlcritic_report.txt"); return new PerlCriticIssuesLoaderSensor(); }
@Test void no_issues_with_sonarqube_71() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 1); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("govet-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).containsExactly("GoVetReportSensor: Import of external issues requires SonarQube 7.2 or greater."); }
@Test void no_issues_with_invalid_report_line() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("golint-report-with-error.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(0); assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("GoLintReportSensor: Unexpected line: xyz"); }
@Test void no_issues_with_sonarqube_71() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 1); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("golint-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).containsExactly("GoLintReportSensor: Import of external issues requires SonarQube 7.2 or greater."); }
@Test void no_issues_with_invalid_report_line() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.gometalinter.reportPaths", REPORT_BASE_PATH.resolve("gometalinter-report-with-error.txt").toString()); List<org.sonar.api.batch.sensor.issue.ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoMetaLinterReportSensor(), context); assertThat(externalIssues).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(0); assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("GoMetaLinterReportSensor: Unexpected line: invalid-invalid-invalid"); }
@Test void no_issues_with_invalid_report_line() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("govet-report-with-error.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(0); assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1); assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("GoVetReportSensor: Unexpected line: abcdefghijkl"); }
@Test void no_issues_with_invalid_report_path() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("invalid-path.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).startsWith("GoVetReportSensor: No issues information will be saved as the report file"); }
@Test void no_issues_with_invalid_report_path() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("invalid-path.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).startsWith("GoLintReportSensor: No issues information will be saved as the report file"); }
@Test void should_match_govet_all_keys() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("all-govet-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).hasSize(263); Stream<String> uniqueKeys = externalIssues.stream().map(externalIssue -> externalIssue.ruleKey().rule()).distinct(); assertThat(uniqueKeys).hasSize(19); // all messages are associated to a rule key assertThat(externalIssues).filteredOn("ruleKey.rule", GENERIC_ISSUE_KEY).hasSize(0); }
@Test void should_match_golint_all_keys() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("all-golint-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues).hasSize(102); Stream<String> uniqueKeys = externalIssues.stream().map(externalIssue -> externalIssue.ruleKey().rule()).distinct(); assertThat(uniqueKeys).hasSize(18); // all messages are associated to a rule key assertThat(externalIssues).filteredOn("ruleKey.rule", GENERIC_ISSUE_KEY).hasSize(0); }
@Test void should_match_govet_asm_keys() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("asm-govet-report.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues).hasSize(734); // all messages should be matched to asmdecl rule key assertThat(externalIssues).extracting("ruleKey.rule").containsOnly("asmdecl"); }
@Test void should_match_to_generic_issue_if_match_not_found() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.golint.reportPaths", REPORT_BASE_PATH.resolve("golint-with-unknown-message.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoLintReportSensor(), context); assertThat(externalIssues.get(0).ruleKey().rule()).isEqualTo(GENERIC_ISSUE_KEY); }
@Test void should_match_to_generic_issue_if_match_not_found() throws IOException { SensorContextTester context = ExternalLinterSensorHelper.createContext(7, 2); context.settings().setProperty("sonar.go.govet.reportPaths", REPORT_BASE_PATH.resolve("govet-with-unknown-message.txt").toString()); List<ExternalIssue> externalIssues = ExternalLinterSensorHelper.executeSensor(new GoVetReportSensor(), context); assertThat(externalIssues.get(0).ruleKey().rule()).isEqualTo(GENERIC_ISSUE_KEY); } }
@Test public void get_report_paths() { SensorContextTester context = SensorContextTester.create(COVERAGE_DIR); context.setSettings(new MapSettings()); Path coverageFile1 = COVERAGE_DIR.resolve("coverage.linux.relative.out").toAbsolutePath(); context.settings().setProperty("sonar.go.coverage.reportPaths", coverageFile1 + ",coverage.linux.absolute.out"); Stream<Path> reportPaths = GoCoverageReport.getReportPaths(context); assertThat(reportPaths).containsExactlyInAnyOrder( coverageFile1, Paths.get("src", "test", "resources", "coverage", "coverage.linux.absolute.out")); }
@Test public void should_continue_if_parsing_fails() { SensorContextTester context = SensorContextTester.create(COVERAGE_DIR); context.setSettings(new MapSettings()); context.settings().setProperty("sonar.go.coverage.reportPaths", "test1" + File.separator + "coverage.out, coverage.relative.out"); Path baseDir = COVERAGE_DIR.toAbsolutePath(); GoPathContext goContext = new GoPathContext(File.separatorChar, File.pathSeparator, baseDir.toString()); GoCoverageReport.saveCoverageReports(context, goContext); assertThat(logTester.logs(LoggerLevel.ERROR)).hasSize(1); assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).endsWith("coverage.out: Invalid go coverage, expect 'mode:' on the first line."); }
@Test public void should_not_throw_on_report_file_not_found() { String relativePath = "lib/Sample/Project.pm"; inputFile(relativePath); context.settings().setProperty(PerlCriticProperties.PERLCRITIC_REPORT_PATH_KEY, "src/test/resources/basic/perlcritic_nonexistant_report.txt"); new PerlCriticIssuesLoaderSensor().execute(context); }