Add MavenProjectTest (#372)

This commit is contained in:
modmuss50 2021-03-27 20:26:10 +00:00 committed by GitHub
parent ed08e47aab
commit 71535fa75f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 306 additions and 3 deletions

View file

@ -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'
}

View file

@ -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"
}
}

View file

@ -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)

View file

@ -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}"
}
}

View file

@ -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)

View file

@ -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")
}

View file

@ -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();
}
}

View file

@ -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
}
}
}

View file

@ -0,0 +1,2 @@
rootProject.name = "fabric-example-lib"

View file

@ -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!");
}
}

View file

@ -0,0 +1,4 @@
{
"schemaVersion": 1,
"id": "modid"
}