|
| 1 | +package net.neoforged.moddevgradle.functional; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.List; |
| 7 | +import java.util.regex.Pattern; |
| 8 | +import org.gradle.testkit.runner.BuildResult; |
| 9 | +import org.gradle.testkit.runner.GradleRunner; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class RepositoriesPluginFunctionalTest extends AbstractFunctionalTest { |
| 13 | + private static final Pattern REPOSITORY_ORDER_PATTERN = Pattern.compile("REPOSITORY_ORDER=(.+)"); |
| 14 | + private static final List<String> MANAGED_REPOSITORIES = List.of("NeoForged Releases"); |
| 15 | + |
| 16 | + @Test |
| 17 | + void placesMavenCentralBeforeManagedRepositories() throws IOException { |
| 18 | + writeFile(settingsFile, "rootProject.name = 'repository-order-test'"); |
| 19 | + writeGroovyBuildScript(""" |
| 20 | + plugins { |
| 21 | + id "net.neoforged.moddev.repositories" |
| 22 | + } |
| 23 | +
|
| 24 | + repositories { |
| 25 | + mavenCentral() |
| 26 | + } |
| 27 | +
|
| 28 | + tasks.register("printRepositoryOrder") { |
| 29 | + doLast { |
| 30 | + println("REPOSITORY_ORDER=" + repositories.collect { |
| 31 | + def urlPart = it.hasProperty("url") ? it.url.toString() : "" |
| 32 | + return it.name + "@" + urlPart |
| 33 | + }.join("|")) |
| 34 | + } |
| 35 | + } |
| 36 | + """); |
| 37 | + |
| 38 | + BuildResult result = GradleRunner.create() |
| 39 | + .withPluginClasspath() |
| 40 | + .withProjectDir(testProjectDir) |
| 41 | + .withArguments("printRepositoryOrder") |
| 42 | + .build(); |
| 43 | + |
| 44 | + List<String> repositoryNames = extractRepositoryOrder(result); |
| 45 | + assertMavenCentralBeforeManagedRepositories(repositoryNames); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void placesMavenCentralBeforeManagedRepositoriesInSettings() throws IOException { |
| 50 | + writeGroovySettingsScript(""" |
| 51 | + plugins { |
| 52 | + id "net.neoforged.moddev.repositories" |
| 53 | + } |
| 54 | +
|
| 55 | + rootProject.name = "repository-order-settings-test" |
| 56 | +
|
| 57 | + dependencyResolutionManagement { |
| 58 | + repositories { |
| 59 | + mavenCentral() |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + gradle.settingsEvaluated { |
| 64 | + println("REPOSITORY_ORDER=" + dependencyResolutionManagement.repositories.collect { |
| 65 | + def urlPart = it.hasProperty("url") ? it.url.toString() : "" |
| 66 | + return it.name + "@" + urlPart |
| 67 | + }.join("|")) |
| 68 | + } |
| 69 | + """); |
| 70 | + writeGroovyBuildScript(""); |
| 71 | + |
| 72 | + BuildResult result = GradleRunner.create() |
| 73 | + .withPluginClasspath() |
| 74 | + .withProjectDir(testProjectDir) |
| 75 | + .withArguments("help") |
| 76 | + .build(); |
| 77 | + |
| 78 | + List<String> repositoryNames = extractRepositoryOrder(result); |
| 79 | + assertMavenCentralBeforeManagedRepositories(repositoryNames); |
| 80 | + } |
| 81 | + |
| 82 | + private static List<String> extractRepositoryOrder(BuildResult result) { |
| 83 | + var matcher = REPOSITORY_ORDER_PATTERN.matcher(result.getOutput()); |
| 84 | + assertThat(matcher.find()) |
| 85 | + .as("Expected repository order marker in Gradle output") |
| 86 | + .isTrue(); |
| 87 | + return List.of(matcher.group(1).split("\\|")); |
| 88 | + } |
| 89 | + |
| 90 | + private static void assertMavenCentralBeforeManagedRepositories(List<String> repositoryNames) { |
| 91 | + int mavenCentralIndex = -1; |
| 92 | + for (int i = 0; i < repositoryNames.size(); i++) { |
| 93 | + String repository = repositoryNames.get(i); |
| 94 | + if (repository.contains("repo.maven.apache.org/maven2") || repository.contains("repo1.maven.org/maven2")) { |
| 95 | + mavenCentralIndex = i; |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + int firstManagedIndex = repositoryNames.size(); |
| 100 | + for (String managedRepository : MANAGED_REPOSITORIES) { |
| 101 | + for (int i = 0; i < repositoryNames.size(); i++) { |
| 102 | + if (repositoryNames.get(i).startsWith(managedRepository + "@") && i < firstManagedIndex) { |
| 103 | + firstManagedIndex = i; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + assertThat(mavenCentralIndex) |
| 109 | + .as("Expected mavenCentral repository to exist. Actual order: %s", repositoryNames) |
| 110 | + .isGreaterThanOrEqualTo(0); |
| 111 | + assertThat(firstManagedIndex) |
| 112 | + .as("Expected at least one managed repository to exist. Actual order: %s", repositoryNames) |
| 113 | + .isLessThan(repositoryNames.size()); |
| 114 | + assertThat(mavenCentralIndex) |
| 115 | + .as("Expected mavenCentral before managed repositories. Actual order: %s", repositoryNames) |
| 116 | + .isLessThan(firstManagedIndex); |
| 117 | + } |
| 118 | +} |
0 commit comments