Skip to content
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
/out.log
/**/logs/
/test_results.html
/jarjar-gradle/src/functionalTest/generated/
/jarjar-gradle/.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package net.minecraftforge.jarjar.nio.pathfs;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -17,7 +18,7 @@

import static org.junit.jupiter.api.Assertions.*;

@SuppressWarnings("resource")
@SuppressWarnings({ "resource", "unused" })
public class TestPathFS
{
@Test
Expand All @@ -38,6 +39,7 @@ public void redirectionTest() throws URISyntaxException, IOException
assertArrayEquals(sourceData, pathFsData);
}

@Disabled
@Test
public void relativeDirectoryMapTest() throws URISyntaxException, IOException
{
Expand All @@ -59,6 +61,7 @@ public void relativeDirectoryMapTest() throws URISyntaxException, IOException
assertIterableEquals(sourceDirectories, pathFSDirectories);
}

@Disabled
@Test
public void absoluteDirectoryMapTest() throws URISyntaxException, IOException
{
Expand Down Expand Up @@ -188,6 +191,7 @@ public void relativeUriToRelativePathToRelativeUriTest() throws URISyntaxExcepti
assertEquals(uriInPathFS, resultUri);
}

@Disabled
@Test
public void recursiveRelativeRedirectionTest() throws URISyntaxException, IOException
{
Expand All @@ -209,6 +213,7 @@ public void recursiveRelativeRedirectionTest() throws URISyntaxException, IOExce
assertArrayEquals(sourceData, pathFsData);
}

@Disabled
@Test
public void absoluteRelativeRedirectionTest() throws URISyntaxException, IOException
{
Expand Down
34 changes: 34 additions & 0 deletions jarjar-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ gradlePlugin {
}
}

testing {
suites {
integrationTest(JvmTestSuite) {
useJUnitJupiter()
dependencies {
implementation project()
}
}
functionalTest(JvmTestSuite) {
useJUnitJupiter()
dependencies {
implementation project()
implementation libs.jarjar.metadata
implementation gradleTestKit()
}
}
}
}

publishing {
repositories {
maven gradleutils.getPublishingForgeMaven(rootProject.file('../repo'))
Expand All @@ -102,3 +121,18 @@ publishing {
}
}
}

// We are not a module, unifying the sourcesets fixes pluginUnderTestMetadata producing the correct paths
sourceSets.each { sourceSet ->
sourceSet.output.resourcesDir = sourceSet.java.destinationDirectory = layout.projectDirectory.dir("bin/$sourceSet.name")
}

// We need to put the plugin metadata file on the resource path, because eclipse doesn't use gradle's build folder
sourceSets.functionalTest.resources.srcDirs += [ 'src/functionalTest/generated/' ]
tasks.named('pluginUnderTestMetadata') {
outputDirectory = file('src/functionalTest/generated/')
}

eclipse {
synchronizationTasks pluginUnderTestMetadata
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.jarjar.gradle.tests;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.junit.jupiter.api.Test;

import net.minecraftforge.jarjar.metadata.ContainedJarIdentifier;
import net.minecraftforge.jarjar.metadata.ContainedJarMetadata;
import net.minecraftforge.jarjar.metadata.ContainedVersion;
import net.minecraftforge.jarjar.metadata.Metadata;
import net.minecraftforge.jarjar.metadata.MetadataIOHandler;

import static org.junit.jupiter.api.Assertions.*;

public class FunctionalTests extends FunctionalTestsBase {
private static final String JAR = ":jar";
private static final String JARJAR = ":jarJar";
private static final String METADATA = "META-INF/jarjar/metadata.json";

public FunctionalTests() {
super("9.0.0");
}

@Test
public void slimJarBuilds() throws IOException {
slimJarBuilds(new GroovyProject(projectDir));
}

@Test
public void slimJarBuildsKotlin() throws IOException {
slimJarBuilds(new KotlinProject(projectDir));
}

private void slimJarBuilds(GradleProject project) throws IOException {
project.simpleProject();
var results = build(JAR);
assertTaskSuccess(results, JAR);
}

@Test
public void jarJarSimple() throws IOException {
jarJarSimple(new GroovyProject(projectDir));
}

@Test
public void jarJarSimpleKotlin() throws IOException {
jarJarSimple(new KotlinProject(projectDir));
}

private void jarJarSimple(GradleProject project) throws IOException {
project.simpleJarJardLibrary("org.apache.maven:maven-artifact:3.9.11");

var results = build(JARJAR);
assertTaskSuccess(results, JARJAR);

var expected = new Metadata(List.of(
new ContainedJarMetadata(
id("org.apache.maven", "maven-artifact"),
version(rangeSpec("[3.9.11,)"), "3.9.11"),
"META-INF/jarjar/maven-artifact-3.9.11.jar",
false
)
));

var archive = projectDir.resolve("build/libs/test-all.jar");
assertTrue(Files.exists(archive), "JarJar'd jar does not exist at: " + archive);
readJarEntry(archive, expected.jars().get(0).path());
var actual = assertMetadataExists(archive);
assertMetadata(archive, expected, actual);
}

@Test
public void libraryConstraint() throws IOException {
libraryConstraint(new GroovyProject(projectDir));
}

@Test
public void libraryConstraintKotlin() throws IOException {
libraryConstraint(new KotlinProject(projectDir));
}

private void libraryConstraint(GradleProject project) throws IOException {
project.libraryConstraint("org.apache.maven:maven-artifact:3.9.11");

var results = build(JARJAR);
assertTaskSuccess(results, JARJAR);

var expected = new Metadata(List.of(
new ContainedJarMetadata(
id("org.apache.maven", "maven-artifact"),
version(rangeSpec("[3.9.11,)"), "3.9.11"),
// It's a constraint, but for legacy reasons we still add the path
"META-INF/jarjar/maven-artifact-3.9.11.jar",
false
)
));

var archive = projectDir.resolve("build/libs/test-all.jar");
assertTrue(Files.exists(archive), "JarJar'd jar does not exist at: " + archive);
// Make sure we don't actually ship the jar
assertFileMissing(archive, expected.jars().get(0).path());
var actual = assertMetadataExists(archive);
assertMetadata(archive, expected, actual);
}


// ==========================================================
// Helpers
// ==========================================================
protected static ContainedJarIdentifier id(String group, String artifact) {
return new ContainedJarIdentifier(group, artifact);
}
protected static VersionRange rangeSpec(String spec) {
try {
return VersionRange.createFromVersionSpec(spec);
} catch (InvalidVersionSpecificationException e) {
throw new RuntimeException(e);
}
}
protected static VersionRange range(String spec) {
return VersionRange.createFromVersion(spec);
}
protected static ArtifactVersion version(String version) {
return new DefaultArtifactVersion(version);
}
protected static ContainedVersion version(VersionRange range, String version) {
return new ContainedVersion(range, version(version));
}

protected static Metadata assertMetadataExists(Path file) throws IOException {
var data = readJarEntry(file, METADATA);
var meta = MetadataIOHandler.fromStream(new ByteArrayInputStream(data)).orElse(null);
assertNotNull(meta, "Invalid metadata file was generated: \n" + new String(data));
return meta;
}

protected static void assertMetadata(Path archive, Metadata expected, Metadata actual) throws IOException {
assertEquals(expected.jars().size(), actual.jars().size(), "Metadata did not have the correct number of jars.");
for (var jar : expected.jars()) {
ContainedJarMetadata ajar = null;
for (var tmp : actual.jars()) {
if (jar.identifier().equals(tmp.identifier())) {
ajar = tmp;
break;
}
}
assertNotNull(ajar, "Could not find " + jar.identifier().group() + ':' + jar.identifier().artifact() + " in metadata");
assertMetadata(archive, jar, ajar);
}
}

protected static void assertMetadata(Path archive, ContainedJarMetadata expected, ContainedJarMetadata actual) throws IOException {
assertEquals(expected.identifier(), actual.identifier());
assertEquals(expected.path(), actual.path(), "Path");
assertEquals(expected.isObfuscated(), actual.isObfuscated(), "isObfusicated");
assertMetadata(expected.version(), actual.version());
}

protected static void assertMetadata(ContainedVersion expected, ContainedVersion actual) {
if (expected == null) {
assertNull(actual, "Expected null contained version, actual: " + actual);
} else {
assertNotNull(actual, "Expected non-null contained version");
assertEquals(expected.range(), actual.range(), "Invalid Range");
assertEquals(expected.artifactVersion(), actual.artifactVersion(), "Invalid ArtifactVersion");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.jarjar.gradle.tests;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.*;

public abstract class FunctionalTestsBase {
@TempDir(cleanup = CleanupMode.ON_SUCCESS)
protected Path projectDir;

@RegisterExtension
AfterTestExecutionCallback afterTestExecutionCallback = this::after;
private void after(ExtensionContext context) throws Exception {
if (context.getExecutionException().isPresent())
System.out.println(context.getDisplayName() + " Failed: " + projectDir);
}

protected final String gradleVersion;

protected FunctionalTestsBase(String gradleVersion) {
this.gradleVersion = gradleVersion;
}

protected void writeFile(Path file, String content) throws IOException {
Files.createDirectories(file.getParent());
Files.writeString(file, content, StandardCharsets.UTF_8);
}

protected GradleRunner runner(String... args) {
return GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(projectDir.toFile())
.withArguments(args)
.withPluginClasspath();
}

protected BuildResult build(String... args) {
return runner(args).build();
}

protected static void assertTaskSuccess(BuildResult result, String task) {
assertTaskOutcome(result, task, TaskOutcome.SUCCESS);
}

protected static void assertTaskFailed(BuildResult result, String task) {
assertTaskOutcome(result, task, TaskOutcome.FAILED);
}

protected static void assertTaskOutcome(BuildResult result, String task, TaskOutcome expected) {
var info = result.task(task);
assertNotNull(info, "Could not find task `" + task + "` in build results");
assertEquals(expected, info.getOutcome());
}

protected static byte[] readJarEntry(Path path, String name) throws IOException {
try (var fs = FileSystems.newFileSystem(path)) {
var target = fs.getPath(name);
assertTrue(Files.exists(target), "Archive " + path + " does not contain " + name);
return Files.readAllBytes(target);
}
}

protected static void assertFileMissing(Path path, String name) throws IOException {
try (var fs = FileSystems.newFileSystem(path)) {
var target = fs.getPath(name);
assertFalse(Files.exists(target), "Archive " + path + " contains `" + name + "` when it should not");
}
}
}
Loading