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
|
/*
* SonarQube
* Copyright (C) 2009-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 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.scanner.sca;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.AfterEach;
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.MockedStatic;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.api.platform.Server;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.core.util.ProcessWrapperFactory;
import org.sonar.scanner.config.DefaultConfiguration;
import org.sonar.scanner.repository.TelemetryCache;
import org.sonar.scanner.scan.filesystem.ProjectExclusionFilters;
import org.sonar.scanner.scm.ScmConfiguration;
import org.sonar.scm.git.GitScmProvider;
import org.sonar.scm.git.JGitUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.slf4j.event.Level.DEBUG;
import static org.slf4j.event.Level.INFO;
class CliServiceTest {
private TelemetryCache telemetryCache;
private DefaultInputModule rootInputModule;
private final Server server = mock(Server.class);
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
@TempDir
Path rootModuleDir;
private final ScmConfiguration scmConfiguration = mock(ScmConfiguration.class);
private final ScmProvider scmProvider = mock(GitScmProvider.class);
ProcessWrapperFactory processWrapperFactory = mock(ProcessWrapperFactory.class, CALLS_REAL_METHODS);
private MockedStatic<JGitUtils> jGitUtilsMock;
DefaultConfiguration configuration = mock(DefaultConfiguration.class);
ProjectExclusionFilters projectExclusionFilters = mock(ProjectExclusionFilters.class);
private CliService underTest;
@BeforeEach
void setup() throws IOException {
telemetryCache = new TelemetryCache();
Path workDir = rootModuleDir.resolve(".scannerwork");
Files.createDirectories(workDir);
rootInputModule = new DefaultInputModule(
ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(workDir.toFile()));
when(scmConfiguration.provider()).thenReturn(scmProvider);
when(scmProvider.key()).thenReturn("git");
when(scmConfiguration.isExclusionDisabled()).thenReturn(false);
jGitUtilsMock = org.mockito.Mockito.mockStatic(JGitUtils.class);
jGitUtilsMock.when(() -> JGitUtils.getAllIgnoredPaths(any(Path.class))).thenReturn(List.of("ignored.txt"));
when(server.getVersion()).thenReturn("1.0.0");
logTester.setLevel(INFO);
when(projectExclusionFilters.getExclusionsConfig(InputFile.Type.MAIN)).thenReturn(new String[0]);
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[0]);
when(configuration.getStringArray(CliService.LEGACY_SCA_EXCLUSIONS_KEY)).thenReturn(new String[0]);
underTest = new CliService(processWrapperFactory, telemetryCache, System2.INSTANCE, server, scmConfiguration, projectExclusionFilters);
}
@AfterEach
void teardown() {
if (jGitUtilsMock != null) {
jGitUtilsMock.close();
}
}
@Test
void generateZip_shouldCallProcessCorrectly_andRegisterTelemetry() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
when(configuration.getProperties()).thenReturn(Map.of(CliService.SCA_EXCLUSIONS_KEY, "foo,bar,baz/**"));
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"foo", "bar", "baz/**"});
File producedZip = underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(producedZip).exists();
var expectedArguments = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--recursive",
"--exclude",
"foo,bar,baz/**,ignored.txt,.scannerwork/**");
assertThat(logTester.logs(INFO))
.contains("Arguments Passed In: " + String.join(" ", expectedArguments))
.contains("TIDELIFT_SKIP_UPDATE_CHECK=1")
.contains("TIDELIFT_ALLOW_MANIFEST_FAILURES=1")
.contains("Generated manifests zip file: " + producedZip.getName());
assertThat(telemetryCache.getAll()).containsKey("scanner.sca.execution.cli.duration").isNotNull();
assertThat(telemetryCache.getAll()).containsEntry("scanner.sca.execution.cli.success", "true");
}
@Test
void generateZip_whenDebugLogLevelAndScaDebugNotEnabled_shouldWriteDebugLogsToDebugStream() throws IOException, URISyntaxException {
logTester.setLevel(DEBUG);
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
var expectedArguments = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--recursive",
"--exclude",
"ignored.txt,.scannerwork/**",
"--debug");
assertThat(logTester.logs(INFO))
.contains("Arguments Passed In: " + String.join(" ", expectedArguments));
}
@Test
void generateZip_whenScaDebugEnabled_shouldWriteDebugLogsToInfoStream() throws IOException, URISyntaxException {
assertThat(rootModuleDir.resolve("test_file").toFile().createNewFile()).isTrue();
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
var expectedArguments = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--recursive",
"--exclude",
"ignored.txt,.scannerwork/**");
assertThat(logTester.logs(INFO))
.contains("Arguments Passed In: " + String.join(" ", expectedArguments));
}
@Test
void generateZip_shouldSendSQEnvVars() throws IOException, URISyntaxException {
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
assertThat(logTester.logs(INFO))
.contains("TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=1")
.contains("TIDELIFT_CLI_SQ_SERVER_VERSION=1.0.0");
}
@Test
void generateZip_includesIgnoredPathsFromGitProvider() throws Exception {
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
var expectedArguments = List.of(
"projects",
"save-lockfiles",
"--zip",
"--zip-filename",
rootInputModule.getWorkDir().resolve("dependency-files.zip").toString(),
"--directory",
rootInputModule.getBaseDir().toString(),
"--recursive",
"--exclude",
"ignored.txt,.scannerwork/**");
assertThat(logTester.logs(INFO))
.contains("Arguments Passed In: " + String.join(" ", expectedArguments))
.contains("TIDELIFT_SKIP_UPDATE_CHECK=1")
.contains("TIDELIFT_ALLOW_MANIFEST_FAILURES=1")
.contains("TIDELIFT_CLI_INSIDE_SCANNER_ENGINE=1")
.contains("TIDELIFT_CLI_SQ_SERVER_VERSION=1.0.0");
}
@Test
void generateZip_withNoScm_doesNotIncludeScmIgnoredPaths() throws Exception {
when(scmConfiguration.provider()).thenReturn(null);
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude .scannerwork/**");
}
@Test
void generateZip_withNonGit_doesNotIncludeScmIgnoredPaths() throws Exception {
when(scmProvider.key()).thenReturn("notgit");
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude .scannerwork/**");
}
@Test
void generateZip_withScmExclusionDisabled_doesNotIncludeScmIgnoredPaths() throws Exception {
when(scmConfiguration.isExclusionDisabled()).thenReturn(true);
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude .scannerwork/**");
}
@Test
void generateZip_withNoScmIgnores_doesNotIncludeScmIgnoredPaths() throws Exception {
jGitUtilsMock.when(() -> JGitUtils.getAllIgnoredPaths(any(Path.class))).thenReturn(List.of());
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude .scannerwork/**");
}
@Test
void generateZip_withExcludedManifests_appendsScmIgnoredPaths() throws Exception {
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"**/test/**"});
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude **/test/**,ignored.txt,.scannerwork/**");
}
@Test
void generateZip_withExcludedManifestsContainingBadCharacters_handlesTheBadCharacters() throws Exception {
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {
"**/test/**", "**/path with spaces/**", "**/path'with'quotes/**", "**/path\"with\"double\"quotes/**"});
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
String expectedExcludeFlag = """
--exclude **/test/**,**/path with spaces/**,**/path'with'quotes/**,"**/path""with""double""quotes/**",ignored.txt
""".strip();
if (SystemUtils.IS_OS_WINDOWS) {
expectedExcludeFlag = """
--exclude "**/test/**,**/path with spaces/**,**/path'with'quotes/**,"**/path""with""double""quotes/**",ignored.txt
""".strip();
}
assertThat(capturedArgs).contains(expectedExcludeFlag);
}
@Test
void generateZip_withExcludedManifestsContainingDupes_dedupes() throws Exception {
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"**/test1/**", "**/test2/**", "**/test1/**"});
when(configuration.getStringArray(CliService.LEGACY_SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"**/test1/**", "**/test3/**"});
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude **/test1/**,**/test2/**,**/test3/**,ignored.txt,.scannerwork/**");
}
@Test
void generateZip_withExcludedManifestsAndSonarExcludesContainingDupes_mergesAndDedupes() throws Exception {
when(projectExclusionFilters.getExclusionsConfig(InputFile.Type.MAIN)).thenReturn(new String[] {"**/test1/**", "**/test4/**"});
when(configuration.getStringArray(CliService.SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"**/test1/**", "**/test2/**", "**/test1/**"});
when(configuration.getStringArray(CliService.LEGACY_SCA_EXCLUSIONS_KEY)).thenReturn(new String[] {"**/test1/**", "**/test3/**"});
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude **/test1/**,**/test4/**,**/test2/**,**/test3/**,ignored.txt,.scannerwork/**");
}
@Test
void generateZip_withScmIgnoresContainingBadCharacters_handlesTheBadCharacters() throws Exception {
jGitUtilsMock.when(() -> JGitUtils.getAllIgnoredPaths(any(Path.class)))
.thenReturn(List.of("**/test/**", "**/path with spaces/**", "**/path'with'quotes/**", "**/path\"with\"double\"quotes/**"));
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
String expectedExcludeFlag = """
--exclude **/test/**,**/path with spaces/**,**/path'with'quotes/**,"**/path""with""double""quotes/**"
""".strip();
if (SystemUtils.IS_OS_WINDOWS) {
expectedExcludeFlag = """
--exclude "**/test/**,**/path with spaces/**,**/path'with'quotes/**,"**/path""with""double""quotes/**"
""".strip();
}
assertThat(capturedArgs).contains(expectedExcludeFlag);
}
@Test
void generateZip_withIgnoredDirectories_GlobifiesDirectories() throws Exception {
String ignoredDirectory = "directory1";
Files.createDirectories(rootModuleDir.resolve(ignoredDirectory));
String ignoredFile = "directory2/file.txt";
Path ignoredFilePath = rootModuleDir.resolve(ignoredFile);
Files.createDirectories(ignoredFilePath.getParent());
Files.createFile(ignoredFilePath);
jGitUtilsMock.when(() -> JGitUtils.getAllIgnoredPaths(any(Path.class))).thenReturn(List.of(ignoredDirectory, ignoredFile));
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
assertThat(capturedArgs).contains("--exclude directory1/**,directory2/file.txt");
}
@Test
void generateZip_withExternalWorkDir_DoesNotExcludeWorkingDir() throws URISyntaxException, IOException {
Path externalWorkDir = Files.createTempDirectory("externalWorkDir");
try {
rootInputModule = new DefaultInputModule(ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(externalWorkDir.toFile()));
underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration);
String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get();
// externalWorkDir is not present in the exclude flag
assertThat(capturedArgs).contains("--exclude ignored.txt");
} finally {
externalWorkDir.toFile().delete();
}
}
private URL scriptUrl() {
// There is a custom test Bash script available in src/test/resources/org/sonar/scanner/sca that
// will serve as our "CLI". This script will output some messages about what arguments were passed
// to it and will try to generate a zip file in the location the process specifies. This allows us
// to simulate a real CLI call without needing an OS specific CLI executable to run on a real project.
URL scriptUrl = CliServiceTest.class.getResource(SystemUtils.IS_OS_WINDOWS ? "echo_args.bat" : "echo_args.sh");
assertThat(scriptUrl).isNotNull();
return scriptUrl;
}
private File scriptDir() throws URISyntaxException {
return new File(scriptUrl().toURI());
}
}
|