From b8e21c970dd581bcca8cb7c7df4c0ea47041944b Mon Sep 17 00:00:00 2001 From: OdysseusLives Date: Wed, 27 May 2020 17:27:52 -0700 Subject: [PATCH] fix: create auxclasspath file for each spotbugs task (#272) * fix: create auxclasspath file for each spotbugs task (#259) * fix: auxclasspath file is removed and created instead of truncated in place * fix: auxclasspath files stored in hierarchy * fix: auxclasspath files are truncated when they already exist and the task can be called repeatedly --- .../snom/StandardFunctionalTest.groovy | 77 ++++++++++++++++++- .../snom/internal/SpotBugsRunner.java | 24 ++++-- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy index 1fc3bed3..58f7e3d2 100644 --- a/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy +++ b/src/functionalTest/groovy/com/github/spotbugs/snom/StandardFunctionalTest.groovy @@ -477,7 +477,6 @@ public class MyFoo { } """ - when: BuildResult result = GradleRunner.create() @@ -491,7 +490,81 @@ public class MyFoo { then: result.task(":spotbugsMain").outcome == TaskOutcome.SUCCESS result.output.contains("Using auxclasspath file") - result.output.contains("/build/spotbugs/spotbugs-auxclasspath") + result.output.contains("/build/spotbugs/auxclasspath/spotbugsMain") + + when: + BuildResult repeatedResult = + GradleRunner.create() + .withProjectDir(rootDir) + .withArguments("spotbugsMain", '--rerun-tasks', '-s') + .withPluginClasspath() + .forwardOutput() + .withGradleVersion(version) + .build() + + then: + repeatedResult.task(":spotbugsMain").outcome == TaskOutcome.SUCCESS + } + + def "can apply plugin using useAuxclasspathFile flag in parallel"() { + given: + buildFile << """ +spotbugs { + useAuxclasspathFile = true +} +dependencies { + implementation 'com.google.guava:guava:19.0' + testImplementation 'junit:junit:4.12' +}""" + + File sourceDir = rootDir.toPath().resolve(Paths.get("src", "main", "java")).toFile() + sourceDir.mkdirs() + File sourceFile = new File(sourceDir, "MyFoo.java") + sourceFile << """ +public class MyFoo { + public static void main(String... args) { + java.util.Map items = com.google.common.collect.ImmutableMap.of("coin", 3, "glass", 4, "pencil", 1); + + items.entrySet() + .stream() + .forEach(System.out::println); + } +} +""" + + File testSourceDir = rootDir.toPath().resolve(Paths.get("src", "test", "java")).toFile() + testSourceDir.mkdirs() + File testSourceFile = new File(testSourceDir, "SimpleTest.java") + testSourceFile << """ +import org.junit.*; +import static org.junit.Assert.*; + +import java.util.*; + +public class SimpleTest { + @Test + public void testEmptyCollection() { + Collection collection = new ArrayList(); + assertTrue(collection.isEmpty()); + } +} +""" + + when: + BuildResult result = + GradleRunner.create() + .withProjectDir(rootDir) + .withArguments("spotbugsMain", "spotbugsTest", '--parallel', '--debug') + .withPluginClasspath() + .forwardOutput() + .withGradleVersion(version) + .build() + + then: + result.task(":spotbugsMain").outcome == TaskOutcome.SUCCESS + result.output.contains("Using auxclasspath file") + result.output.contains("/build/spotbugs/auxclasspath/spotbugsMain") + result.output.contains("/build/spotbugs/auxclasspath/spotbugsTest") } @Unroll diff --git a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunner.java b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunner.java index 113b3d9a..3540578d 100644 --- a/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunner.java +++ b/src/main/groovy/com/github/spotbugs/snom/internal/SpotBugsRunner.java @@ -127,14 +127,24 @@ private String createFileForAuxClasspath(SpotBugsTask task) { Paths.get( task.getProject().getBuildDir().getAbsolutePath(), "spotbugs", - "spotbugs-auxclasspath"); - Files.createDirectories(auxClasspathFile.getParent()); - Files.createFile(auxClasspathFile); - Files.write(auxClasspathFile, auxClasspath.getBytes(), StandardOpenOption.TRUNCATE_EXISTING); - return auxClasspathFile.normalize().toString(); + "auxclasspath", + task.getName()); + try { + Files.createDirectories(auxClasspathFile.getParent()); + if (!Files.exists(auxClasspathFile)) { + Files.createFile(auxClasspathFile); + } + Files.write( + auxClasspathFile, auxClasspath.getBytes(), StandardOpenOption.TRUNCATE_EXISTING); + return auxClasspathFile.normalize().toString(); + } catch (Exception e) { + throw new GradleException( + "Could not create auxiliary classpath file for SpotBugsTask at " + + auxClasspathFile.normalize().toString(), + e); + } } catch (Exception e) { - // oops - throw new GradleException("Could not create auxiliary classpath file for SpotBugsTask"); + throw new GradleException("Could not create auxiliary classpath file for SpotBugsTask", e); } }