From 1f3c126367a0dd3c947596747e35c5b673fe7393 Mon Sep 17 00:00:00 2001 From: Jared Burrows Date: Wed, 20 Apr 2022 10:58:16 -0400 Subject: [PATCH] add more tests (#195) --- .../com/jaredsburrows/license/projectJava.kt | 19 +----- .../license/LicensePluginAndroidSpec.groovy | 68 +++++++++---------- .../license/LicensePluginJavaSpec.groovy | 22 +++--- .../license/LicensePluginSpec.groovy | 58 ++++++++++++++-- .../internal/report/HtmlReportSpec.groovy | 22 +++--- src/test/groovy/test/TestUtils.groovy | 8 +++ 6 files changed, 117 insertions(+), 80 deletions(-) diff --git a/src/main/kotlin/com/jaredsburrows/license/projectJava.kt b/src/main/kotlin/com/jaredsburrows/license/projectJava.kt index eda80a45..b73c83e4 100644 --- a/src/main/kotlin/com/jaredsburrows/license/projectJava.kt +++ b/src/main/kotlin/com/jaredsburrows/license/projectJava.kt @@ -6,18 +6,10 @@ import org.gradle.api.Project internal fun Project.isJavaProject(): Boolean { return hasPlugin( listOf( - // ApplicationPlugin - "application", - // GroovyPlugin - "groovy", // JavaPlugin "java", - // JavaGradlePluginPlugin - "java-gradle-plugin", - // JavaLibraryPlugin - "java-library", - // ScalaPlugin - "scala", + // JavaPlatformPlugin + "java-platform" ) ) } @@ -25,12 +17,7 @@ internal fun Project.isJavaProject(): Boolean { /** * Configure for Java projects. * - * ApplicationPlugin - "application" - also applies JavaPlugin - * GroovyPlugin - "groovy" - also applies JavaPlugin - * JavaPlugin - "java" - also applies JavaBasePlugin - * JavaGradlePluginPlugin - "java-gradle-plugin" - also applies JavaPlugin - * JavaLibraryPlugin - "java-library" - also applies JavaPlugin - * ScalaPlugin - "scala" - also applies JavaPlugin + * All of these plugins will apply the JavaPlugin(relies on JavaBasePlugin) or JavaPlatformPlugin. */ internal fun Project.configureJavaProject() { tasks.register("licenseReport", LicenseReportTask::class.java) diff --git a/src/test/groovy/com/jaredsburrows/license/LicensePluginAndroidSpec.groovy b/src/test/groovy/com/jaredsburrows/license/LicensePluginAndroidSpec.groovy index 56343678..d01405fd 100644 --- a/src/test/groovy/com/jaredsburrows/license/LicensePluginAndroidSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/LicensePluginAndroidSpec.groovy @@ -68,10 +68,7 @@ final class LicensePluginAndroidSpec extends Specification { """ - + Open source licenses @@ -995,7 +992,8 @@ final class LicensePluginAndroidSpec extends Specification {
Some license
-                  http://website.tld/
+ http://website.tld/ +

@@ -1200,24 +1198,25 @@ final class LicensePluginAndroidSpec extends Specification { def expectedHtml = """ - + Open source licenses - - -

Notice for packages:

- + """ def expectedJson = @@ -1241,7 +1240,6 @@ final class LicensePluginAndroidSpec extends Specification { ] """ - then: result.task(":${taskName}").outcome == SUCCESS result.output.find("Wrote CSV report to .*${reportFolder}/${taskName}.csv.") @@ -1300,24 +1298,25 @@ final class LicensePluginAndroidSpec extends Specification { def expectedHtml = """ - + Open source licenses - - -

Notice for packages:

- + """ def expectedJson = @@ -1341,7 +1340,6 @@ final class LicensePluginAndroidSpec extends Specification { ] """ - then: result.task(":${taskName}").outcome == SUCCESS result.output.find("Wrote CSV report to .*${reportFolder}/${taskName}.csv.") diff --git a/src/test/groovy/com/jaredsburrows/license/LicensePluginJavaSpec.groovy b/src/test/groovy/com/jaredsburrows/license/LicensePluginJavaSpec.groovy index 243b34ad..a4160269 100644 --- a/src/test/groovy/com/jaredsburrows/license/LicensePluginJavaSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/LicensePluginJavaSpec.groovy @@ -40,10 +40,7 @@ final class LicensePluginJavaSpec extends Specification { """ - + Open source licenses @@ -266,10 +263,7 @@ final class LicensePluginJavaSpec extends Specification { """ - + Open source licenses @@ -333,7 +327,8 @@ final class LicensePluginJavaSpec extends Specification {
Some license
-                  http://website.tld/
+ http://website.tld/ +

@@ -414,10 +409,12 @@ final class LicensePluginJavaSpec extends Specification {
Some license
-            http://website.tld/
+ http://website.tld/ +
Some license
-            http://website.tld/
+ http://website.tld/ +

@@ -513,7 +510,8 @@ final class LicensePluginJavaSpec extends Specification {
Some license
-                  http://website.tld/
+ http://website.tld/ +

diff --git a/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy b/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy index 840b97b8..7e2c7b1d 100644 --- a/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy @@ -2,7 +2,9 @@ package com.jaredsburrows.license import static org.gradle.testkit.runner.TaskOutcome.SUCCESS import static test.TestUtils.gradleWithCommand +import static test.TestUtils.gradleWithCommandWithFail +import org.gradle.testkit.runner.GradleRunner import org.junit.Rule import org.junit.rules.TemporaryFolder import spock.lang.Specification @@ -72,6 +74,46 @@ final class LicensePluginSpec extends Specification { result.task(':licenseReport').outcome == SUCCESS } + def 'apply with no plugins'() { + given: + buildFile << + """ + plugins { + id 'com.jaredsburrows.license' + } + """ + + when: + def result = gradleWithCommandWithFail(testProjectDir.root, 'licenseReport', '-s') + + then: + result.output.contains("'com.jaredsburrows.license' requires Java or Android Gradle Plugins.") + } + + @Unroll def 'apply with non java or agp plugins: #plugin'() { + given: + buildFile << + """ + plugins { + id '${plugin}' + id 'com.jaredsburrows.license' + } + """ + + when: + def result = gradleWithCommandWithFail(testProjectDir.root, 'licenseReport', '-s') + + then: + result.output.contains("'com.jaredsburrows.license' requires Java or Android Gradle Plugins.") + + where: + plugin << [ + 'c', // CPlugin + 'cpp', // CppPlugin + 'cpp-library', // CppLibraryPlugin + ] + } + @Unroll def 'apply with allowed java plugins: #javaPlugin'() { given: buildFile << @@ -89,13 +131,17 @@ final class LicensePluginSpec extends Specification { result.task(':licenseReport').outcome == SUCCESS where: + // https://github.com/gradle/gradle/find/master, search for gradle-plugins javaPlugin << [ - 'application', // JavaApplicationPlugin - 'groovy', // GroovyPlugin - 'java', // JavaPlugin - 'java-gradle-plugin', // JavaGradlePluginPlugin - 'java-library', // JavaLibraryPlugin - 'scala', // ScalaPlugin + 'application', // JavaApplicationPlugin, applies JavaPlugin + 'groovy', // GroovyPlugin, applies JavaPlugin + 'java', // JavaPlugin, applies JavaBasePlugin + 'java-gradle-plugin', // JavaGradlePluginPlugin, applies JavaLibraryPlugin, JavaPlugin + 'java-library', // JavaLibraryPlugin, applies JavaPlugin + 'java-library-distribution', // JavaLibraryDistributionPlugin, applies JavaPlugin + 'java-platform', // JavaPlatformPlugin, can't use with JavaPlugin + 'scala', // ScalaPlugin, applies JavaPlugin + 'war', // WarPlugin, applies JavaPlugin ] } diff --git a/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy b/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy index ebfae065..e6cd21a2 100644 --- a/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy @@ -19,10 +19,7 @@ final class HtmlReportSpec extends Specification { """ - + Open source licenses @@ -80,30 +77,33 @@ final class HtmlReportSpec extends Specification {

Notice for packages:

diff --git a/src/test/groovy/test/TestUtils.groovy b/src/test/groovy/test/TestUtils.groovy index 3f4f5017..d805aa7c 100644 --- a/src/test/groovy/test/TestUtils.groovy +++ b/src/test/groovy/test/TestUtils.groovy @@ -42,6 +42,14 @@ final class TestUtils { .build() } + static def gradleWithCommandWithFail(def file, String... commands) { + return GradleRunner.create() + .withProjectDir(file) + .withArguments(commands) + .withPluginClasspath() + .buildAndFail() + } + static def myGetLicenseText(String fileName) { return HtmlReport.getLicenseText(fileName) }