Skip to content

Commit 9810c1c

Browse files
SONARPY-155 Add test verifying integration with SonarLint (SonarSource#49)
1 parent f4c7951 commit 9810c1c

4 files changed

Lines changed: 147 additions & 1 deletion

File tree

its/plugin/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
<groupId>org.easytesting</groupId>
3535
<artifactId>fest-assert</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.sonarsource.sonarlint.core</groupId>
39+
<artifactId>sonarlint-core</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.assertj</groupId>
43+
<artifactId>assertj-core</artifactId>
44+
</dependency>
3745
</dependencies>
3846

3947
<build>
@@ -44,6 +52,7 @@
4452
<configuration>
4553
<includes>
4654
<include>com/sonar/python/it/plugin/Tests.java</include>
55+
<include>com/sonar/python/it/plugin/SonarLintTest.java</include>
4756
</includes>
4857
</configuration>
4958
</plugin>
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2012-2016 SonarSource SA
4+
* mailto:contact AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package com.sonar.python.it.plugin;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
import java.nio.charset.Charset;
25+
import java.nio.charset.StandardCharsets;
26+
import java.nio.file.Path;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import org.apache.commons.io.FileUtils;
32+
import org.junit.AfterClass;
33+
import org.junit.BeforeClass;
34+
import org.junit.ClassRule;
35+
import org.junit.Test;
36+
import org.junit.rules.TemporaryFolder;
37+
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl;
38+
import org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile;
39+
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
40+
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration;
41+
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration;
42+
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine;
43+
44+
import static org.assertj.core.api.Assertions.assertThat;
45+
import static org.assertj.core.api.Assertions.tuple;
46+
47+
public class SonarLintTest {
48+
49+
@ClassRule
50+
public static TemporaryFolder temp = new TemporaryFolder();
51+
52+
private static StandaloneSonarLintEngine sonarlintEngine;
53+
54+
private static File baseDir;
55+
56+
@BeforeClass
57+
public static void prepare() throws Exception {
58+
StandaloneGlobalConfiguration sonarLintConfig = StandaloneGlobalConfiguration.builder()
59+
.addPlugin(Tests.PLUGIN_LOCATION.getFile().toURI().toURL())
60+
.setSonarLintUserHome(temp.newFolder().toPath())
61+
.setLogOutput((formattedMessage, level) -> {
62+
/* Don't pollute logs */ })
63+
.build();
64+
sonarlintEngine = new StandaloneSonarLintEngineImpl(sonarLintConfig);
65+
baseDir = temp.newFolder();
66+
}
67+
68+
@AfterClass
69+
public static void stop() {
70+
sonarlintEngine.stop();
71+
}
72+
73+
@Test
74+
public void should_raise_issues() throws IOException {
75+
ClientInputFile inputFile = prepareInputFile("foo.py",
76+
"def fooBar():\n"
77+
+ " `1` \n",
78+
false);
79+
80+
List<Issue> issues = new ArrayList<>();
81+
sonarlintEngine.analyze(
82+
new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), new HashMap<>()),
83+
issues::add);
84+
85+
assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path", "severity").containsOnly(
86+
tuple("python:BackticksUsage", 2, inputFile.getPath(), "MAJOR"),
87+
tuple("python:S1542", 1, inputFile.getPath(), "MAJOR"));
88+
}
89+
90+
private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest) throws IOException {
91+
File file = new File(baseDir, relativePath);
92+
FileUtils.write(file, content, StandardCharsets.UTF_8);
93+
return createInputFile(file.toPath(), isTest);
94+
}
95+
96+
private ClientInputFile createInputFile(final Path path, final boolean isTest) {
97+
return new ClientInputFile() {
98+
99+
@Override
100+
public Path getPath() {
101+
return path;
102+
}
103+
104+
@Override
105+
public boolean isTest() {
106+
return isTest;
107+
}
108+
109+
@Override
110+
public Charset getCharset() {
111+
return StandardCharsets.UTF_8;
112+
}
113+
114+
@Override
115+
public <G> G getClientObject() {
116+
return null;
117+
}
118+
119+
};
120+
}
121+
}

its/plugin/src/test/java/com/sonar/python/it/plugin/Tests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444
})
4545
public class Tests {
4646

47+
public static final FileLocation PLUGIN_LOCATION = FileLocation.byWildcardMavenFilename(new File("../../sonar-python-plugin/target"), "sonar-python-plugin-*.jar");
48+
4749
@ClassRule
4850
public static Orchestrator ORCHESTRATOR = Orchestrator.builderEnv()
49-
.addPlugin(FileLocation.byWildcardMavenFilename(new File("../../sonar-python-plugin/target"), "sonar-python-plugin-*.jar"))
51+
.addPlugin(PLUGIN_LOCATION)
5052
.restoreProfileAtStartup(FileLocation.of("profiles/no_rule.xml"))
5153
.restoreProfileAtStartup(FileLocation.of("profiles/pylint.xml"))
5254
.restoreProfileAtStartup(FileLocation.of("profiles/nosonar.xml"))

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@
7777
<commons.io.version>2.5</commons.io.version>
7878
<commons.lang.version>2.6</commons.lang.version>
7979
<fest-assert.version>1.4</fest-assert.version>
80+
<assertj-core.version>3.5.2</assertj-core.version>
8081
<junit.version>4.12</junit.version>
8182
<logback.version>1.1.7</logback.version>
8283
<maven.project.version>2.2.1</maven.project.version>
8384
<mockito.version>1.10.19</mockito.version>
8485
<slf4j.version>1.7.21</slf4j.version>
8586
<sonar.version>5.6</sonar.version>
8687
<sonar.orchestrator.version>3.12</sonar.orchestrator.version>
88+
<sonarlint-core.version>2.4.1</sonarlint-core.version>
8789
<sslr.version>1.21</sslr.version>
8890
<sslr.squid.bridge.version>2.6.1</sslr.squid.bridge.version>
8991
</properties>
@@ -189,6 +191,18 @@
189191
<version>${maven.project.version}</version>
190192
<scope>test</scope>
191193
</dependency>
194+
<dependency>
195+
<groupId>org.assertj</groupId>
196+
<artifactId>assertj-core</artifactId>
197+
<version>${assertj-core.version}</version>
198+
<scope>test</scope>
199+
</dependency>
200+
<dependency>
201+
<groupId>org.sonarsource.sonarlint.core</groupId>
202+
<artifactId>sonarlint-core</artifactId>
203+
<version>${sonarlint-core.version}</version>
204+
<scope>test</scope>
205+
</dependency>
192206

193207
<!-- provided dependencies -->
194208
<dependency>

0 commit comments

Comments
 (0)