use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method simpleJavaWithCommaInClasspath.
// SLCORE-350
@Disabled("Can be enabled when sonar-java is fixed with https://github.com/SonarSource/sonar-java/pull/3886")
@Test
void simpleJavaWithCommaInClasspath() throws Exception {
ClientInputFile inputFile = prepareInputFile(A_JAVA_FILE_PATH, "public class Foo {\n" + " public void foo() {\n" + " int x;\n" + " }\n" + "}", false);
final List<Issue> issues = new ArrayList<>();
sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).putExtraProperty("sonar.java.libraries", "\"" + Paths.get("target/lib/guava,with,comma.jar").toAbsolutePath().toString() + "\"").build(), issues::add, null, null);
assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, Issue::getStartLineOffset, Issue::getEndLine, Issue::getEndLineOffset, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(tuple("java:S1220", null, null, null, null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 3, 8, 3, 9, A_JAVA_FILE_PATH, "MINOR"));
}
use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method lazy_init_file_metadata.
@Test
void lazy_init_file_metadata() throws Exception {
final var inputFile1 = prepareInputFile(A_JAVA_FILE_PATH, "public class Foo {\n" + " public void foo() {\n" + " int x;\n" + " System.out.println(\"Foo\");\n" + " System.out.println(\"Foo\"); //NOSONAR\n" + " }\n" + "}", false);
var unexistingPath = new File(baseDir, "missing.bin");
assertThat(unexistingPath).doesNotExist();
ClientInputFile inputFile2 = new OnDiskTestClientInputFile(unexistingPath.toPath(), "missing.bin", false, StandardCharsets.UTF_8, null);
final List<Issue> issues = new ArrayList<>();
final List<String> logs = new CopyOnWriteArrayList<>();
var analysisResults = sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFiles(inputFile1, inputFile2).build(), issues::add, (m, l) -> logs.add(m), null);
assertThat(analysisResults.failedAnalysisFiles()).isEmpty();
assertThat(analysisResults.indexedFileCount()).isEqualTo(2);
assertThat(logs).contains("Initializing metadata of file " + inputFile1.uri()).doesNotContain("Initializing metadata of file " + inputFile2.uri());
}
use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method useRelativePathToEvaluatePathPatterns.
// SLCORE-162
@Test
void useRelativePathToEvaluatePathPatterns() throws Exception {
// Temporary file doesn't have the correct file suffix
final var file = new File(baseDir, "foo.tmp");
FileUtils.write(file, "def my_function(name):\n" + " print \"Hello\"\n" + " print \"world!\" # NOSONAR\n" + "\n", StandardCharsets.UTF_8);
ClientInputFile inputFile = new OnDiskTestClientInputFile(file.toPath(), "foo.py", false, StandardCharsets.UTF_8, null);
final List<Issue> issues = new ArrayList<>();
sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).build(), issues::add, null, null);
assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath()).containsOnly(tuple("python:PrintStatementUsage", 2, "foo.py"));
}
use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.
the class ConnectedAnalysisConfigurationTests method testToString.
@Test
void testToString(@TempDir Path temp) throws Exception {
Map<String, String> props = new HashMap<>();
props.put("sonar.java.libraries", "foo bar");
final var srcFile1 = createDirectory(temp.resolve("src1"));
final var srcFile2 = createDirectory(temp.resolve("src2"));
ClientInputFile inputFile = new TestClientInputFile(temp, srcFile1, false, StandardCharsets.UTF_8, null);
ClientInputFile testInputFile = new TestClientInputFile(temp, srcFile2, true, StandardCharsets.UTF_8, null);
var baseDir = createDirectory(temp.resolve("baseDir"));
var config = ConnectedAnalysisConfiguration.builder().setProjectKey("foo").setBaseDir(baseDir).addInputFiles(inputFile, testInputFile).putAllExtraProperties(props).build();
assertThat(config.toString()).isEqualTo("[\n" + " projectKey: foo\n" + " baseDir: " + baseDir.toString() + "\n" + " extraProperties: {sonar.java.libraries=foo bar}\n" + " moduleKey: null\n" + " inputFiles: [\n" + " " + srcFile1.toUri().toString() + " (UTF-8)\n" + " " + srcFile2.toUri().toString() + " (UTF-8) [test]\n" + " ]\n" + "]\n");
assertThat(config.baseDir()).isEqualTo(baseDir);
assertThat(config.inputFiles()).containsExactly(inputFile, testInputFile);
assertThat(config.projectKey()).isEqualTo("foo");
assertThat(config.extraProperties()).containsExactly(entry("sonar.java.libraries", "foo bar"));
config = ConnectedAnalysisConfiguration.builder().setBaseDir(baseDir).addInputFiles(inputFile, testInputFile).putAllExtraProperties(props).build();
assertThat(config.toString()).isEqualTo("[\n" + " baseDir: " + baseDir.toString() + "\n" + " extraProperties: {sonar.java.libraries=foo bar}\n" + " moduleKey: null\n" + " inputFiles: [\n" + " " + srcFile1.toUri().toString() + " (UTF-8)\n" + " " + srcFile2.toUri().toString() + " (UTF-8) [test]\n" + " ]\n" + "]\n");
}
use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.
the class StandaloneAnalysisConfigurationTests method testToString_and_getters.
@Test
void testToString_and_getters(@TempDir Path temp) throws Exception {
Map<String, String> props = new HashMap<>();
props.put("sonar.java.libraries", "foo bar");
final var srcFile1 = createDirectory(temp.resolve("src1"));
final var srcFile2 = createDirectory(temp.resolve("src2"));
final var srcFile3 = createDirectory(temp.resolve("src3"));
ClientInputFile inputFile = new TestClientInputFile(temp, srcFile1, false, StandardCharsets.UTF_8, null);
ClientInputFile inputFileWithLanguage = new TestClientInputFile(temp, srcFile2, false, StandardCharsets.UTF_8, Language.JAVA);
ClientInputFile testInputFile = new TestClientInputFile(temp, srcFile3, true, null, Language.PHP);
var baseDir = createDirectory(temp.resolve("baseDir"));
Collection<RuleKey> excludedRules = Arrays.asList(new RuleKey("squid", "S1135"), new RuleKey("squid", "S1181"));
Collection<RuleKey> includedRules = Arrays.asList(new RuleKey("javascript", "S2424"), new RuleKey("javascript", "S1442"));
Map<String, String> squidS5Parameters = new HashMap<>();
squidS5Parameters.put("s5param1", "s5value1");
squidS5Parameters.put("s5param2", "s5value2");
Map<String, String> squidS6Parameters = new HashMap<>();
squidS6Parameters.put("s6param1", "s6value1");
squidS6Parameters.put("s6param2", "s6value2");
Map<RuleKey, Map<String, String>> ruleParameters = new HashMap<>();
ruleParameters.put(RuleKey.parse("squid:S6"), squidS6Parameters);
var config = StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir).addInputFile(inputFile).addInputFiles(inputFileWithLanguage).addInputFiles(Arrays.asList(testInputFile)).putAllExtraProperties(props).putExtraProperty("sonar.foo", "bar").addExcludedRules(excludedRules).addExcludedRule(RuleKey.parse("squid:S1")).addExcludedRules(RuleKey.parse("squid:S2"), RuleKey.parse("squid:S3")).addIncludedRules(includedRules).addIncludedRule(RuleKey.parse("squid:I1")).addIncludedRules(RuleKey.parse("squid:I2"), RuleKey.parse("squid:I3")).addRuleParameter(RuleKey.parse("squid:S4"), "param1", "value1").addRuleParameters(RuleKey.parse("squid:S5"), squidS5Parameters).addRuleParameters(ruleParameters).build();
assertThat(config.toString()).isEqualTo("[\n" + " baseDir: " + baseDir.toString() + "\n" + " extraProperties: {sonar.java.libraries=foo bar, sonar.foo=bar}\n" + " moduleKey: null\n" + " excludedRules: [squid:S1135, squid:S1181, squid:S1, squid:S2, squid:S3]\n" + " includedRules: [javascript:S2424, javascript:S1442, squid:I1, squid:I2, squid:I3]\n" + " ruleParameters: {squid:S4={param1=value1}, squid:S5={s5param1=s5value1, s5param2=s5value2}, squid:S6={s6param1=s6value1, s6param2=s6value2}}\n" + " inputFiles: [\n" + " " + srcFile1.toUri().toString() + " (UTF-8)\n" + " " + srcFile2.toUri().toString() + " (UTF-8) [java]\n" + " " + srcFile3.toUri().toString() + " (default) [test] [php]\n" + " ]\n" + "]\n");
assertThat(config.baseDir()).isEqualTo(baseDir);
assertThat(config.inputFiles()).containsExactly(inputFile, inputFileWithLanguage, testInputFile);
assertThat(config.extraProperties()).containsExactly(entry("sonar.java.libraries", "foo bar"), entry("sonar.foo", "bar"));
assertThat(config.excludedRules()).extracting(RuleKey::toString).containsExactly("squid:S1135", "squid:S1181", "squid:S1", "squid:S2", "squid:S3");
assertThat(config.includedRules()).extracting(RuleKey::toString).containsExactly("javascript:S2424", "javascript:S1442", "squid:I1", "squid:I2", "squid:I3");
assertThat(config.ruleParameters()).containsKeys(RuleKey.parse("squid:S4"), RuleKey.parse("squid:S5"), RuleKey.parse("squid:S6"));
}
Aggregations