diff --git a/plugin/build.gradle b/plugin/build.gradle index 0aa715d..d4e57d6 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -1,5 +1,4 @@ apply plugin: 'groovy' -apply plugin: 'java-gradle-plugin' apply from: rootProject.file('gradle/publish.gradle') sourceSets { @@ -29,5 +28,4 @@ dependencies { testCompile libraries.findbugs.annotations testCompile libraries.mockito - testRuntime files(pluginUnderTestMetadata) } diff --git a/plugin/src/test/groovy/com/novoda/test/DeployRulesTestRule.groovy b/plugin/src/test/groovy/com/novoda/test/DeployRulesTestRule.groovy index cbbf1f6..fc95bc7 100644 --- a/plugin/src/test/groovy/com/novoda/test/DeployRulesTestRule.groovy +++ b/plugin/src/test/groovy/com/novoda/test/DeployRulesTestRule.groovy @@ -24,13 +24,12 @@ class DeployRulesTestRule implements TestRule { void evaluate() throws Throwable { cleanRepo() File projectDir = createProjectDir("${System.currentTimeMillis()}") - createBuildScript(projectDir) + createBuildScripts(projectDir) GradleRunner.create() .withProjectDir(projectDir) .withDebug(true) - .forwardStdOutput(new OutputStreamWriter(System.out)) - .forwardStdError(new OutputStreamWriter(System.out)) .withArguments('clean', 'publish') + .forwardOutput() .build() base.evaluate() projectDir.deleteDir() @@ -46,40 +45,41 @@ class DeployRulesTestRule implements TestRule { } } - private void createBuildScript(File projectDir) { + private void createBuildScripts(File projectDir) { new File(projectDir, 'build.gradle').text = """ -buildscript { - repositories { - jcenter() - } -} - -version='$version' - -apply plugin: 'java' - -sourceSets { - main { - resources { - srcDirs = ${resourceDirs.collect { "'$it.path'" }} - } - } -} - -apply plugin: 'maven-publish' - -publishing { - repositories { - maven { url '${repoDir}' } - } - publications { - mavenJava(MavenPublication) { - groupId '$groupId' - artifactId '$artifactId' - from components.java - } - } -}""" + buildscript { + repositories { + jcenter() + } + } + + version='$version' + + apply plugin: 'java' + + sourceSets { + main { + resources { + srcDirs = ${resourceDirs.collect { "'$it.path'" }} + } + } + } + + apply plugin: 'maven-publish' + + publishing { + repositories { + maven { url '${repoDir}' } + } + publications { + mavenJava(MavenPublication) { + groupId '$groupId' + artifactId '$artifactId' + from components.java + } + } + }""".stripIndent() + new File(projectDir, 'settings.gradle').text = '''''' } private static File createProjectDir(String path) { diff --git a/plugin/src/test/groovy/com/novoda/test/Fixtures.groovy b/plugin/src/test/groovy/com/novoda/test/Fixtures.groovy index 9c17b14..9c29084 100644 --- a/plugin/src/test/groovy/com/novoda/test/Fixtures.groovy +++ b/plugin/src/test/groovy/com/novoda/test/Fixtures.groovy @@ -4,7 +4,7 @@ import com.google.common.io.Resources public final class Fixtures { private static final Exception NO_INSTANCE_ALLOWED = new UnsupportedOperationException("No instance allowed"); - private static final File ROOT_DIR = new File(Resources.getResource('.').file).parentFile.parentFile.parentFile.parentFile + public static final File ROOT_DIR = new File(Resources.getResource('.').file).parentFile.parentFile.parentFile.parentFile private static final File FIXTURES_DIR = new File(ROOT_DIR, 'src/test/fixtures') private static final File SOURCES_DIR = new File(FIXTURES_DIR, 'sources') private static final File REPORTS_DIR = new File(FIXTURES_DIR, 'reports') diff --git a/plugin/src/test/groovy/com/novoda/test/TestAndroidKotlinProject.groovy b/plugin/src/test/groovy/com/novoda/test/TestAndroidKotlinProject.groovy index 03089c3..2811250 100644 --- a/plugin/src/test/groovy/com/novoda/test/TestAndroidKotlinProject.groovy +++ b/plugin/src/test/groovy/com/novoda/test/TestAndroidKotlinProject.groovy @@ -11,11 +11,11 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.1.4' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.20' + classpath 'com.novoda:gradle-static-analysis-plugin:local' } } plugins { ${formatPlugins(project)} - id 'com.novoda.static-analysis' } repositories { google() @@ -23,6 +23,7 @@ repositories { } apply plugin: 'com.android.library' apply plugin: 'kotlin-android' +apply plugin: 'com.novoda.static-analysis' android { compileSdkVersion 27 diff --git a/plugin/src/test/groovy/com/novoda/test/TestAndroidProject.groovy b/plugin/src/test/groovy/com/novoda/test/TestAndroidProject.groovy index b4dc3ac..e99b83a 100644 --- a/plugin/src/test/groovy/com/novoda/test/TestAndroidProject.groovy +++ b/plugin/src/test/groovy/com/novoda/test/TestAndroidProject.groovy @@ -10,17 +10,18 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' + classpath 'com.novoda:gradle-static-analysis-plugin:local' } } plugins { ${formatPlugins(project)} - id 'com.novoda.static-analysis' } repositories { google() jcenter() } apply plugin: 'com.android.library' +apply plugin: 'com.novoda.static-analysis' android { compileSdkVersion 27 diff --git a/plugin/src/test/groovy/com/novoda/test/TestJavaProject.groovy b/plugin/src/test/groovy/com/novoda/test/TestJavaProject.groovy index ac01e0a..156123a 100644 --- a/plugin/src/test/groovy/com/novoda/test/TestJavaProject.groovy +++ b/plugin/src/test/groovy/com/novoda/test/TestJavaProject.groovy @@ -4,14 +4,20 @@ final class TestJavaProject extends TestProject { private static final Closure TEMPLATE = { TestProject project -> """ +buildscript { + dependencies { + classpath 'com.novoda:gradle-static-analysis-plugin:local' + } +} plugins { ${formatPlugins(project)} - id 'com.novoda.static-analysis' } repositories { jcenter() } apply plugin: 'java' +apply plugin: 'com.novoda.static-analysis' + sourceSets { ${formatSourceSets(project)} } diff --git a/plugin/src/test/groovy/com/novoda/test/TestKotlinProject.groovy b/plugin/src/test/groovy/com/novoda/test/TestKotlinProject.groovy index 2debb58..d3de1a6 100644 --- a/plugin/src/test/groovy/com/novoda/test/TestKotlinProject.groovy +++ b/plugin/src/test/groovy/com/novoda/test/TestKotlinProject.groovy @@ -9,16 +9,17 @@ buildscript { jcenter() } dependencies { + classpath 'com.novoda:gradle-static-analysis-plugin:local' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.10' } } plugins { ${formatPlugins(project)} - id 'com.novoda.static-analysis' } apply plugin: 'kotlin' +apply plugin: 'com.novoda.static-analysis' repositories { jcenter() diff --git a/plugin/src/test/groovy/com/novoda/test/TestProject.groovy b/plugin/src/test/groovy/com/novoda/test/TestProject.groovy index 5bc0fb7..0210b32 100644 --- a/plugin/src/test/groovy/com/novoda/test/TestProject.groovy +++ b/plugin/src/test/groovy/com/novoda/test/TestProject.groovy @@ -9,16 +9,25 @@ import javax.annotation.Nullable abstract class TestProject { private static final Closure EXTENSION_TEMPLATE = { TestProject project -> """ -staticAnalysis { - ${(project.penalty ?: '').replace(' ', '')} - ${(project.toolsConfig ?: '').replace(' ', ' ')} -} -${project.additionalConfiguration} -""" + staticAnalysis { + ${(project.penalty ?: '').replace(' ', '')} + ${(project.toolsConfig ?: '').replace(' ', ' ')} + } + ${project.additionalConfiguration} + """.stripIndent() + } + private static final Closure SETTINGS_GRADLE_TEMPLATE = { String projectName -> + """ + rootProject.name = '$projectName' + includeBuild('$Fixtures.ROOT_DIR/src/test/test-plugin') { + dependencySubstitution { + substitute module('com.novoda:gradle-static-analysis-plugin') with project(':') + } + } + """ } private final File projectDir - private final GradleRunner gradleRunner private final Closure template String additionalConfiguration = '' Map> sourceSets = [main: []] @@ -29,11 +38,6 @@ ${project.additionalConfiguration} TestProject(Closure template) { this.template = template this.projectDir = createProjectDir("${System.currentTimeMillis()}") - this.gradleRunner = GradleRunner.create() - .withProjectDir(projectDir) - .withPluginClasspath() - .forwardStdOutput(new OutputStreamWriter(System.out)) - .forwardStdError(new OutputStreamWriter(System.out)) } private static File createProjectDir(String path) { @@ -44,87 +48,97 @@ ${project.additionalConfiguration} } List defaultArguments() { - Collections.emptyList() + ['--stacktrace'] } - public T withFile(File source, String path) { + T withFile(File source, String path) { write(source.text, path) return this } - public T withFile(String text, String path) { + T withFile(String text, String path) { write(text, path) return this } private void write(String text, String path) { - File file = new File(gradleRunner.projectDir, path) + File file = new File(projectDir, path) file.parentFile.mkdirs() file.text = text } - public T copyIntoSourceSet(String sourceSet, File srcDir) { + T copyIntoSourceSet(String sourceSet, File srcDir) { srcDir.listFiles().each { withFile(it, "src/${sourceSet}/java/${it.name}") } return this } - public T withSourceSet(String sourceSet, File... srcDirs) { + T withSourceSet(String sourceSet, File... srcDirs) { sourceSets[sourceSet] = srcDirs return this } - public T withPenalty(String penalty) { + T withPenalty(String penalty) { this.penalty = "penalty $penalty" return this } - public T withToolsConfig(String toolsConfig) { + T withToolsConfig(String toolsConfig) { this.toolsConfig = toolsConfig return this } - public T withAdditionalConfiguration(String additionalConfiguration) { + T withAdditionalConfiguration(String additionalConfiguration) { this.additionalConfiguration = additionalConfiguration return this } - public T withPlugin(String plugin, String version = null) { + T withPlugin(String plugin, String version = null) { this.plugins.add("id '$plugin' ${version ? "version '$version'" : ""}") return this } - public Result build(String... arguments) { + Result build(String... arguments) { BuildResult buildResult = newRunner(arguments).build() createResult(buildResult) } private GradleRunner newRunner(String... arguments) { new File(projectDir, 'build.gradle').text = template.call(this) + new File(projectDir, 'settings.gradle').text = SETTINGS_GRADLE_TEMPLATE.call(projectDir.name) List defaultArgs = defaultArguments() List args = new ArrayList<>(arguments.size() + defaultArgs.size()) args.addAll(defaultArgs) args.addAll(arguments) - gradleRunner.withArguments(args) + return GradleRunner.create() + .withProjectDir(projectDir) + .withArguments(args) + .forwardOutput() } private createResult(BuildResult buildResult) { new Result(buildResult, new File(projectDir, 'build')) } - public Result buildAndFail(String... arguments) { + Result buildAndFail(String... arguments) { BuildResult buildResult = newRunner(arguments).buildAndFail() createResult(buildResult) } - public void deleteDir() { - projectDir.deleteDir() + void deleteDir() { + recursiveDelete(projectDir) } - String projectDir() { - return projectDir + private void recursiveDelete(File file) { + File[] files = file.listFiles() + if (files != null) { + for (File each : files) { + recursiveDelete(each) + } + } + file.delete() } protected static String formatExtension(TestProject project) { @@ -135,7 +149,7 @@ ${project.additionalConfiguration} project.plugins.join('\n') } - public static class Result { + static class Result { private final BuildResult buildResult private final File buildDir @@ -161,7 +175,7 @@ ${project.additionalConfiguration} buildResult.task(taskPath).outcome } - public static class Logs { + static class Logs { final String output Logs(String output) { diff --git a/plugin/src/test/test-plugin/build.gradle b/plugin/src/test/test-plugin/build.gradle new file mode 100644 index 0000000..2d3640b --- /dev/null +++ b/plugin/src/test/test-plugin/build.gradle @@ -0,0 +1,12 @@ +apply plugin: 'groovy' + +sourceSets { + main { + groovy.srcDir(project.file('../../main/groovy')) + resources.srcDir(project.file('../../main/resources')) + } +} + +dependencies { + implementation gradleApi() +} diff --git a/plugin/src/test/test-plugin/settings.gradle b/plugin/src/test/test-plugin/settings.gradle new file mode 100644 index 0000000..e69de29