diff --git a/build.gradle b/build.gradle index 1458eac..e128c26 100644 --- a/build.gradle +++ b/build.gradle @@ -78,6 +78,7 @@ dependencies { testImplementation('org.spockframework:spock-core:1.3-groovy-2.4') { exclude module: 'groovy-all' } + testImplementation 'io.javalin:javalin:3.13.4' compileOnly 'org.jetbrains:annotations:20.1.0' } diff --git a/src/test/groovy/net/fabricmc/loom/MavenProjectTest.groovy b/src/test/groovy/net/fabricmc/loom/MavenProjectTest.groovy new file mode 100644 index 0000000..9c70591 --- /dev/null +++ b/src/test/groovy/net/fabricmc/loom/MavenProjectTest.groovy @@ -0,0 +1,96 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom + +import net.fabricmc.loom.util.ArchiveAssertionsTrait +import net.fabricmc.loom.util.MockMavenServerTrait +import spock.lang.Specification +import spock.lang.Stepwise +import spock.lang.Unroll +import spock.util.environment.RestoreSystemProperties + +import static java.lang.System.setProperty +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS + +/** + * This tests publishing a range of versions and then tries to resolve and build against them + */ +@Stepwise +class MavenProjectTest extends Specification implements MockMavenServerTrait, ArchiveAssertionsTrait { + @RestoreSystemProperties + @Unroll + def "publish lib #version"() { + given: + setProperty('loom.test.version', version) + library = true + when: + def result = create("publish") + then: + result.task(":publish").outcome == SUCCESS + hasArchiveEntry("fabric-example-lib-${version}.jar", "net/fabricmc/example/ExampleLib.class") + where: + version | _ + '1.0.0' | _ + '1.1.0' | _ + '1.1.1' | _ + '1.2.0+meta' | _ + '2.0.0-SNAPSHOT' | _ + '2.0.0-SNAPSHOT' | _ // Publish this twice to give ourselves a bit of a challenge + 'master-SNAPSHOT' | _ + } + + @RestoreSystemProperties + @Unroll + def "resolve #version"() { + given: + setProperty('loom.test.resolve', "com.example:fabric-example-lib:${version}") + library = false + when: + def result = create("build") + then: + result.task(":build").outcome == SUCCESS + hasArchiveEntry("fabric-example-mod-1.0.0.jar", "net/fabricmc/examplemod/ExampleMod.class") + where: + version | _ + '1.0.0' | _ + '1.1.+' | _ + '1.2.0+meta' | _ + '2.0.0-SNAPSHOT' | _ + 'master-SNAPSHOT' | _ + '1.0.0:classifier' | _ + '1.1.+:classifier' | _ + '1.2.0+meta:classifier' | _ + '2.0.0-SNAPSHOT:classifier' | _ + 'master-SNAPSHOT:classifier' | _ + } + + // Set to true when to build and publish the mavenLibrary + private boolean library = false + + @Override + String name() { + library ? "mavenLibrary" : "maven" + } +} diff --git a/src/test/groovy/net/fabricmc/loom/util/ArchiveAssertionsTrait.groovy b/src/test/groovy/net/fabricmc/loom/util/ArchiveAssertionsTrait.groovy index 88d4df1..5a7a74b 100644 --- a/src/test/groovy/net/fabricmc/loom/util/ArchiveAssertionsTrait.groovy +++ b/src/test/groovy/net/fabricmc/loom/util/ArchiveAssertionsTrait.groovy @@ -26,7 +26,7 @@ package net.fabricmc.loom.util import org.zeroturnaround.zip.ZipUtil -trait ArchiveAssertionsTrait extends ProjectTestTrait { +trait ArchiveAssertionsTrait { String getArchiveEntry(String name, String entry, String project = "") { def file = getOutputFile(name, project) diff --git a/src/test/groovy/net/fabricmc/loom/util/MockMavenServerTrait.groovy b/src/test/groovy/net/fabricmc/loom/util/MockMavenServerTrait.groovy new file mode 100644 index 0000000..f7bfcb4 --- /dev/null +++ b/src/test/groovy/net/fabricmc/loom/util/MockMavenServerTrait.groovy @@ -0,0 +1,91 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016, 2017, 2018 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.util + +import io.javalin.Javalin +import org.apache.commons.io.IOUtils + +trait MockMavenServerTrait extends ProjectTestTrait { + public final int mavenServerPort = 9876 + public final File testMavenDir = File.createTempDir() + private Javalin server + + @SuppressWarnings('unused') + def setupSpec() { + println "Maven server path: ${testMavenDir.absolutePath}" + + server = Javalin.create { config -> + config.enableDevLogging() + }.start(mavenServerPort) + + /** + * A very very basic maven server impl, DO NOT copy this and use in production as its not secure + */ + server.get("*") { ctx -> + println "GET: " + ctx.path() + File file = getMavenPath(ctx.path()) + + if (!file.exists()) { + ctx.status(404) + return + } + + ctx.result(file.bytes) + } + + server.put("*") { ctx -> + println "PUT: " + ctx.path() + File file = getMavenPath(ctx.path()) + file.parentFile.mkdirs() + + file.withOutputStream { + IOUtils.copy(ctx.bodyAsInputStream(), it) + } + } + } + + @SuppressWarnings('unused') + def setup() { + System.setProperty('loom.test.mavenPort', port()) + } + + @SuppressWarnings('unused') + def cleanupSpec() { + server.stop() + super.cleanupSpec() + } + + File getMavenDirectory() { + new File(testMavenDir, "maven") + } + + File getMavenPath(String path) { + new File(getMavenDirectory(), path) + } + + String port() { + "${mavenServerPort}" + } +} \ No newline at end of file diff --git a/src/test/groovy/net/fabricmc/loom/util/ProjectTestTrait.groovy b/src/test/groovy/net/fabricmc/loom/util/ProjectTestTrait.groovy index fb323b6..76278f6 100644 --- a/src/test/groovy/net/fabricmc/loom/util/ProjectTestTrait.groovy +++ b/src/test/groovy/net/fabricmc/loom/util/ProjectTestTrait.groovy @@ -35,8 +35,9 @@ trait ProjectTestTrait { abstract String name() - @SuppressWarnings('unused') - def setup() { + def copyInputFiles() { + println "Project directory: ${testProjectDir}" + def baseProjectDir = new File("src/test/resources/projects/" + name()) if (!baseProjectDir.exists()) { @@ -51,6 +52,11 @@ trait ProjectTestTrait { def path = file.path.replace(baseProjectDir.path, "") File tempFile = new File(testProjectDir, path) + + if (tempFile.exists()) { + tempFile.delete() + } + tempFile.parentFile.mkdirs() tempFile << file.text } @@ -74,6 +80,7 @@ trait ProjectTestTrait { BuildResult create(String task, String gradleVersion = DEFAULT_GRADLE) { System.setProperty("fabric.loom.test", "true") + copyInputFiles() GradleRunner.create() .withProjectDir(testProjectDir) diff --git a/src/test/resources/projects/maven/build.gradle b/src/test/resources/projects/maven/build.gradle new file mode 100644 index 0000000..56930fc --- /dev/null +++ b/src/test/resources/projects/maven/build.gradle @@ -0,0 +1,25 @@ +plugins { + id 'fabric-loom' + id 'maven-publish' +} + +archivesBaseName = "fabric-example-mod" +version = "1.0.0" +group = "com.example" + +println archivesBaseName + +repositories { + maven { + url = "http://localhost:${System.getProperty("loom.test.mavenPort")}/" + allowInsecureProtocol = true + } +} + +dependencies { + minecraft "com.mojang:minecraft:1.16.5" + mappings "net.fabricmc:yarn:1.16.5+build.5:v2" + modImplementation "net.fabricmc:fabric-loader:0.11.2" + + modImplementation System.getProperty("loom.test.resolve") +} \ No newline at end of file diff --git a/src/test/resources/projects/maven/src/main/java/net/fabricmc/examplemod/ExampleMod.java b/src/test/resources/projects/maven/src/main/java/net/fabricmc/examplemod/ExampleMod.java new file mode 100644 index 0000000..f9831e9 --- /dev/null +++ b/src/test/resources/projects/maven/src/main/java/net/fabricmc/examplemod/ExampleMod.java @@ -0,0 +1,12 @@ +package net.fabricmc.examplemod; + +import net.fabricmc.api.ModInitializer; +import net.fabricmc.example.ExampleLib; + +public class ExampleMod implements ModInitializer { + @Override + public void onInitialize() { + // Lets make sure we can compile against the lib + ExampleLib.hello(); + } +} diff --git a/src/test/resources/projects/mavenLibrary/build.gradle b/src/test/resources/projects/mavenLibrary/build.gradle new file mode 100644 index 0000000..e0121c3 --- /dev/null +++ b/src/test/resources/projects/mavenLibrary/build.gradle @@ -0,0 +1,52 @@ +plugins { + id 'fabric-loom' + id 'maven-publish' +} + +archivesBaseName = "fabric-example-lib" +version = System.getProperty("loom.test.version") +group = "com.example" + +println archivesBaseName + +dependencies { + minecraft "com.mojang:minecraft:1.16.5" + mappings "net.fabricmc:yarn:1.16.5+build.5:v2" + modImplementation "net.fabricmc:fabric-loader:0.11.2" +} + +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } +} + +java { + withSourcesJar() +} + +publishing { + publications { + mavenJava(MavenPublication) { + artifact(remapJar) { + builtBy remapJar + } + artifact(remapJar) { + builtBy remapJar + classifier "classifier" + } + artifact(sourcesJar) { + builtBy remapSourcesJar + } + } + } + + repositories { + maven { + url "http://localhost:${System.getProperty("loom.test.mavenPort")}/" + allowInsecureProtocol = true + } + } +} diff --git a/src/test/resources/projects/mavenLibrary/settings.gradle b/src/test/resources/projects/mavenLibrary/settings.gradle new file mode 100644 index 0000000..fc4c0f7 --- /dev/null +++ b/src/test/resources/projects/mavenLibrary/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = "fabric-example-lib" + diff --git a/src/test/resources/projects/mavenLibrary/src/main/java/net/fabricmc/example/ExampleLib.java b/src/test/resources/projects/mavenLibrary/src/main/java/net/fabricmc/example/ExampleLib.java new file mode 100644 index 0000000..e6ec1e6 --- /dev/null +++ b/src/test/resources/projects/mavenLibrary/src/main/java/net/fabricmc/example/ExampleLib.java @@ -0,0 +1,13 @@ +package net.fabricmc.example; + +import net.fabricmc.api.ModInitializer; + +public class ExampleLib implements ModInitializer { + @Override + public void onInitialize() { + } + + public static void hello() { + System.out.println("Hello Fabric world!"); + } +} diff --git a/src/test/resources/projects/mavenLibrary/src/main/resources/fabric.mod.json b/src/test/resources/projects/mavenLibrary/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..f527100 --- /dev/null +++ b/src/test/resources/projects/mavenLibrary/src/main/resources/fabric.mod.json @@ -0,0 +1,4 @@ +{ + "schemaVersion": 1, + "id": "modid" +}