Skip to content

Commit

Permalink
fix: create auxclasspath file for each spotbugs task (#272)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
OdysseusLives authored May 28, 2020
1 parent ef626ad commit b8e21c9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ public class MyFoo {
}
"""
when:
BuildResult result =
GradleRunner.create()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit b8e21c9

Please sign in to comment.