Skip to content

Commit

Permalink
fix: SpotBugsTask should be cacheable even when no report is configur…
Browse files Browse the repository at this point in the history
…ed by user (#736)

* test: reproduce the reported issue

refs #630

Signed-off-by: Kengo TODA <skypencil@gmail.com>

* fix: treat the temporal file as task output

* chore: make the field final

* chore: stop updating abstract field in the constructor

Signed-off-by: Kengo TODA <skypencil@gmail.com>

* chore: make field to abstract method

Signed-off-by: Kengo TODA <skypencil@gmail.com>

* chore: create a new file if no file exists

Signed-off-by: Kengo TODA <skypencil@gmail.com>
  • Loading branch information
KengoTODA committed Jun 13, 2022
1 parent e838323 commit 18f0228
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,47 @@ class CacheabilityFunctionalTest extends Specification {
hashKeyLine1 == hashKeyLine2
}

def 'spotbugsMain is cacheable even when no report is configured'() {
given:
def buildDir = Files.createTempDirectory(null).toFile()
def version = System.getProperty('snom.test.functional.gradle', GradleVersion.current().version)
def buildFile = new File(buildDir, "build.gradle")

initializeBuildFile(buildDir)
buildFile.write """
|plugins {
| id 'java'
| id 'com.github.spotbugs'
|}
|
|version = 1.0
|
|repositories {
| mavenCentral()
|}
|""".stripMargin()

when:
GradleRunner.create()
.withProjectDir(buildDir)
.withArguments(':spotbugsMain', '--build-cache')
.withPluginClasspath()
.forwardOutput()
.withGradleVersion(version)
.build()
BuildResult result =
GradleRunner.create()
.withProjectDir(buildDir)
.withArguments(':spotbugsMain', '--build-cache')
.withPluginClasspath()
.forwardOutput()
.withGradleVersion(version)
.build()

then:
result.task(":spotbugsMain").outcome == TaskOutcome.UP_TO_DATE
}

private static String getHashKeyLine(BuildResult result) {
return result.output.find('Build cache key for task \':spotbugsMain\' is .*')
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/groovy/com/github/spotbugs/snom/SpotBugsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.provider.Provider;
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.SkipWhenEmpty

import org.gradle.api.Action;
Expand All @@ -51,6 +52,7 @@ import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.VerificationTask
import org.gradle.internal.enterprise.test.FileProperty
import org.gradle.jvm.toolchain.JavaLauncher
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.util.ClosureBackedAction
Expand Down Expand Up @@ -300,6 +302,13 @@ abstract class SpotBugsTask extends DefaultTask implements VerificationTask {
@Optional
abstract Property<JavaLauncher> getLauncher()

/**
* A file that lists class files and jar files to analyse.
*/
@OutputFile
@NonNull
abstract RegularFileProperty getAnalyseClassFile()

@Inject
SpotBugsTask(ObjectFactory objects, WorkerExecutor workerExecutor) {
this.workerExecutor = Objects.requireNonNull(workerExecutor);
Expand Down Expand Up @@ -403,6 +412,9 @@ abstract class SpotBugsTask extends DefaultTask implements VerificationTask {

this.enableWorkerApi = enableWorkerApi
this.enableHybridWorker = enableHybridWorker

def file = new File(project.buildDir, this.name + "-analyse-class-file.txt")
analyseClassFile.set(file)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.gradle.api.GradleException;
import org.gradle.api.Task;
import org.gradle.api.file.FileCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -123,8 +122,11 @@ protected List<String> buildArguments(SpotBugsTask task) {
args.add(task.getProjectName().get());
args.add("-release");
args.add(task.getRelease().get());

File file = task.getAnalyseClassFile().getAsFile().get();
generateFile(task.getClasses(), task.getAnalyseClassFile().getAsFile().get());
args.add("-analyzeFromFile");
args.add(generateFile(task.getClasses(), task).getAbsolutePath());
args.add(file.getAbsolutePath());

args.addAll(task.getExtraArgs().getOrElse(Collections.emptyList()));
log.debug("Arguments for SpotBugs are generated: {}", args);
Expand Down Expand Up @@ -162,14 +164,11 @@ private String createFileForAuxClasspath(SpotBugsTask task) {
}
}

private File generateFile(FileCollection files, Task task) {
private void generateFile(FileCollection files, File file) {
try {
File file = File.createTempFile("spotbugs-gradle-plugin", ".txt", task.getTemporaryDir());
Iterable<String> lines =
files.filter(File::exists).getFiles().stream().map(File::getAbsolutePath)::iterator;
Files.write(file.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.WRITE);

return file;
Files.write(file.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
} catch (IOException e) {
throw new GradleException("Fail to generate the text file to list target .class files", e);
}
Expand Down

0 comments on commit 18f0228

Please sign in to comment.