Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static me.itzg.helpers.McImageHelper.SPLIT_COMMA_NL;
import static me.itzg.helpers.McImageHelper.SPLIT_SYNOPSIS_COMMA_NL;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -86,31 +87,31 @@ static String processGameVersions(List<List<String>> allGameVersions) {

final int projectCount = allGameVersions.size();

// positions will start at the first usable position at end of each list and decrement
// and will become negative when finished traversing
final int[] positions = new int[projectCount];
for (int i = 0; i < projectCount; i++) {
positions[i] = allGameVersions.get(i).size();
positions[i] = allGameVersions.get(i).size() - 1;
}

while (!finished(positions)) {
while (Arrays.stream(positions)
// while any position is still usable
.anyMatch(p -> p >= 0)
) {
for (int i = 0; i < projectCount; i++) {
final String version = allGameVersions.get(i).get(--positions[i]);
final Integer result = gameVersionCounts.compute(version, (k, count) -> count == null ? 1 : count + 1);
if (result == projectCount) {
return version;
// still usable?
if (positions[i] >= 0) {
final int position = positions[i]--;
final String version = allGameVersions.get(i).get(position);
final Integer result = gameVersionCounts.compute(version, (k, count) -> count == null ? 1 : count + 1);
// did this version slot indicate match for all?
if (result == projectCount) {
return version;
}
}
}
}

return null;
}

static private boolean finished(int[] positions) {
for (final int position : positions) {
// since we pre-increment the positions
if (position <= 0) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package me.itzg.helpers.modrinth;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;

import com.github.stefanbirkner.systemlambda.SystemLambda;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -36,27 +37,71 @@ void processGameVersions(List<List<String>> versions, String expected) {
}

@SuppressWarnings("unused") // will be fixed https://youtrack.jetbrains.com/issue/IDEA-358214/Support-JUnit-5-FieldSource-annotation
static List<Arguments> processGameVersionsArgs = Arrays.asList(
argumentSet("matches", Arrays.asList(
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
static List<Arguments> processGameVersionsArgs = asList(
argumentSet("matches", asList(
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7", "1.21.8")
), "1.21.8"
),
argumentSet("justOneOff", Arrays.asList(
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
argumentSet("justOneOff", asList(
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7"),
asList("1.21.6", "1.21.7", "1.21.8")
), "1.21.7"
),
argumentSet("mismatch", Arrays.asList(
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
Arrays.asList("1.21.4", "1.21.5"),
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
argumentSet("mismatch", asList(
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.6", "1.21.7", "1.21.8"),
asList("1.21.4", "1.21.5"),
asList("1.21.6", "1.21.7", "1.21.8")
), null
),
argumentSet("fabric-api + nucledoom", asList(
// part of fabric-api
asList("24w46a",
"1.21.4-pre1",
"1.21.4-pre2",
"1.21.4-pre3",
"1.21.4-rc3",
"1.21.4",
"25w02a",
"25w03a",
"25w04a",
"25w05a",
"25w06a",
"25w07a",
"25w08a",
"25w09a",
"25w09b",
"25w10a",
"1.21.5-pre1",
"1.21.5-pre2",
"1.21.5-pre3",
"1.21.5-rc1",
"1.21.5-rc2",
"1.21.5",
"25w14craftmine",
"25w15a",
"25w16a",
"25w17a",
"25w18a",
"25w19a",
"25w20a",
"25w21a",
"1.21.6-pre1",
"1.21.6-pre3",
"1.21.6",
"1.21.7-rc1",
"1.21.7",
"1.21.8",
"25w31a",
"25w32a"),
// part of nucledoom
singletonList("1.21.4")
), "1.21.4"
)
);

Expand All @@ -79,6 +124,25 @@ void testCommand(WireMockRuntimeInfo wmInfo) throws Exception {
assertThat(out).isEqualToNormalizingNewlines("1.21.7\n");
}

@Test
void testCommandFabric(WireMockRuntimeInfo wmInfo) throws Exception {

stubGetProjects("fabric-api", "nucledoom");

final String out = SystemLambda.tapSystemOut(() -> {
final int exitCode = new CommandLine(new VersionFromModrinthProjectsCommand())
.execute(
"--api-base-url", wmInfo.getHttpBaseUrl(),
"--projects", "fabric-api, nucledoom"
);

assertThat(exitCode)
.isEqualTo(ExitCode.OK);
});

assertThat(out).isEqualToNormalizingNewlines("1.21.4\n");
}

@Test
void testCommandWithProjectQualifiers(WireMockRuntimeInfo wmInfo) throws Exception {

Expand Down
Loading