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

Eliminate redundant TestFile in functional tests #1083

Merged
merged 10 commits into from
Dec 3, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow

import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenPathRepository
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
import groovy.json.JsonSlurper
import groovy.xml.XmlSlurper
Expand All @@ -10,7 +10,7 @@ import spock.lang.Issue

class PublishingSpec extends PluginSpecification {

AppendableMavenFileRepository publishingRepo
AppendableMavenPathRepository publishingRepo

@Override
def setup() {
Expand Down Expand Up @@ -60,14 +60,14 @@ class PublishingSpec extends PluginSpecification {
run('publish')

then:
File publishedFile = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile
File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile
assert publishedFile.exists()

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

and:
File pom = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile
assert pom.exists()

def contents = new XmlSlurper().parse(pom)
Expand Down Expand Up @@ -128,7 +128,7 @@ class PublishingSpec extends PluginSpecification {
run('publish')

then:
File publishedFile = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0-my-classifier.my-ext').canonicalFile
File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0-my-classifier.my-ext').canonicalFile
assert publishedFile.exists()
}

Expand Down Expand Up @@ -209,14 +209,14 @@ class PublishingSpec extends PluginSpecification {
run('publish')

then:
File publishedFile = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile
File publishedFile = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.jar').canonicalFile
assert publishedFile.exists()

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

and:
File pom = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').canonicalFile
assert pom.exists()

def contents = new XmlSlurper().parse(pom)
Expand Down Expand Up @@ -273,17 +273,17 @@ class PublishingSpec extends PluginSpecification {
run('publish')

then:
File mainJar = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.jar').canonicalFile
File shadowJar = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0-all.jar').canonicalFile
File mainJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.jar').canonicalFile
File shadowJar = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0-all.jar').canonicalFile
assert mainJar.exists()
assert shadowJar.exists()

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

and: "publishes both a POM file and a Gradle metadata file"
File pom = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.pom').canonicalFile
File gmm = publishingRepo.rootDir.file('com/acme/maven/1.0/maven-1.0.module').canonicalFile
File pom = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.pom').canonicalFile
File gmm = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.module').canonicalFile
pom.exists()
gmm.exists()

Expand Down Expand Up @@ -328,13 +328,13 @@ class PublishingSpec extends PluginSpecification {

and: "verify shadow publication"
assertions {
shadowJar = publishingRepo.rootDir.file('com/acme/maven-all/1.0/maven-all-1.0-all.jar').canonicalFile
shadowJar = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0-all.jar').canonicalFile
assert shadowJar.exists()
contains(shadowJar, ['a.properties', 'a2.properties'])
}

assertions {
pom = publishingRepo.rootDir.file('com/acme/maven-all/1.0/maven-all-1.0.pom').canonicalFile
pom = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.pom').canonicalFile
assert pom.exists()
pomContents = new XmlSlurper().parse(pom)
assert pomContents.dependencies[0].dependency.size() == 1
Expand All @@ -351,7 +351,7 @@ class PublishingSpec extends PluginSpecification {
}

assertions {
gmm = publishingRepo.rootDir.file('com/acme/maven-all/1.0/maven-all-1.0.module').canonicalFile
gmm = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0.module').canonicalFile
assert gmm.exists()
gmmContents = new JsonSlurper().parse(gmm)
assert gmmContents.variants.size() == 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.jengelman.gradle.plugins.shadow.util

import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenFileRepository
import com.github.jengelman.gradle.plugins.shadow.util.repo.maven.MavenPathRepository
import groovy.transform.InheritConstructors

@InheritConstructors
class AppendableMavenFileRepository extends MavenFileRepository {
class AppendableMavenPathRepository extends MavenPathRepository {

@Override
AppendableMavenFileModule module(String groupId, String artifactId, Object version = '1.0') {
def artifactDir = rootDir.file("${groupId.replace('.', '/')}/$artifactId/$version")
def artifactDir = rootDir.resolve("${groupId.replace('.', '/')}/$artifactId/$version")
return new AppendableMavenFileModule(artifactDir, groupId, artifactId, version as String)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.jengelman.gradle.plugins.shadow.util

import com.github.jengelman.gradle.plugins.shadow.util.file.TestFile

Goooler marked this conversation as resolved.
Show resolved Hide resolved
import org.codehaus.plexus.util.IOUtil
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
Expand All @@ -19,7 +19,7 @@ abstract class PluginSpecification extends Specification {

public static final String SHADOW_VERSION = System.getProperty("shadowVersion")

AppendableMavenFileRepository repo
AppendableMavenPathRepository repo

def setup() {
repo = repo()
Expand Down Expand Up @@ -120,8 +120,8 @@ abstract class PluginSpecification extends Specification {
return dir.resolve(path).toFile()
}

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

void assertJarFileContentsEqual(File f, String path, String contents) {
Expand Down Expand Up @@ -186,4 +186,29 @@ abstract class PluginSpecification extends Specification {
}
return new File(gradleUserHome, "testkit")
}

/**
* TODO: this is used as extensions for Groovy, could be replaced after migrated to Kotlin.
* Registered in resources/META-INF/services/org.codehaus.groovy.runtime.ExtensionModule.
*/
static final class FileExtensions {
static final File resolve(File file, String relativePath) {
try {
return new File(file, relativePath)
} catch (RuntimeException e) {
throw new RuntimeException(String.format("Could not locate file '%s' relative to '%s'.", Arrays.toString(relativePath), file), e)
}
}

static final File createDir(File file) {
if (file.mkdirs()) {
return file
}
if (file.isDirectory()) {
return file
}
throw new AssertionError("Problems creating dir: " + this
+ ". Diagnostics: exists=" + file.exists() + ", isFile=" + file.isFile() + ", isDirectory=" + file.isDirectory())
}
}
}

This file was deleted.

Loading
Loading