org.sonar.api.batch.sensor.issue.Issue Java Examples

The following examples show how to use org.sonar.api.batch.sensor.issue.Issue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AncientSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
@Test
public void shouldExecuteAncient() throws IOException {
    SensorContextTester context = prepareContext();

    CommandStreamConsumer stdOut = new CommandStreamConsumer();
    stdOut.consumeLine("This is some non related line which should not end to report");
    stdOut.consumeLine("[metosin/reitit \"0.2.10\"] is available but we use \"0.2.1\"");
    stdOut.consumeLine("[metosin/ring-http-response \"0.9.1\"] is available but we use \"0.9.0\"");
    when(commandRunner.run(any(), eq("lein"), eq("ancient"))).thenReturn(stdOut);

    ancientSensor.execute(context);

    List<Issue> issuesList = new ArrayList<>(context.allIssues());

    assertThat(issuesList.size(), is(2));
    assertThat(issuesList.get(0).ruleKey().rule(), is("ancient-clj-dependency"));
    assertThat(issuesList.get(0).primaryLocation().message(),
            is("metosin/reitit is using version: 0.2.1 but version: 0.2.10 is available."));

    assertThat(issuesList.get(1).ruleKey().rule(), is("ancient-clj-dependency"));
    assertThat(issuesList.get(1).primaryLocation().message(),
            is("metosin/ring-http-response is using version: 0.9.0 but version: 0.9.1 is available."));
}
 
Example #2
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void custom_rule() throws Exception {
    inputFile("file.esql");
    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("customKey", "key")).build()).build();
    checkFactory = new CheckFactory(activeRules);
    createSensorWithCustomRules().execute(context);

    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);
    Issue issue = issues.iterator().next();
    assertThat(issue.gap()).isEqualTo(42);
    assertThat(issue.primaryLocation().message()).isEqualTo("Message of custom rule");
    assertThat(issue.primaryLocation().textRange())
            .isEqualTo(new DefaultTextRange(new DefaultTextPointer(1, 0), new DefaultTextPointer(1, 24)));
}
 
Example #3
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void analysis_with_issues_should_not_add_error_to_context() {
    inputFile("file.esql");

    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "MissingNewlineAtEndOfFile")).build())
            .build();
    checkFactory = new CheckFactory(activeRules);

    createSensor().execute(context);

    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);

    assertThat(context.allAnalysisErrors()).isEmpty();
}
 
Example #4
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void parsing_error() {
    String relativePath = "cpd/parsingError.esql";
    inputFile(relativePath);

    String parsingErrorCheckKey = "ParsingError";

    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setName("ParsingError").setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, parsingErrorCheckKey)).build())
            .build();

    checkFactory = new CheckFactory(activeRules);

    context.setActiveRules(activeRules);
    createSensor().execute(context);
    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(1);
    Issue issue = issues.iterator().next();
    assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(3);
    assertThat(issue.primaryLocation().message()).isEqualTo("Parse error");

    assertThat(context.allAnalysisErrors()).hasSize(1);
}
 
Example #5
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  String relativePath = "parsing-error.feature";
  inputFile(relativePath, Charsets.UTF_8);

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(GherkinRulesDefinition.REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createGherkinSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example #6
Source File: ScssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.scss");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.SCSS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createScssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example #7
Source File: LessAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.less");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.LESS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createLessSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example #8
Source File: CssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void should_raise_an_issue_because_the_parsing_error_rule_is_activated() {
  inputFile("parsingError.css");

  ActiveRules activeRules = (new ActiveRulesBuilder())
    .create(RuleKey.of(CheckList.CSS_REPOSITORY_KEY, "S2260"))
    .activate()
    .build();

  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createCssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(1);
  Issue issue = issues.iterator().next();
  assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
}
 
Example #9
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveIssue() throws IOException {
    InputFile playbook = Utils.getInputFile("playbooks/playbook1.yml");

    // Try to save issue for an unknown rule
    logTester.clear();
    sensor.saveIssue(context, playbook, 2, "foo", "An error here");
    assertEquals(1, logTester.logs(LoggerLevel.DEBUG).size());
    assertEquals("Rule foo ignored, not found in repository", logTester.logs(LoggerLevel.DEBUG).get(0));

    // Save issue for a known rule
    logTester.clear();
    sensor.saveIssue(context, playbook, 2, RULE_ID2, "An error here");
    assertEquals(1, context.allIssues().size());
    Issue issue = (Issue)context.allIssues().toArray()[0];
    assertEquals(ruleKey2, issue.ruleKey());
    IssueLocation location = issue.primaryLocation();
    assertEquals(playbook.key(), location.inputComponent().key());
    assertEquals(2, location.textRange().start().line());
    assertEquals("An error here", location.message());
}
 
Example #10
Source File: EastwoodSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
@Test
public void shouldExecuteEastwood() throws IOException {
    SensorContextTester context = prepareContext();

    CommandStreamConsumer stdOut = new CommandStreamConsumer();
    stdOut.consumeLine("file.clj:1:0:issue-1:description-1");
    stdOut.consumeLine("file.clj:2:0:issue-2:description-2");
    String options = "eastwood-option";
    when(commandRunner.run(300L, "lein", "eastwood", options))
            .thenReturn(stdOut);

    eastwoodSensor.execute(context);

    List<Issue> issuesList = new ArrayList<>(context.allIssues());
    assertThat(issuesList.size(), is(2));
    assertThat(issuesList.get(0).ruleKey().rule(), is("issue-1"));
    assertThat(issuesList.get(0).primaryLocation().message(), is("description-1"));
    assertThat(issuesList.get(1).ruleKey().rule(), is("issue-2"));
    assertThat(issuesList.get(1).primaryLocation().message(), is("description-2"));
}
 
Example #11
Source File: LeinNvdSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
@Test
public void shouldExecuteLeinNvd() throws IOException {
    SensorContextTester context = prepareContext();

    CommandStreamConsumer stdOut = new CommandStreamConsumer();
    stdOut.consumeLine("We are not really interested of std out");

    leinNvdSensor.execute(context);

    List<Issue> issuesList = new ArrayList<>(context.allIssues());
    assertThat(issuesList.size(), is(2));
    assertThat(issuesList.get(0).ruleKey().rule(), is("nvd-high"));
    assertThat(issuesList.get(0).primaryLocation().message(),
            is("CVE-2018-5968;CWE-502,CWE-184;jackson-databind-2.9.3.jar"));
    assertThat(issuesList.get(1).ruleKey().rule(), is("nvd-critical"));
    assertThat(issuesList.get(1).primaryLocation().message(),
            is("CVE-2018-19362;CWE-502;jackson-databind-2.9.3.jar"));
}
 
Example #12
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintWinthConf() throws IOException {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    context.fileSystem().add(playbook1);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/echo_as_issue.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/echo_as_issue.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        setShellRights(path);
    }

    context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_CONF_PATH_KEY, "/path/to/ansible-lint.conf");

    sensor.executeWithAnsibleLint(context, null);
    Collection<Issue> issues = context.allIssues();
    assertEquals(1, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook1, 2, "-p --nocolor -c /path/to/ansible-lint\\.conf " + Pattern.quote(new File(playbook1.uri()).getAbsolutePath())));
}
 
Example #13
Source File: KibitSensorTest.java    From sonar-clojure with MIT License 6 votes vote down vote up
@Test
public void shouldExecuteKibit() throws IOException {
    SensorContextTester context = prepareContext();

    CommandStreamConsumer stdOut = new CommandStreamConsumer();
    stdOut.consumeLine("----");
    stdOut.consumeLine("At kibit.clj:5:");
    stdOut.consumeLine("Kibit will say that there is pos? function available");
    when(commandRunner.run(300L, "lein", "kibit")).thenReturn(stdOut);

    kibitSensor.execute(context);

    List<Issue> issuesList = new ArrayList<>(context.allIssues());
    assertThat(issuesList.size(), is(1));
    assertThat(issuesList.get(0).ruleKey().rule(), is("kibit"));
}
 
Example #14
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintWinthoutConf() throws IOException {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    context.fileSystem().add(playbook1);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/echo_as_issue.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/echo_as_issue.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        setShellRights(path);
    }

    sensor.executeWithAnsibleLint(context, null);
    Collection<Issue> issues = context.allIssues();
    assertEquals(1, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook1, 2, "-p --nocolor " + Pattern.quote(new File(playbook1.uri()).getAbsolutePath())));
}
 
Example #15
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintIOException() throws IOException, InterruptedException {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    InputFile playbook2 = Utils.getInputFile("playbooks/playbook2.yml");
    InputFile playbook3 = Utils.getInputFile("playbooks/playbook3.yml");
    context.fileSystem().add(playbook1).add(playbook2).add(playbook3);

    MySensor theSensor = spy(sensor);
    doThrow(new IOException("Boom!")).when(theSensor).executeCommand(any(), any(), any());

    theSensor.executeWithAnsibleLint(context, Arrays.asList("foo", "bar"));
    assertEquals(1, theSensor.scannedFiles.size());
    assertTrue(theSensor.scannedFiles.contains(playbook1));

    Collection<Issue> issues = context.allIssues();
    assertEquals(0, issues.size());
}
 
Example #16
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveIssues() throws Exception {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    InputFile playbook2 = Utils.getInputFile("playbooks/playbook2.yml");
    InputFile playbook3 = Utils.getInputFile("playbooks/playbook3.yml");
    sensor.scannedFiles.add(playbook1);
    sensor.scannedFiles.add(playbook2);
    sensor.scannedFiles.add(playbook3);
    sensor.registerIssue("Bla bla");
    sensor.registerIssue(playbook1.toString() + ":Bla bla");
    sensor.registerIssue(playbook1.toString() + ":2: [EUNKNOWN] Bla bla");
    sensor.registerIssue(playbook1.toString() + ":3: [EAnyCheck1] An error");
    sensor.registerIssue(playbook1.toString() + ":4: [AnyCheck2] Another error");
    sensor.registerIssue(playbook1.toString() + ":5: [EAnyCheck2] Another error");
    sensor.registerIssue(playbook2.toString() + ":3: [EAnyCheck2] Another error");
    sensor.saveIssues(context);

    Collection<Issue> issues = context.allIssues();
    assertEquals(3, issues.size());
    assertTrue(issueExists(issues, ruleKey2, playbook1, 3, "An error"));
    assertTrue(issueExists(issues, ruleKey3, playbook1, 5, "Another error"));
    assertTrue(issueExists(issues, ruleKey3, playbook2, 3, "Another error"));
}
 
Example #17
Source File: ScssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.scss");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createScssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example #18
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void save_issue() throws Exception {
    inputFile("file.esql");

    ActiveRules activeRules = (new ActiveRulesBuilder())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "MissingNewlineAtEndOfFile")).build())
            .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "InitializeVariables")).build())
            .build();

    checkFactory = new CheckFactory(activeRules);
    createSensor().execute(context);
    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(2);
}
 
Example #19
Source File: EsqlSensorTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void analysis_with_no_issue_should_not_add_error_to_context() {
    inputFile("file.esql");

    createSensor().execute(context);

    Collection<Issue> issues = context.allIssues();
    assertThat(issues).hasSize(0);

    assertThat(context.allAnalysisErrors()).isEmpty();
}
 
Example #20
Source File: HtlSensorTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void checkIncorrectFile_issuesFound() throws Exception {
    DefaultInputFile inputFile = createInputFile(TEST_DIR, "test.html");
    tester.fileSystem().add(inputFile);
    sensor.execute(tester);
    assertThat(tester.allIssues()).isNotEmpty();
    List<String> issuesRules = tester.allIssues().stream()
        .map(Issue::ruleKey)
        .map(RuleKey::rule)
        .distinct()
        .collect(Collectors.toList());
    assertThat(issuesRules).contains(HtlAttributesShouldBeAtTheEndCheck.RULE_KEY);
}
 
Example #21
Source File: CustomChecksSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testTSQLGrammarFiles() throws IOException {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();
	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM_RULES, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_MAX_FILE_SIZE, 100);
	String dirPath = "..\\grammars\\tsql";
	File dir = new File(dirPath);
	Collection<File> files = FileUtils.listFiles(dir, new String[] { "sql" }, true);
	for (File f : files) {
		String tempName = f.getName() + System.nanoTime();
		File dest = folder.newFile(tempName);
		FileUtils.copyFile(f, dest);

		DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
				.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
		ctxTester.fileSystem().add(file1);

	}
	CustomChecksSensor sensor = new CustomChecksSensor();
	sensor.execute(ctxTester);
	Collection<Issue> issues = ctxTester.allIssues();
	Assert.assertEquals(183, issues.size());

}
 
Example #22
Source File: CustomChecksSensorTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSingleFile() throws IOException {
	TemporaryFolder folder = new TemporaryFolder();
	folder.create();

	SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());

	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM_RULES, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_SKIP_CUSTOM, false);
	ctxTester.settings().setProperty(Constants.PLUGIN_MAX_FILE_SIZE, 100);
	String tempName = "test.sql";

	File f = folder.newFile(tempName);

	FileUtils.write(f, "select * from dbo.test;\r\n--test", Charset.defaultCharset());
	DefaultInputFile file1 = new TestInputFileBuilder(folder.getRoot().getAbsolutePath(), tempName)
			.initMetadata(new String(Files.readAllBytes(f.toPath()))).setLanguage(TSQLLanguage.KEY).build();
	ctxTester.fileSystem().add(file1);

	CustomChecksSensor sensor = new CustomChecksSensor();
	sensor.execute(ctxTester);
	Collection<Issue> issues = ctxTester.allIssues();
	for (Issue i : issues) {
		System.out.println(i.ruleKey() + " " + i.primaryLocation());
	}

	Assert.assertEquals(1, issues.size());
	Assert.assertEquals(2, ctxTester.cpdTokens(file1.key()).size());
	Assert.assertEquals(1, ctxTester.highlightingTypeAt(file1.key(), 2, 1).size());
	Assert.assertEquals(4, ctxTester.measures(file1.key()).size());
}
 
Example #23
Source File: GherkinSquidSensorTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  String relativePath = "parsing-error.feature";
  inputFile(relativePath, Charsets.UTF_8);

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createGherkinSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).hasSize(0);
}
 
Example #24
Source File: AbstractAnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintStdOutput() throws IOException {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    InputFile playbook2 = Utils.getInputFile("playbooks/playbook2.yml");
    InputFile playbook3 = Utils.getInputFile("playbooks/playbook3.yml");
    context.fileSystem().add(playbook1).add(playbook2).add(playbook3);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/ansible-lint3.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/ansible-lint3.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        setShellRights(path);
    }

    sensor.executeWithAnsibleLint(context, Arrays.asList("foo", "bar"));
    assertEquals(3, sensor.scannedFiles.size());
    assertTrue(sensor.scannedFiles.contains(playbook1));
    assertTrue(sensor.scannedFiles.contains(playbook2));
    assertTrue(sensor.scannedFiles.contains(playbook3));

    Collection<Issue> issues = context.allIssues();
    assertEquals(4, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook1, 2, "A first error"));
    assertTrue(issueExists(issues, ruleKey2, playbook1, 3, "An error -p"));
    assertTrue(issueExists(issues, ruleKey3, playbook1, 5, "Another error foo"));
    assertTrue(issueExists(issues, ruleKey3, playbook2, 3, "Another error bar"));
}
 
Example #25
Source File: LessAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.less");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createLessSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example #26
Source File: CssAnalyzerSensorTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void should_not_raise_any_issue_because_the_parsing_error_rule_is_not_activated() {
  inputFile("parsingError.css");

  ActiveRules activeRules = new ActiveRulesBuilder().build();
  checkFactory = new CheckFactory(activeRules);

  context.setActiveRules(activeRules);
  createCssSquidSensor().execute(context);
  Collection<Issue> issues = context.allIssues();
  assertThat(issues).isEmpty();
}
 
Example #27
Source File: AnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
private boolean issueExists(Collection<Issue> issues, RuleKey ruleKey, InputFile file, int line, String regex) {
    // Brut force...
    for (Issue issue : issues) {
        IssueLocation location = issue.primaryLocation();
        if (issue.ruleKey().equals(ruleKey) &&
                file.key().equals(location.inputComponent().key()) &&
                line == location.textRange().start().line() &&
                location.message().matches(regex)
        ) {
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: AnsibleSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecuteWithAnsibleLintStdOutput() throws IOException {
    Path baseDir = context.fileSystem().baseDirPath();
    InputFile playbook = TestInputFileBuilder.create("moduleKey", baseDir.resolve("playbooks/playbook1.yml").toString())
            .setModuleBaseDir(Paths.get("."))
            .setContents(new String(Files.readAllBytes(baseDir.resolve("playbooks/playbook1.yml"))))
            .setLanguage(YamlLanguage.KEY)
            .setCharset(StandardCharsets.UTF_8)
            .build();
    context.fileSystem().add(playbook);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/ansible-lint.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/ansible-lint.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        Set<PosixFilePermission> perms = new HashSet<>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_EXECUTE);
        perms.add(PosixFilePermission.OTHERS_READ);
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.setPosixFilePermissions(Paths.get(path), perms);
    }

    sensor.execute(context);
    assertEquals(1, sensor.scannedFiles.size());
    assertTrue(sensor.scannedFiles.contains(playbook));

    Collection<Issue> issues = context.allIssues();
    assertEquals(3, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook, 1, "A first error"));
    assertTrue(issueExists(issues, ruleKey2, playbook, 3, "An error -p"));
    assertTrue(issueExists(issues, ruleKey3, playbook, 5, "Another error --nocolor"));
}
 
Example #29
Source File: Utils.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
public static boolean issueExists(Collection<Issue> issues, RuleKey ruleKey, InputFile file, int line, String regex) {
    // Brut force...
    for (Issue issue : issues) {
        IssueLocation location = issue.primaryLocation();
        if (issue.ruleKey().equals(ruleKey) &&
                file.key().equals(location.inputComponent().key()) &&
                line == location.textRange().start().line() &&
                location.message().matches(regex)
        ) {
            return true;
        }
    }
    return false;
}
 
Example #30
Source File: AnsibleExtraSensorTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecute() throws IOException {
    InputFile playbook1 = Utils.getInputFile("playbooks/playbook1.yml");
    InputFile playbook2 = Utils.getInputFile("playbooks/playbook2.yml");
    InputFile playbook3 = Utils.getInputFile("playbooks/playbook3.yml");
    context.fileSystem().add(playbook1).add(playbook2).add(playbook3);

    if (System.getProperty("os.name").toLowerCase().contains("windows")) {
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY,
                new File(getClass().getResource("/scripts/ansible-lint4.cmd").getFile()).getAbsolutePath());
    } else {
        String path = new File(getClass().getResource("/scripts/ansible-lint4.sh").getFile()).getAbsolutePath();
        context.settings().appendProperty(AnsibleSettings.ANSIBLE_LINT_PATH_KEY, path);
        setShellRights(path);
    }

    DummySensorDescriptor descriptor = new DummySensorDescriptor();
    sensor.describe(descriptor);

    assertEquals("Ansible-Lint Sensor with Extra Rules", descriptor.sensorName);
    assertEquals(YamlLanguage.KEY, descriptor.languageKey);

    sensor.execute(context);

    Collection<Issue> issues = context.allIssues();
    assertEquals(3, issues.size());
    assertTrue(issueExists(issues, ruleKey1, playbook1, 3, "An error -p"));
    assertTrue(issueExists(issues, ruleKey2, playbook1, 5, "Another error --nocolor"));
    assertTrue(issueExists(issues, ruleKey2, playbook2, 3, "Another error -r"));
}