-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSonarLintTest.java
More file actions
207 lines (177 loc) · 7.17 KB
/
SonarLintTest.java
File metadata and controls
207 lines (177 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* SonarSource Scala
* Copyright (C) 2018-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonarsource.slang;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.junit4.OrchestratorRuleBuilder;
import com.sonar.orchestrator.locator.Locators;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.commons.io.FileUtils;
import org.assertj.core.groups.Tuple;
import org.jetbrains.annotations.NotNull;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl;
import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile;
import org.sonarsource.sonarlint.core.client.api.common.analysis.Issue;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration;
import org.sonarsource.sonarlint.core.client.api.standalone.StandaloneSonarLintEngine;
import org.sonarsource.sonarlint.core.commons.IssueSeverity;
import org.sonarsource.sonarlint.core.commons.Language;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
public class SonarLintTest {
@ClassRule
public static TemporaryFolder temp = new TemporaryFolder();
private static StandaloneSonarLintEngine sonarlintEngine;
private static File baseDir;
@BeforeClass
public static void prepare() throws Exception {
// Orchestrator is used only to retrieve plugin artifacts from filesystem or maven
OrchestratorRuleBuilder orchestratorBuilder = OrchestratorRule.builderEnv();
Tests.addLanguagePlugins(orchestratorBuilder);
OrchestratorRule orchestrator = orchestratorBuilder
.useDefaultAdminCredentialsForBuilds(true)
.setSonarVersion(System.getProperty(Tests.SQ_VERSION_PROPERTY, Tests.DEFAULT_SQ_VERSION))
.build();
Locators locators = orchestrator.getConfiguration().locators();
StandaloneGlobalConfiguration.Builder sonarLintConfigBuilder = StandaloneGlobalConfiguration.builder();
orchestrator.getDistribution().getPluginLocations().stream()
.filter(location -> !location.toString().contains("sonar-reset-data-plugin"))
.map(plugin -> locators.locate(plugin).toPath())
.forEach(sonarLintConfigBuilder::addPlugin);
sonarLintConfigBuilder
.setSonarLintUserHome(temp.newFolder().toPath())
.setLogOutput((formattedMessage, level) -> {
/* Don't pollute logs */
});
StandaloneGlobalConfiguration configuration = sonarLintConfigBuilder
.addEnabledLanguage(Language.SCALA)
.build();
sonarlintEngine = new StandaloneSonarLintEngineImpl(configuration);
baseDir = temp.newFolder();
}
@AfterClass
public static void stop() {
sonarlintEngine.stop();
}
@Test
public void test_scala() throws Exception {
ClientInputFile inputFile = prepareInputFile("foo.scala",
"object Code {\n"
+ " def foo_bar() = {\n" // scala:S100 (Method name)
+ " if (true) { \n" // scala:S1145 (Useless if(true))
+ " val password = \"blabla\"\n" // scala:S181 (Unused variable)
+ " } \n"
+ " }\n"
+ "}",
false, "scala");
assertIssues(analyzeWithSonarLint(inputFile),
tuple("scala:S100", 2, inputFile.getPath(), IssueSeverity.MINOR),
tuple("scala:S1145", 3, inputFile.getPath(), IssueSeverity.MAJOR),
tuple("scala:S1481", 4, inputFile.getPath(), IssueSeverity.MINOR)
);
}
@Test
public void test_scala_nosonar() throws Exception {
ClientInputFile scalaInputFile = prepareInputFile("foo.scala",
"package main"
+ "object Code {\n"
+ " def foo_bar() = { // NOSONAR\n" // skipped scala:S100 (Method name)
+ " if (true) { // NOSONAR\n" // skipped scala:S1145 (Useless if(true))
+ " val password = \"blabla\" // NOSONAR\n" // skipped scala:S181 (Unused variable)
+ " } \n"
+ " }\n"
+ "}",
false, "scala");
assertThat(analyzeWithSonarLint(scalaInputFile)).isEmpty();
}
private List<Issue> analyzeWithSonarLint(ClientInputFile inputFile) {
List<Issue> issues = new ArrayList<>();
StandaloneAnalysisConfiguration analysisConfiguration = StandaloneAnalysisConfiguration.builder()
.setBaseDir(baseDir.toPath())
.addInputFiles(Collections.singletonList(inputFile))
.build();
sonarlintEngine.analyze(analysisConfiguration, issues::add, null, null);
return issues;
}
private void assertIssues(List<Issue> issues, Tuple... expectedIssues) {
assertThat(issues)
.extracting(Issue::getRuleKey, Issue::getStartLine, issue -> issue.getInputFile().getPath(), Issue::getSeverity)
.containsExactlyInAnyOrder(expectedIssues);
}
private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest, String language) throws IOException {
File file = new File(baseDir, relativePath);
FileUtils.write(file, content, StandardCharsets.UTF_8);
return createInputFile(file.toPath(), isTest, language);
}
private ClientInputFile createInputFile(final Path path, final boolean isTest, String language) {
return new ClientInputFile() {
@Override
public URI uri() {
return path.toUri();
}
@Override
public String getPath() {
return path.toString();
}
@Override
public boolean isTest() {
return isTest;
}
@Override
public Charset getCharset() {
return StandardCharsets.UTF_8;
}
@Override
public <G> G getClientObject() {
return null;
}
@Override
public String contents() throws IOException {
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
}
@Override
public String relativePath() {
return path.toString();
}
@Override
public InputStream inputStream() throws IOException {
return Files.newInputStream(path);
}
@NotNull
@Override
public Language language() {
return Language.forKey(language).orElse(Language.APEX);
}
};
}
}