diff options
author | Travis Collins <travistx@gmail.com> | 2025-03-19 14:48:28 -0600 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2025-03-20 20:03:15 +0000 |
commit | c0dc7d251693063b14ed189b17ac7eec6c8f3f78 (patch) | |
tree | e26d96738430ca6ca9a5fc35cf249e713ce8c012 /sonar-scanner-engine | |
parent | a99d83cdb292dd010cdd6edf657f413f797f4ffb (diff) | |
download | sonarqube-c0dc7d251693063b14ed189b17ac7eec6c8f3f78.tar.gz sonarqube-c0dc7d251693063b14ed189b17ac7eec6c8f3f78.zip |
SCA-155 Exclude WorkDir
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java | 18 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java | 39 |
2 files changed, 46 insertions, 11 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java index 893a2a1649e..52db1e334f6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sca/CliService.java @@ -134,6 +134,11 @@ public class CliService { mergedExclusionPaths.addAll(configExcludedPaths); mergedExclusionPaths.addAll(scmIgnoredPaths); + String workDirExcludedPath = getWorkDirExcludedPath(module); + if (workDirExcludedPath != null) { + mergedExclusionPaths.add(workDirExcludedPath); + } + if (mergedExclusionPaths.isEmpty()) { return null; } @@ -176,6 +181,19 @@ public class CliService { .toList(); } + private static String getWorkDirExcludedPath(DefaultInputModule module) { + Path baseDir = module.getBaseDir().toAbsolutePath().normalize(); + Path workDir = module.getWorkDir().toAbsolutePath().normalize(); + + if (workDir.startsWith(baseDir)) { + // workDir is inside baseDir, so return the relative path as a glob + Path relativeWorkDir = baseDir.relativize(workDir); + return relativeWorkDir + "/**"; + } + + return null; + } + private static String toCsvString(List<String> values) throws IOException { StringWriter sw = new StringWriter(); try (CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT)) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java index 27aa9bd16e2..f8bf8d33c70 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sca/CliServiceTest.java @@ -73,10 +73,12 @@ class CliServiceTest { private CliService underTest; @BeforeEach - void setup() { + void setup() throws IOException { telemetryCache = new TelemetryCache(); + Path workDir = rootModuleDir.resolve(".scannerwork"); + Files.createDirectories(workDir); rootInputModule = new DefaultInputModule( - ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(rootModuleDir.toFile())); + ProjectDefinition.create().setBaseDir(rootModuleDir.toFile()).setWorkDir(workDir.toFile())); when(scmConfiguration.provider()).thenReturn(scmProvider); when(scmProvider.key()).thenReturn("git"); when(scmConfiguration.isExclusionDisabled()).thenReturn(false); @@ -117,7 +119,7 @@ class CliServiceTest { "--directory", rootInputModule.getBaseDir().toString(), "--exclude", - "foo,bar,baz/**,ignored.txt", + "foo,bar,baz/**,ignored.txt,.scannerwork/**", "--debug"); assertThat(logTester.logs(INFO)) @@ -149,7 +151,7 @@ class CliServiceTest { "--directory", rootInputModule.getBaseDir().toString(), "--exclude", - "ignored.txt", + "ignored.txt,.scannerwork/**", "--debug"); assertThat(logTester.logs(DEBUG)) @@ -173,7 +175,7 @@ class CliServiceTest { "--directory", rootInputModule.getBaseDir().toString(), "--exclude", - "ignored.txt", + "ignored.txt,.scannerwork/**", "--debug"); assertThat(logTester.logs(INFO)) @@ -202,7 +204,7 @@ class CliServiceTest { "--directory", rootInputModule.getBaseDir().toString(), "--exclude", - "ignored.txt", + "ignored.txt,.scannerwork/**", "--debug"); assertThat(logTester.logs(INFO)) @@ -221,7 +223,7 @@ class CliServiceTest { underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration); String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get(); - assertThat(capturedArgs).doesNotContain("--exclude"); + assertThat(capturedArgs).contains("--exclude .scannerwork/** --debug"); } @Test @@ -231,7 +233,7 @@ class CliServiceTest { underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration); String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get(); - assertThat(capturedArgs).doesNotContain("--exclude"); + assertThat(capturedArgs).contains("--exclude .scannerwork/** --debug"); } @Test @@ -241,7 +243,7 @@ class CliServiceTest { underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration); String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get(); - assertThat(capturedArgs).doesNotContain("--exclude"); + assertThat(capturedArgs).contains("--exclude .scannerwork/** --debug"); } @Test @@ -251,7 +253,7 @@ class CliServiceTest { underTest.generateManifestsZip(rootInputModule, scriptDir(), configuration); String capturedArgs = logTester.logs().stream().filter(log -> log.contains("Arguments Passed In:")).findFirst().get(); - assertThat(capturedArgs).doesNotContain("--exclude"); + assertThat(capturedArgs).contains("--exclude .scannerwork/** --debug"); } @Test @@ -261,7 +263,7 @@ class CliServiceTest { 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"); + assertThat(capturedArgs).contains("--exclude **/test/**,ignored.txt,.scannerwork/**"); } @Test @@ -310,6 +312,21 @@ class CliServiceTest { 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 --debug"); + } 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 |