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

Align logic for functional test kits #1122

Merged
merged 16 commits into from
Dec 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
import org.apache.commons.lang3.StringUtils
import org.codehaus.plexus.util.IOUtil
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -18,25 +19,25 @@ import java.util.jar.JarFile
abstract class BasePluginSpecification extends Specification {

@TempDir
Path dir
Path root

AppendableMavenFileRepository repo

def setup() {
repo = repo()
repo.module('junit', 'junit', '3.8.2').use(testJar).publish()
repo.module('junit', 'junit', '3.8.2')
.use(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
.publish()

buildFile << getDefaultBuildScript('java', true, true)
buildFile << System.lineSeparator()
settingsFile << settingsBuildScript
settingsFile << System.lineSeparator()
projectScriptFile << getDefaultProjectBuildScript('java', true, true)
settingsScriptFile << getDefaultSettingsBuildScript()
}

def cleanup() {
println buildFile.text
println projectScriptFile.text
}

String getDefaultBuildScript(
String getDefaultProjectBuildScript(
String javaPlugin = 'java',
boolean withGroup = false,
boolean withVersion = false
Expand All @@ -52,10 +53,10 @@ abstract class BasePluginSpecification extends Specification {

$groupInfo
$versionInfo
""".stripIndent().trim()
""".stripIndent().trim() + System.lineSeparator()
}

String getSettingsBuildScript(boolean withRootProject = true) {
String getDefaultSettingsBuildScript(boolean withRootProject = true) {
def rootProjectInfo = withRootProject ? "rootProject.name = 'shadow'" : ""
return """
dependencyResolutionManagement {
Expand All @@ -66,14 +67,14 @@ abstract class BasePluginSpecification extends Specification {
}

$rootProjectInfo
""".stripIndent().trim()
""".stripIndent().trim() + System.lineSeparator()
}

static def shadowJar = "tasks.named('shadowJar', ${ShadowJar.class.name})".trim()

GradleRunner getRunner() {
GradleRunner.create()
.withProjectDir(dir.toFile())
.withProjectDir(root.toFile())
.forwardOutput()
.withPluginClasspath()
.withTestKitDir(testKitDir)
Expand All @@ -93,37 +94,40 @@ abstract class BasePluginSpecification extends Specification {
return result
}

BuildResult runWithDebug(String... tasks) {
return run(tasks.toList(), { it.withDebug(true) })
}

BuildResult runWithFailure(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
def result = runnerFunction.apply(runner(tasks)).buildAndFail()
assertNoDeprecationWarnings(result)
return result
}

static void assertNoDeprecationWarnings(BuildResult result) {
private static void assertNoDeprecationWarnings(BuildResult result) {
result.output.eachLine {
assert !containsDeprecationWarning(it)
}
}

static boolean containsDeprecationWarning(String output) {
private static boolean containsDeprecationWarning(String output) {
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
}

File getBuildFile() {
File getProjectScriptFile() {
file('build.gradle')
}

File getSettingsFile() {
File getSettingsScriptFile() {
file('settings.gradle')
}

File file(String path) {
File f = dir.resolve(path).toFile()
File f = root.resolve(path).toFile()
String extension = StringUtils.substringAfterLast(path, '.')

// Binary files should be asserted to exist, text files should be created.
if (extension == "jar" || extension == "zip") {
return f
}

if (!f.exists()) {
f.parentFile.mkdirs()
if (!f.createNewFile()) {
Expand All @@ -133,16 +137,8 @@ abstract class BasePluginSpecification extends Specification {
return f
}

File getFile(String path) {
return dir.resolve(path).toFile()
}

AppendableMavenFileRepository repo(String path = 'maven-repo') {
new AppendableMavenFileRepository(dir.resolve(path))
}

void assertJarFileContentsEqual(File f, String path, String contents) {
assert getJarFileContents(f, path) == contents
new AppendableMavenFileRepository(root.resolve(path))
}

String getJarFileContents(File f, String path) {
Expand All @@ -155,15 +151,15 @@ abstract class BasePluginSpecification extends Specification {
return sw.toString()
}

void contains(File f, List<String> paths) {
void assertContains(File f, List<String> paths) {
JarFile jar = new JarFile(f)
paths.each { path ->
assert jar.getJarEntry(path), "${f.path} does not contain [$path]"
}
jar.close()
}

void doesNotContain(File f, List<String> paths) {
void assertDoesNotContain(File f, List<String> paths) {
JarFile jar = new JarFile(f)
paths.each { path ->
assert !jar.getJarEntry(path), "${f.path} contains [$path]"
Expand All @@ -184,27 +180,19 @@ abstract class BasePluginSpecification extends Specification {
return new AppendableJar(file(path).toPath())
}

protected File getOutput() {
getFile('build/libs/shadow-1.0-all.jar')
}

protected File output(String name) {
getFile("build/libs/${name}")
}

protected Path getTestJar(String name = 'junit-3.8.2.jar') {
return Paths.get(this.class.classLoader.getResource(name).toURI())
File getOutputShadowJar() {
file('build/libs/shadow-1.0-all.jar')
}

protected static File getTestKitDir() {
static File getTestKitDir() {
def gradleUserHome = System.getenv("GRADLE_USER_HOME")
if (!gradleUserHome) {
gradleUserHome = new File(System.getProperty("user.home"), ".gradle").absolutePath
}
return new File(gradleUserHome, "testkit")
}

protected static String escapedPath(Path path) {
static String escapedPath(Path path) {
return path.toString().replaceAll('\\\\', '\\\\\\\\')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class PublishingSpec extends BasePluginSpecification {
.insertFile('b.properties', 'b')
.publish()

settingsFile << "rootProject.name = 'maven'"
buildFile << """
settingsScriptFile << "rootProject.name = 'maven'"
projectScriptFile << """
apply plugin: 'maven-publish'

dependencies {
Expand Down Expand Up @@ -63,7 +63,7 @@ class PublishingSpec extends BasePluginSpecification {
assert publishedFile.exists()

and:
contains(publishedFile, ['a.properties', 'a2.properties'])
assertContains(publishedFile, ['a.properties', 'a2.properties'])

and:
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile
Expand Down Expand Up @@ -93,8 +93,8 @@ class PublishingSpec extends BasePluginSpecification {
.insertFile('b.properties', 'b')
.publish()

settingsFile << "rootProject.name = 'maven'"
buildFile << """
settingsScriptFile << "rootProject.name = 'maven'"
projectScriptFile << """
apply plugin: 'maven-publish'

dependencies {
Expand Down Expand Up @@ -134,14 +134,14 @@ class PublishingSpec extends BasePluginSpecification {
def "publish multiproject shadow jar with maven-publish plugin"() {
given:

settingsFile << """
settingsScriptFile << """
rootProject.name = 'maven'
include 'a'
include 'b'
include 'c'
""".stripMargin()

buildFile.text = """
projectScriptFile.text = """
subprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'
Expand Down Expand Up @@ -211,7 +211,7 @@ class PublishingSpec extends BasePluginSpecification {
assert publishedFile.exists()

and:
contains(publishedFile, ['a.properties', 'a2.properties'])
assertContains(publishedFile, ['a.properties', 'a2.properties'])

and:
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile
Expand All @@ -237,10 +237,10 @@ class PublishingSpec extends BasePluginSpecification {
.insertFile('b.properties', 'b')
.publish()

settingsFile << """
settingsScriptFile << """
rootProject.name = 'maven'
"""
buildFile << """
projectScriptFile << """
apply plugin: 'maven-publish'
dependencies {
implementation 'shadow:a:1.0'
Expand Down Expand Up @@ -277,7 +277,7 @@ class PublishingSpec extends BasePluginSpecification {
assert shadowJar.exists()

and:
contains(shadowJar, ['a.properties', 'a2.properties'])
assertContains(shadowJar, ['a.properties', 'a2.properties'])

and: "publishes both a POM file and a Gradle metadata file"
File pom = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.pom').toFile().canonicalFile
Expand Down Expand Up @@ -328,7 +328,7 @@ class PublishingSpec extends BasePluginSpecification {
assertions {
shadowJar = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0-all.jar').toFile().canonicalFile
assert shadowJar.exists()
contains(shadowJar, ['a.properties', 'a2.properties'])
assertContains(shadowJar, ['a.properties', 'a2.properties'])
}

assertions {
Expand Down
Loading
Loading