Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more tests #195

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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