Skip to content

Commit

Permalink
Add tests and fail fast
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jun 18, 2024
1 parent 15e729c commit 5197caf
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,14 @@ private static void collectFromComponent(ResolvedComponentResult rootComponent,
String versionRange = null;
if (requested instanceof ModuleComponentSelector requestedModule) {
var constraint = requestedModule.getVersionConstraint();
if (isValidVersionRange(constraint.getStrictVersion())) {
versionRange = constraint.getStrictVersion();
} else if (isValidVersionRange(constraint.getRequiredVersion())) {
versionRange = constraint.getRequiredVersion();
} else if (isValidVersionRange(constraint.getPreferredVersion())) {
versionRange = constraint.getPreferredVersion();
if (!constraint.getStrictVersion().isEmpty()) {
versionRange = validateVersionRange(constraint.getStrictVersion(), requestedModule);
} else if (!constraint.getRequiredVersion().isEmpty()) {
versionRange = validateVersionRange(constraint.getRequiredVersion(), requestedModule);
} else if (!constraint.getPreferredVersion().isEmpty()) {
versionRange = validateVersionRange(constraint.getPreferredVersion(), requestedModule);
} else {
// If none of the three version ranges was valid, but any of them was specified, we will now throw
// to inform the user they are using incompatible version ranges
validateSupportedRange(requestedModule, constraint.getStrictVersion());
validateSupportedRange(requestedModule, constraint.getRequiredVersion());
validateSupportedRange(requestedModule, constraint.getPreferredVersion());

if (isValidVersionRange(requestedModule.getVersion())) {
versionRange = requestedModule.getVersion();
}
versionRange = validateVersionRange(requestedModule.getVersion(), requestedModule);
}
}

Expand All @@ -183,12 +175,6 @@ private static void collectFromComponent(ResolvedComponentResult rootComponent,
}
}

private static void validateSupportedRange(ModuleComponentSelector component, String range) {
if (!range.isEmpty() && !isValidVersionRange(range)) {
throw new GradleException("Unsupported version constraint '" + range + "' on Jar-in-Jar dependency " + component.getModuleIdentifier());
}
}

private static @Nullable ArtifactIdentifier capabilityOrModule(final ResolvedVariantResult variant) {
ArtifactIdentifier moduleIdentifier = null;
if (variant.getOwner() instanceof ModuleComponentIdentifier moduleComponentIdentifier) {
Expand Down Expand Up @@ -237,16 +223,16 @@ private static void validateSupportedRange(ModuleComponentSelector component, St
return moduleOrCapabilityVersion(variant);
}

private static boolean isValidVersionRange(final @Nullable String range) {
if (range == null) {
return false;
}
private static String validateVersionRange(String range, ModuleComponentSelector module) {
try {
final VersionRange data = VersionRange.createFromVersionSpec(range);
return data.hasRestrictions() && data.getRecommendedVersion() == null && !range.contains("+");
} catch (InvalidVersionSpecificationException e) {
return false;
var data = VersionRange.createFromVersionSpec(range);
if (data.hasRestrictions() && data.getRecommendedVersion() == null && !range.contains("+")) {
return range;
}
} catch (InvalidVersionSpecificationException ignored) {
}

throw new GradleException("Unsupported version constraint '" + range + "' on Jar-in-Jar dependency " + module.getModuleIdentifier());
}

/**
Expand Down
112 changes: 112 additions & 0 deletions src/test/java/net/neoforged/moddevgradle/tasks/JarJarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.neoforged.moddevgradle.tasks;

import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.gradle.testkit.runner.TaskOutcome.NO_SOURCE;
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class JarJarTest {
@TempDir
Path tempDir;

@Test
public void testNoSourceWhenNoDependenciesAreDefined() throws IOException {
var result = runWithSource("");
assertEquals(NO_SOURCE, result.task(":jarJar").getOutcome());
}

@Test
public void testSuccessfulEmbed() throws IOException {
var result = runWithSource("""
dependencies {
jarJar(implementation("org.slf4j:slf4j-api")) {
version {
strictly '[0.1, 3.0)'
prefer '2.0.13'
}
}
}
""");
assertEquals(SUCCESS, result.task(":jarJar").getOutcome());
}

@Test
public void testUnsupportedStrictlyRange() {
var e = assertThrows(UnexpectedBuildFailure.class, () -> runWithSource("""
dependencies {
jarJar(implementation("org.slf4j:slf4j-api")) {
version {
strictly '[0.1, 3.0['
prefer '2.0.13'
}
}
}
"""));
assertThat(e).hasMessageContaining("Unsupported version constraint '[0.1, 3.0[' on Jar-in-Jar dependency org.slf4j:slf4j-api");
}

@Test
public void testUnsupportedRequiredRange() {
var e = assertThrows(UnexpectedBuildFailure.class, () -> runWithSource("""
dependencies {
jarJar(implementation("org.slf4j:slf4j-api:[0.1, 3.0["))
}
"""));
assertThat(e).hasMessageContaining("Unsupported version constraint '[0.1, 3.0[' on Jar-in-Jar dependency org.slf4j:slf4j-api");
}

@Test
public void testUnsupportedPreferredRange() {
var e = assertThrows(UnexpectedBuildFailure.class, () -> runWithSource("""
dependencies {
jarJar(implementation("org.slf4j:slf4j-api")) {
version {
prefer '[0.1, 3.0['
}
}
}
"""));
assertThat(e).hasMessageContaining("Unsupported version constraint '[0.1, 3.0[' on Jar-in-Jar dependency org.slf4j:slf4j-api");
}

@Test
public void testUnsupportedDynamicVersion() {
var e = assertThrows(UnexpectedBuildFailure.class, () -> runWithSource("""
dependencies {
jarJar(implementation("org.slf4j:slf4j-api:2.0.+"))
}
"""));
assertThat(e).hasMessageContaining("Unsupported version constraint '2.0.+' on Jar-in-Jar dependency org.slf4j:slf4j-api");
}

private BuildResult runWithSource(String source) throws IOException {
Files.writeString(tempDir.resolve("settings.gradle"), "");
Files.writeString(tempDir.resolve("build.gradle"), """
plugins {
id "net.neoforged.moddev"
}
repositories {
mavenCentral()
}
""" + source);

return GradleRunner.create()
.withPluginClasspath()
.withProjectDir(tempDir.toFile())
.withArguments("jarjar")
.withDebug(true)
.build();
}

}

0 comments on commit 5197caf

Please sign in to comment.