Skip to content

Commit

Permalink
add more tests (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredsburrows committed Apr 20, 2022
1 parent 396796f commit 1f3c126
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 80 deletions.
19 changes: 3 additions & 16 deletions src/main/kotlin/com/jaredsburrows/license/projectJava.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,18 @@ 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"
)
)
}

/**
* 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ final class LicensePluginAndroidSpec extends Specification {
"""
<html>
<head>
<style>
body { font-family: sans-serif }
pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }
</style>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
Expand Down Expand Up @@ -995,7 +992,8 @@ final class LicensePluginAndroidSpec extends Specification {
</li>
<a name="-296292112"></a>
<pre>Some license
<a href="http://website.tld/">http://website.tld/</a></pre>
<a href="http://website.tld/">http://website.tld/</a>
</pre>
<br>
<hr>
</ul>
Expand Down Expand Up @@ -1200,24 +1198,25 @@ final class LicensePluginAndroidSpec extends Specification {
def expectedHtml =
"""
<html>
<head>
<head>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
<h3>Notice for packages:</h3>
<ul>
<li><a href="#1934118923">design (26.1.0)</a>
</head>
<body>
<h3>Notice for packages:</h3>
<ul>
<li>
<a href="#1934118923">design (26.1.0)</a>
<dl>
<dt>Copyright &copy; 20xx The original author or authors</dt>
<dt>Copyright &copy; 20xx The original author or authors</dt>
</dl>
</li>
<a name="1934118923"></a>
<pre>${myGetLicenseText('apache-2.0.txt')}</pre>
<br>
<hr>
</ul>
</body>
</li>
<a name="1934118923"></a>
<pre>${myGetLicenseText('apache-2.0.txt')}</pre>
<br>
<hr>
</ul>
</body>
</html>
"""
def expectedJson =
Expand All @@ -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.")
Expand Down Expand Up @@ -1300,24 +1298,25 @@ final class LicensePluginAndroidSpec extends Specification {
def expectedHtml =
"""
<html>
<head>
<head>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
<h3>Notice for packages:</h3>
<ul>
<li><a href="#1934118923">design (26.1.0)</a>
</head>
<body>
<h3>Notice for packages:</h3>
<ul>
<li>
<a href="#1934118923">design (26.1.0)</a>
<dl>
<dt>Copyright &copy; 20xx The original author or authors</dt>
<dt>Copyright &copy; 20xx The original author or authors</dt>
</dl>
</li>
<a name="1934118923"></a>
<pre>${myGetLicenseText('apache-2.0.txt')}</pre>
<br>
<hr>
</ul>
</body>
</li>
<a name="1934118923"></a>
<pre>${myGetLicenseText('apache-2.0.txt')}</pre>
<br>
<hr>
</ul>
</body>
</html>
"""
def expectedJson =
Expand All @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ final class LicensePluginJavaSpec extends Specification {
"""
<html>
<head>
<style>
body { font-family: sans-serif }
pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }
</style>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
Expand Down Expand Up @@ -266,10 +263,7 @@ final class LicensePluginJavaSpec extends Specification {
"""
<html>
<head>
<style>
body { font-family: sans-serif }
pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }
</style>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
Expand Down Expand Up @@ -333,7 +327,8 @@ final class LicensePluginJavaSpec extends Specification {
</li>
<a name="-296292112"></a>
<pre>Some license
<a href="http://website.tld/">http://website.tld/</a></pre>
<a href="http://website.tld/">http://website.tld/</a>
</pre>
<br>
<hr>
</ul>
Expand Down Expand Up @@ -414,10 +409,12 @@ final class LicensePluginJavaSpec extends Specification {
</li>
<a name="1195092182"></a>
<pre>Some license
<a href="http://website.tld/">http://website.tld/</a></pre>
<a href="http://website.tld/">http://website.tld/</a>
</pre>
<br>
<pre>Some license
<a href="http://website.tld/">http://website.tld/</a></pre>
<a href="http://website.tld/">http://website.tld/</a>
</pre>
<br>
<hr>
</ul>
Expand Down Expand Up @@ -513,7 +510,8 @@ final class LicensePluginJavaSpec extends Specification {
</li>
<a name="-296292112"></a>
<pre>Some license
<a href="http://website.tld/">http://website.tld/</a></pre>
<a href="http://website.tld/">http://website.tld/</a>
</pre>
<br>
<hr>
</ul>
Expand Down
58 changes: 52 additions & 6 deletions src/test/groovy/com/jaredsburrows/license/LicensePluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <<
Expand All @@ -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
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ final class HtmlReportSpec extends Specification {
"""
<html>
<head>
<style>
body { font-family: sans-serif }
pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }
</style>
<style>body { font-family: sans-serif } pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }</style>
<title>Open source licenses</title>
</head>
<body>
Expand Down Expand Up @@ -80,30 +77,33 @@ final class HtmlReportSpec extends Specification {
<body>
<h3>Notice for packages:</h3>
<ul>
<li><a href="#87638953">name (1.2.3)</a>
<li>
<a href="#87638953">name (1.2.3)</a>
<dl>
<dt>Copyright &copy; year name</dt>
<dt>Copyright &copy; year name</dt>
</dl>
</li>
<li><a href="#87638953">name (1.2.3)</a>
<li>
<a href="#87638953">name (1.2.3)</a>
<dl>
<dt>Copyright &copy; year name</dt>
<dt>Copyright &copy; year name</dt>
</dl>
</li>
<a name="87638953"></a>
<a name="87638953"></a>
<pre>name
<a href="url">url</a></pre>
<br>
<a href="url">url</a></pre>
<br>
<hr>
<li><a href="#0">name (1.2.3)</a>
<li>
<a href="#0">name (1.2.3)</a>
<dl>
<dt>Copyright &copy; 20xx name</dt>
<dt>Copyright &copy; 20xx name</dt>
</dl>
</li>
<a name="0"></a>
<a name="0"></a>
<pre>No license found</pre>
<hr>
</ul>
Expand Down
8 changes: 8 additions & 0 deletions src/test/groovy/test/TestUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 1f3c126

Please sign in to comment.