33import static com .github .stefanbirkner .systemlambda .SystemLambda .tapSystemErrNormalized ;
44import static com .github .tomakehurst .wiremock .client .WireMock .*;
55import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
6- import static org .apache .commons .lang3 .RandomStringUtils .randomAlphabetic ;
7- import static org .apache .commons .lang3 .RandomStringUtils .randomAlphanumeric ;
86import static org .assertj .core .api .Assertions .assertThat ;
97
108import com .fasterxml .jackson .core .JsonProcessingException ;
2422import me .itzg .helpers .modrinth .ModrinthCommand .DownloadDependencies ;
2523import me .itzg .helpers .modrinth .model .Project ;
2624import me .itzg .helpers .modrinth .model .ProjectType ;
25+ import org .apache .commons .lang3 .RandomStringUtils ;
2726import org .assertj .core .api .AbstractPathAssert ;
2827import org .jetbrains .annotations .NotNull ;
2928import org .junit .jupiter .api .Test ;
@@ -48,6 +47,8 @@ class ModrinthCommandTest {
4847 .configureStaticDsl (true )
4948 .build ();
5049
50+ private final RandomStringUtils randomStringUtils = RandomStringUtils .insecure ();
51+
5152 @ Test
5253 void commaNewlineDelimited (@ TempDir Path tempDir ) {
5354 setupStubs ();
@@ -153,6 +154,15 @@ else if (downloadDependencies == DownloadDependencies.OPTIONAL) {
153154 }
154155 }
155156
157+ private String randomAlphanumeric (int n ) {
158+ return randomStringUtils .nextAlphanumeric (n );
159+ }
160+
161+ @ SuppressWarnings ("SameParameterValue" )
162+ private String randomAlphabetic (int n ) {
163+ return randomStringUtils .nextAlphabetic (n );
164+ }
165+
156166 @ Test
157167 void failsWhenNoDependenciesForModLoader (@ TempDir Path tempDir ) throws JsonProcessingException {
158168 final String projectId = randomAlphanumeric (6 );
@@ -162,11 +172,11 @@ void failsWhenNoDependenciesForModLoader(@TempDir Path tempDir) throws JsonProce
162172
163173 stubProjectBulkRequest (projectId , projectSlug );
164174
165- stubVersionRequest (projectId , versionId , deps -> {
175+ stubVersionRequest (projectId , versionId , deps ->
166176 deps .addObject ()
167- .put ("project_id" , requiredDepProjectId )
168- .put ("dependency_type" , "required" );
169- } );
177+ .put ("project_id" , requiredDepProjectId )
178+ .put ("dependency_type" , "required" )
179+ );
170180 stubVersionRequestEmptyResponse (requiredDepProjectId , "paper" );
171181 stubVersionRequestEmptyResponse (requiredDepProjectId , "spigot" );
172182 stubGetProject (requiredDepProjectId , new Project ().setProjectType (ProjectType .resourcepack ));
@@ -277,7 +287,6 @@ void handlesDatapacksSpecificVersion(boolean absoluteWorldDir, @TempDir Path tem
277287
278288 stubProjectBulkRequest (projectId , projectSlug );
279289
280- final ArrayNode versionResp = objectMapper .createArrayNode ();
281290 final ObjectNode versionNode = objectMapper .createObjectNode ()
282291 .put ("id" , versionId )
283292 .put ("project_id" , projectId )
@@ -412,6 +421,51 @@ void usingListingFile(@TempDir Path tempDir) throws Exception {
412421 assertThat (tempDir .resolve ("mods/cloth-config-8.3.103-fabric.jar" )).exists ();
413422 }
414423
424+ @ Test
425+ void removesAllWhenEmpty (@ TempDir Path tempDir ) {
426+ setupStubs ();
427+
428+ {
429+ final int exitCode = new CommandLine (
430+ new ModrinthCommand ()
431+ )
432+ .execute (
433+ "--api-base-url" , wm .getRuntimeInfo ().getHttpBaseUrl (),
434+ "--output-directory" , tempDir .toString (),
435+ "--game-version" , "1.19.2" ,
436+ "--loader" , "fabric" ,
437+ "--projects" , "fabric-api,cloth-config"
438+ );
439+
440+ assertThat (exitCode ).isEqualTo (ExitCode .OK );
441+
442+ assertThat (tempDir .resolve ("mods/fabric-api-0.76.1+1.19.2.jar" )).exists ();
443+ assertThat (tempDir .resolve ("mods/cloth-config-8.3.103-fabric.jar" )).exists ();
444+ }
445+
446+ // now process an empty projects list to uninstall all
447+ {
448+ final int exitCode = new CommandLine (
449+ new ModrinthCommand ()
450+ )
451+ .execute (
452+ "--api-base-url" , wm .getRuntimeInfo ().getHttpBaseUrl (),
453+ "--output-directory" , tempDir .toString (),
454+ "--game-version" , "1.19.2" ,
455+ "--loader" , "fabric" ,
456+ "--projects" , ""
457+ );
458+
459+ assertThat (exitCode ).isEqualTo (ExitCode .OK );
460+
461+ // assert files now do NOT exist
462+ assertThat (tempDir .resolve ("mods/fabric-api-0.76.1+1.19.2.jar" ))
463+ .doesNotExist ();
464+ assertThat (tempDir .resolve ("mods/cloth-config-8.3.103-fabric.jar" ))
465+ .doesNotExist ();
466+ }
467+ }
468+
415469 @ NotNull
416470 private static RequestPatternBuilder projectVersionsRequest (String projectId ) {
417471 return getRequestedFor (urlPathEqualTo ("/v2/project/" + projectId + "/version" ));
0 commit comments