Skip to content

Commit

Permalink
Add more tests to gradle plugin, and close #320 (#337)
Browse files Browse the repository at this point in the history
* add test to find problem like #320

* close #320: iterate ClassesDirs to find all target files

* install not only spotbugs but also others

* move timing to install

* use both of central and local as repository

* update CHANGELOG

* Revert "move timing to install"

This reverts commit e997efb.

* refs #337: extract shared logic to Before method

* refs #337: add more tests

* shorten test by deleting temporal File object in field

* generate plugin classpath by gradle

this change lets us stop invoking `install` task before the `test`.

* Revert "generate plugin classpath by gradle"

This reverts commit 9952aa6.
  • Loading branch information
KengoTODA authored and iloveeclipse committed Sep 6, 2017
1 parent 3e94a3b commit f7a08d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ install:
- rm -rf eclipse
- tar xzvf deps/eclipse-SDK-4.6.3-linux-gtk-x86_64.tar.gz eclipse
- echo eclipseRoot.dir=$(pwd)/eclipse > eclipsePlugin/local.properties
# To run gradlePlugin:test, we need to install spotbugs and its dependencies into local repository
- ./gradlew install -x test

This comment has been minimized.

Copy link
@KengoTODA

KengoTODA Sep 7, 2017

Author Member

@ThrawnCA you need to install spotbugs first, then you can run test in local.

This comment has been minimized.

Copy link
@ThrawnCA

ThrawnCA Sep 8, 2017

Contributor

Hmm. Helpful to know, and thanks, but this isn't good for new starters. It should be possible to just clone the repository and run ./gradlew build or ./gradlew check. At a minimum, this should be documented in the README file.

This comment has been minimized.

Copy link
@KengoTODA

KengoTODA Sep 8, 2017

Author Member

Maybe we can add task dependency from gradlePlugin:test to spotbugs:install, I will have a try. Thanks for your notice!

This comment has been minimized.

Copy link
@KengoTODA

KengoTODA Sep 8, 2017

Author Member

Proposed PR: #369


script:
# Manually switch to JDK9 if needed
Expand Down
18 changes: 15 additions & 3 deletions gradlePlugin/src/main/java/com/github/spotbugs/SpotBugsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import java.net.URL;
import java.util.Collection;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.gradle.api.Action;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.file.ConfigurableFileTree;
import org.gradle.api.file.FileCollection;
import org.gradle.api.internal.ConventionMapping;
import org.gradle.api.internal.file.collections.SimpleFileCollection;
import org.gradle.api.plugins.quality.CodeQualityExtension;
import org.gradle.api.plugins.quality.internal.AbstractCodeQualityPlugin;
import org.gradle.api.reporting.SingleFileReport;
Expand Down Expand Up @@ -191,9 +196,16 @@ protected void configureForSourceSet(final SourceSet sourceSet, SpotBugsTask tas
taskMapping.map("classes", new Callable<FileCollection>() {
@Override
public FileCollection call() {
// the simple "classes = sourceSet.output" may lead to non-existing resources directory
// being passed to SpotBugs Ant task, resulting in an error
return project.fileTree(sourceSet.getOutput().getClassesDir()).builtBy(sourceSet.getOutput());
Set<File> files = StreamSupport.stream(sourceSet.getOutput().getClassesDirs().spliterator(), false)
.map(dir -> {
// the simple "classes = sourceSet.output" may lead to non-existing resources directory
// being passed to SpotBugs Ant task, resulting in an error
return project.fileTree(dir).builtBy(sourceSet.getOutput());
})
.map(ConfigurableFileTree::spliterator)
.flatMap(spliterator -> StreamSupport.stream(spliterator, false))
.collect(Collectors.toSet());
return new SimpleFileCollection(files);
}
});
taskMapping.map("classpath", new Callable<FileCollection>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import static org.hamcrest.CoreMatchers.notNullValue;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.BuildTask;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -19,32 +24,72 @@
public class SpotBugsPluginTest extends Assert{
@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void TestSpotBugsTasksExist() throws Exception{
String buildScript = "plugins {\n" +
" id 'java'\n" +
" id 'com.github.spotbugs'\n" +
"}\n" +
"version = 1.0\n" +
"repositories {\n" +
" mavenCentral()\n" +
"}";

File sourceDir = folder.newFolder("src", "main", "java");
File to = new File(sourceDir, "foo.java");
File from = new File("src/test/java/com/github/spotbugs/Foo.java");
Files.copy(from, to);


@Before
public void createProject() throws IOException {
String buildScript = "plugins {\n" +
" id 'java'\n" +
" id 'com.github.spotbugs'\n" +
"}\n" +
"version = 1.0\n" +
"repositories {\n" +
" mavenCentral()\n" +
" mavenLocal()\n" +
"}";
File buildFile = folder.newFile("build.gradle");
Files.write(buildScript.getBytes(StandardCharsets.UTF_8), buildFile);

File sourceDir = folder.newFolder("src", "main", "java");
File to = new File(sourceDir, "Foo.java");
File from = new File("src/test/java/com/github/spotbugs/Foo.java");
Files.copy(from, to);
}

@Test
public void TestSpotBugsTasksExist() throws Exception{
BuildResult result = GradleRunner.create().withProjectDir(folder.getRoot()).withArguments(Arrays.asList("tasks", "--all")).withPluginClasspath().build();
assertTrue(result.getOutput().contains("spotbugsMain"));
assertTrue(result.getOutput().contains("spotbugsTest"));
}

@Test
public void testSpotBugsTaskCanRun() throws Exception {
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("compileJava", "spotbugsMain"))
.withPluginClasspath().build();
Optional<BuildTask> spotbugsMain = findTask(result, ":spotbugsMain");
assertTrue(spotbugsMain.isPresent());
assertThat(spotbugsMain.get().getOutcome(), is(TaskOutcome.SUCCESS));
}

@Test
public void testSpotBugsTestTaskCanRun() throws Exception {
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("compileTestJava", "spotbugsTest"))
.withPluginClasspath().build();
Optional<BuildTask> spotbugsTest = findTask(result, ":spotbugsTest");
assertTrue(spotbugsTest.isPresent());
assertThat(spotbugsTest.get().getOutcome(), is(TaskOutcome.NO_SOURCE));
}

@Test
public void testCheckTaskDependsOnSpotBugsTasks() throws Exception {
BuildResult result = GradleRunner.create()
.withProjectDir(folder.getRoot())
.withArguments(Arrays.asList("compileJava", "compileTestJava", "check"))
.withPluginClasspath().build();
assertTrue(findTask(result, ":spotbugsMain").isPresent());
assertTrue(findTask(result, ":spotbugsTest").isPresent());
}

private Optional<BuildTask> findTask(BuildResult result, String taskName) {
return result.getTasks().stream()
.filter(task -> task.getPath().equals(taskName))
.findAny();
}

@Test
public void testLoadToolVersion() {
SpotBugsPlugin plugin = new SpotBugsPlugin();
Expand Down

1 comment on commit f7a08d9

@ThrawnCA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks my build, complaining that it can't find com.github.spotbugs:spotbugs:3.1.0-SNAPSHOT in any repository. Rolling back to the previous commit fixes it.

Please sign in to comment.