forked from SonarSource/SonarJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CssRuleSensorTest.java
412 lines (362 loc) · 15.1 KB
/
CssRuleSensorTest.java
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2024 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 GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* 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 GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.javascript.bridge;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.event.Level;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.batch.rule.internal.ActiveRulesBuilder;
import org.sonar.api.batch.rule.internal.NewActiveRule;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.impl.utils.DefaultTempFolder;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.TempFolder;
import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.css.CssLanguage;
import org.sonar.plugins.javascript.bridge.BridgeServer.AnalysisResponse;
import org.sonar.plugins.javascript.bridge.BridgeServer.CssAnalysisRequest;
import org.sonar.plugins.javascript.nodejs.NodeCommandException;
class CssRuleSensorTest {
@RegisterExtension
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
@Mock
private BridgeServer bridgeServerMock;
@Mock
private FileLinesContextFactory fileLinesContextFactory;
@TempDir
Path baseDir;
@TempDir
File tempDir;
@TempDir
Path workDir;
TempFolder tempFolder;
private SensorContextTester context;
private static final CheckFactory CHECK_FACTORY = new CheckFactory(
activeRules("S4647", "S4656", "S4658")
);
private CssRuleSensor sensor;
@BeforeEach
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
when(bridgeServerMock.isAlive()).thenReturn(true);
when(bridgeServerMock.analyzeCss(any()))
.thenReturn(
response(
"{ issues: [{\"line\":1,\"ruleId\":\"block-no-empty\",\"message\":\"Unexpected empty block\"}]}"
)
);
when(bridgeServerMock.getCommandInfo()).thenReturn("bridgeServerMock command info");
context = SensorContextTester.create(baseDir);
context.fileSystem().setWorkDir(workDir);
tempFolder = new DefaultTempFolder(tempDir, true);
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarLint(Version.create(8, 9));
sensor =
new CssRuleSensor(
sonarRuntime,
bridgeServerMock,
new AnalysisWarningsWrapper(),
CHECK_FACTORY
);
}
static ActiveRules activeRules(String... rules) {
var activeRulesBuilder = new ActiveRulesBuilder();
for (String rule : rules) {
var activeRule = new NewActiveRule.Builder().setRuleKey(RuleKey.of("css", rule)).build();
activeRulesBuilder.addRule(activeRule);
}
return activeRulesBuilder.build();
}
@Test
void test_descriptor() {
DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
sensor.describe(sensorDescriptor);
assertThat(sensorDescriptor.name()).isEqualTo("CSS Rules");
assertThat(sensorDescriptor.languages()).isEmpty();
assertThat(sensorDescriptor.configurationPredicate()).isNull();
assertThat(sensorDescriptor.ruleRepositories()).containsOnly("css");
// true even for SonarLint runtime (see setUp) as this sensor is hardcoded by name
assertTrue(sensorDescriptor.isProcessesFilesIndependently());
}
@Test
void test_descriptor_sonarlint() {
var sonarlintDescriptor =
new org.sonarsource.sonarlint.core.analysis.sonarapi.DefaultSensorDescriptor();
// should not throw as 'processesFilesIndependently' is not executed for SonarLint
sensor.describe(sonarlintDescriptor);
assertThat(sonarlintDescriptor.name()).isEqualTo("CSS Rules");
}
@Test
void test_descriptor_sonarqube_9_3() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(
Version.create(9, 3),
SonarQubeSide.SCANNER,
SonarEdition.COMMUNITY
);
sensor =
new CssRuleSensor(
sonarRuntime,
bridgeServerMock,
new AnalysisWarningsWrapper(),
CHECK_FACTORY
);
DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
sensor.describe(sensorDescriptor);
assertThat(sensorDescriptor.name()).isEqualTo("CSS Rules");
assertTrue(sensorDescriptor.isProcessesFilesIndependently());
}
@Test
void test_descriptor_sonarqube_9_2() {
SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(
Version.create(9, 2),
SonarQubeSide.SCANNER,
SonarEdition.COMMUNITY
);
sensor =
new CssRuleSensor(
sonarRuntime,
bridgeServerMock,
new AnalysisWarningsWrapper(),
CHECK_FACTORY
);
DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor();
sensor.describe(sensorDescriptor);
assertThat(sensorDescriptor.name()).isEqualTo("CSS Rules");
// true even for v9.2 as this sensor is hardcoded by name
assertTrue(sensorDescriptor.isProcessesFilesIndependently());
}
@Test
void test_execute() throws IOException {
addInputFile("file.css");
sensor.execute(context);
assertThat(context.allIssues()).hasSize(1);
assertThat(context.allIssues())
.extracting("primaryLocation.message")
.containsOnly("Unexpected empty block");
assertThat(String.join("\n", logTester.logs(LoggerLevel.DEBUG)))
.matches("(?s).*Analyzing file: \\S*file\\.css.*")
.matches("(?s).*Found 1 issue\\(s\\).*");
}
@Test
void should_trim_rule_key_from_message() throws IOException {
AnalysisResponse responseIssues = response(
"{ issues: [{\"line\":2,\"ruleId\":\"color-no-invalid-hex\",\"message\":\"some message (color-no-invalid-hex)\"}]}"
);
when(bridgeServerMock.analyzeCss(any())).thenReturn(responseIssues);
addInputFile("file-with-rule-id-message.css");
sensor.execute(context);
assertThat(context.allIssues()).hasSize(1);
assertThat(context.allIssues())
.extracting("primaryLocation.message")
.containsOnly("some message");
assertThat(String.join("\n", logTester.logs(LoggerLevel.DEBUG)))
.matches("(?s).*Analyzing file: \\S*file-with-rule-id-message\\.css.*")
.matches("(?s).*Found 1 issue\\(s\\).*");
}
@Test
void test_non_css_files() {
var fileCss = addInputFile("file.css");
var fileVue = addInputFile("file.vue");
var fileHtml = addInputFile("file.html");
var fileHtm = addInputFile("file.htm");
var fileXhtml = addInputFile("file.xhtml");
addInputFile("file.js");
addInputFile("file.php");
addInputFile("file.cshtml");
addInputFile("file.vbhtml");
addInputFile("file.aspx");
addInputFile("file.ascx");
addInputFile("file.rhtml");
addInputFile("file.erb");
addInputFile("file.shtm");
addInputFile("file.shtml");
addInputFile("file.cmp");
addInputFile("file.twig");
sensor.execute(context);
assertThat(context.allIssues()).hasSize(5);
assertThat(context.allIssues())
.extracting("primaryLocation.component")
.containsOnly(fileCss, fileVue, fileHtml, fileHtm, fileXhtml);
}
@Test
void test_no_file_to_analyze() {
sensor.execute(context);
assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
assertThat(logTester.logs(LoggerLevel.INFO))
.contains(
"No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped."
);
}
@Test
void failed_server_should_log_error_with_css() throws IOException {
doThrow(new NodeCommandException("Exception Message"))
.when(bridgeServerMock)
.startServerLazily(any());
addInputFile("file.css");
assertThatThrownBy(() -> sensor.execute(context))
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Error while running Node.js. A supported version of Node.js is required for running the analysis of CSS files. Please make sure a supported version of Node.js is available in the PATH. Alternatively, you can exclude CSS files from your analysis using the 'sonar.exclusions' configuration property. See the docs for configuring the analysis environment: https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/languages/javascript-typescript-css/"
);
assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Exception Message");
}
@Test
void should_not_analyze_files_with_not_file_uri() throws URISyntaxException, IOException {
InputFile httpFile = mock(InputFile.class);
when(httpFile.filename()).thenReturn("file.css");
when(httpFile.uri()).thenReturn(new URI("http://lost-on-earth.com/file.css"));
sensor.analyzeFile(httpFile, context, Collections.emptyList());
assertThat(String.join("\n", logTester.logs(LoggerLevel.DEBUG)))
.matches("(?s).*Skipping \\S*file.css as it has not 'file' scheme.*")
.doesNotMatch("(?s).*\nAnalyzing \\S*file.css.*");
}
@Test
void should_stop_execution_when_sensor_context_is_cancelled() {
addInputFile("file.css");
context.setCancelled(true);
sensor.execute(context);
assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.INFO))
.contains(
"org.sonar.plugins.javascript.CancellationException: Analysis interrupted because the SensorContext is in cancelled state"
);
}
@Test
void test_syntax_error() throws IOException {
AnalysisResponse responseIssues = response(
"{ issues: [{\"line\":2,\"ruleId\":\"CssSyntaxError\",\"message\":\"Missed semicolon (CssSyntaxError)\"}]}"
);
when(bridgeServerMock.analyzeCss(any())).thenReturn(responseIssues);
InputFile inputFile = addInputFile("syntax-error.css");
InputFile inputFileNotCss = addInputFile("syntax-error.html");
sensor.execute(context);
assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.WARN))
.contains("Failed to parse file " + inputFile.uri() + ", line 2, Missed semicolon");
assertThat(logTester.logs(LoggerLevel.DEBUG))
.contains("Failed to parse file " + inputFileNotCss.uri() + ", line 2, Missed semicolon");
}
@Test
void test_unknown_rule() throws IOException {
AnalysisResponse responseIssues = response(
"{ issues: [{\"line\":2,\"ruleId\":\"unknown-rule-key\",\"message\":\"Some message\"}]}"
);
when(bridgeServerMock.analyzeCss(any())).thenReturn(responseIssues);
addInputFile("unknown-rule.css");
sensor.execute(context);
assertThat(context.allIssues()).isEmpty();
assertThat(logTester.logs(LoggerLevel.ERROR))
.contains("Unknown stylelint rule or rule not enabled: 'unknown-rule-key'");
}
@Test
void should_not_send_file_content_if_encoding_is_utf8_and_context_is_not_sonarlint()
throws IOException {
String filePath = "copy-file-content-into-issue-message.css";
DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", filePath)
.setLanguage(CssLanguage.KEY)
.setCharset(StandardCharsets.UTF_8)
.setContents("css content")
.build();
context.fileSystem().add(inputFile);
sensor.execute(context);
ArgumentCaptor<CssAnalysisRequest> capturedRequest = ArgumentCaptor.forClass(
CssAnalysisRequest.class
);
verify(bridgeServerMock).analyzeCss(capturedRequest.capture());
assertThat(capturedRequest.getValue().fileContent).isNull();
}
@Test
void should_send_file_content_if_encoding_is_not_utf8() throws IOException {
String filePath = "copy-file-content-into-issue-message.css";
DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", filePath)
.setLanguage(CssLanguage.KEY)
.setCharset(StandardCharsets.ISO_8859_1)
.setContents("css content")
.build();
context.fileSystem().add(inputFile);
sensor.execute(context);
ArgumentCaptor<CssAnalysisRequest> capturedRequest = ArgumentCaptor.forClass(
CssAnalysisRequest.class
);
verify(bridgeServerMock).analyzeCss(capturedRequest.capture());
assertThat(capturedRequest.getValue().fileContent).isEqualTo("css content");
}
@Test
void should_send_file_content_if_context_is_sonarlint() throws IOException {
context.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(7, 9)));
addInputFile("file.css");
sensor.execute(context);
ArgumentCaptor<CssAnalysisRequest> capturedRequest = ArgumentCaptor.forClass(
CssAnalysisRequest.class
);
verify(bridgeServerMock).analyzeCss(capturedRequest.capture());
assertThat(capturedRequest.getValue().fileContent).isNotNull();
}
private DefaultInputFile addInputFile(String relativePath) {
String extension = relativePath.split("\\.")[1];
String language = extension.equals("vue") ? "js" : extension;
DefaultInputFile inputFile = new TestInputFileBuilder("moduleKey", relativePath)
.setLanguage(language)
.setCharset(StandardCharsets.UTF_8)
.setContents("some css content\n on 2 lines")
.build();
context.fileSystem().add(inputFile);
return inputFile;
}
private AnalysisResponse response(String json) {
return new Gson().fromJson(json, AnalysisResponse.class);
}
}