Skip to content

Commit 1ae1a3a

Browse files
authored
modrinth: fixed version-from-modrinth-projects when a project has shorter list (#615)
1 parent fb5aa25 commit 1ae1a3a

File tree

4 files changed

+1586
-33
lines changed

4 files changed

+1586
-33
lines changed

src/main/java/me/itzg/helpers/modrinth/VersionFromModrinthProjectsCommand.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static me.itzg.helpers.McImageHelper.SPLIT_COMMA_NL;
44
import static me.itzg.helpers.McImageHelper.SPLIT_SYNOPSIS_COMMA_NL;
55

6+
import java.util.Arrays;
67
import java.util.HashMap;
78
import java.util.List;
89
import java.util.Map;
@@ -86,31 +87,31 @@ static String processGameVersions(List<List<String>> allGameVersions) {
8687

8788
final int projectCount = allGameVersions.size();
8889

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

94-
while (!finished(positions)) {
97+
while (Arrays.stream(positions)
98+
// while any position is still usable
99+
.anyMatch(p -> p >= 0)
100+
) {
95101
for (int i = 0; i < projectCount; i++) {
96-
final String version = allGameVersions.get(i).get(--positions[i]);
97-
final Integer result = gameVersionCounts.compute(version, (k, count) -> count == null ? 1 : count + 1);
98-
if (result == projectCount) {
99-
return version;
102+
// still usable?
103+
if (positions[i] >= 0) {
104+
final int position = positions[i]--;
105+
final String version = allGameVersions.get(i).get(position);
106+
final Integer result = gameVersionCounts.compute(version, (k, count) -> count == null ? 1 : count + 1);
107+
// did this version slot indicate match for all?
108+
if (result == projectCount) {
109+
return version;
110+
}
100111
}
101112
}
102113
}
103114

104115
return null;
105116
}
106-
107-
static private boolean finished(int[] positions) {
108-
for (final int position : positions) {
109-
// since we pre-increment the positions
110-
if (position <= 0) {
111-
return true;
112-
}
113-
}
114-
return false;
115-
}
116117
}

src/test/java/me/itzg/helpers/modrinth/VersionFromModrinthProjectsCommandTest.java

Lines changed: 81 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package me.itzg.helpers.modrinth;
22

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

79
import com.github.stefanbirkner.systemlambda.SystemLambda;
810
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
911
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
10-
import java.util.Arrays;
1112
import java.util.List;
1213
import org.junit.jupiter.api.Test;
1314
import org.junit.jupiter.params.ParameterizedTest;
@@ -36,27 +37,71 @@ void processGameVersions(List<List<String>> versions, String expected) {
3637
}
3738

3839
@SuppressWarnings("unused") // will be fixed https://youtrack.jetbrains.com/issue/IDEA-358214/Support-JUnit-5-FieldSource-annotation
39-
static List<Arguments> processGameVersionsArgs = Arrays.asList(
40-
argumentSet("matches", Arrays.asList(
41-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
42-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
43-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
44-
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
40+
static List<Arguments> processGameVersionsArgs = asList(
41+
argumentSet("matches", asList(
42+
asList("1.21.6", "1.21.7", "1.21.8"),
43+
asList("1.21.6", "1.21.7", "1.21.8"),
44+
asList("1.21.6", "1.21.7", "1.21.8"),
45+
asList("1.21.6", "1.21.7", "1.21.8")
4546
), "1.21.8"
4647
),
47-
argumentSet("justOneOff", Arrays.asList(
48-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
49-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
50-
Arrays.asList("1.21.6", "1.21.7"),
51-
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
48+
argumentSet("justOneOff", asList(
49+
asList("1.21.6", "1.21.7", "1.21.8"),
50+
asList("1.21.6", "1.21.7", "1.21.8"),
51+
asList("1.21.6", "1.21.7"),
52+
asList("1.21.6", "1.21.7", "1.21.8")
5253
), "1.21.7"
5354
),
54-
argumentSet("mismatch", Arrays.asList(
55-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
56-
Arrays.asList("1.21.6", "1.21.7", "1.21.8"),
57-
Arrays.asList("1.21.4", "1.21.5"),
58-
Arrays.asList("1.21.6", "1.21.7", "1.21.8")
55+
argumentSet("mismatch", asList(
56+
asList("1.21.6", "1.21.7", "1.21.8"),
57+
asList("1.21.6", "1.21.7", "1.21.8"),
58+
asList("1.21.4", "1.21.5"),
59+
asList("1.21.6", "1.21.7", "1.21.8")
5960
), null
61+
),
62+
argumentSet("fabric-api + nucledoom", asList(
63+
// part of fabric-api
64+
asList("24w46a",
65+
"1.21.4-pre1",
66+
"1.21.4-pre2",
67+
"1.21.4-pre3",
68+
"1.21.4-rc3",
69+
"1.21.4",
70+
"25w02a",
71+
"25w03a",
72+
"25w04a",
73+
"25w05a",
74+
"25w06a",
75+
"25w07a",
76+
"25w08a",
77+
"25w09a",
78+
"25w09b",
79+
"25w10a",
80+
"1.21.5-pre1",
81+
"1.21.5-pre2",
82+
"1.21.5-pre3",
83+
"1.21.5-rc1",
84+
"1.21.5-rc2",
85+
"1.21.5",
86+
"25w14craftmine",
87+
"25w15a",
88+
"25w16a",
89+
"25w17a",
90+
"25w18a",
91+
"25w19a",
92+
"25w20a",
93+
"25w21a",
94+
"1.21.6-pre1",
95+
"1.21.6-pre3",
96+
"1.21.6",
97+
"1.21.7-rc1",
98+
"1.21.7",
99+
"1.21.8",
100+
"25w31a",
101+
"25w32a"),
102+
// part of nucledoom
103+
singletonList("1.21.4")
104+
), "1.21.4"
60105
)
61106
);
62107

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

127+
@Test
128+
void testCommandFabric(WireMockRuntimeInfo wmInfo) throws Exception {
129+
130+
stubGetProjects("fabric-api", "nucledoom");
131+
132+
final String out = SystemLambda.tapSystemOut(() -> {
133+
final int exitCode = new CommandLine(new VersionFromModrinthProjectsCommand())
134+
.execute(
135+
"--api-base-url", wmInfo.getHttpBaseUrl(),
136+
"--projects", "fabric-api, nucledoom"
137+
);
138+
139+
assertThat(exitCode)
140+
.isEqualTo(ExitCode.OK);
141+
});
142+
143+
assertThat(out).isEqualToNormalizingNewlines("1.21.4\n");
144+
}
145+
82146
@Test
83147
void testCommandWithProjectQualifiers(WireMockRuntimeInfo wmInfo) throws Exception {
84148

0 commit comments

Comments
 (0)