Skip to content

Commit 1b6a09b

Browse files
authored
users: log warning if attempting to sync from same input file as output (#462)
1 parent a818c94 commit 1b6a09b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/main/java/me/itzg/helpers/users/ManageUsersCommand.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,16 @@ private void processInputAsFile(SharedFetch sharedFetch, String filePathUrl) thr
313313
}
314314
}
315315
else {
316-
log.debug("Copying from {} to {}", filePathUrl, outputFile);
316+
final Path inputFile = Paths.get(filePathUrl);
317317

318-
Files.copy(Paths.get(filePathUrl), outputFile, StandardCopyOption.REPLACE_EXISTING);
318+
if (!Files.exists(outputFile) || !Files.isSameFile(inputFile, outputFile)) {
319+
log.debug("Copying from {} to {}", filePathUrl, outputFile);
320+
Files.copy(inputFile, outputFile, StandardCopyOption.REPLACE_EXISTING);
321+
}
322+
else {
323+
log.warn("Attempting to synchronize input {} file onto itself: {}",
324+
type, outputFile);
325+
}
319326
}
320327
}
321328

src/test/java/me/itzg/helpers/users/ManageUsersCommandTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,28 @@ void remoteFile(Type type, WireMockRuntimeInfo wmInfo) {
737737

738738
assertThat(expectedFile).hasContent(sourceContent);
739739
}
740+
741+
@Test
742+
void handlesSameInputFileAsOutput(WireMockRuntimeInfo wmInfo) throws IOException {
743+
@Language("JSON") final String sourceContent = "[{\"name\": \"testing\", \"uuid\": \"dec16109-30d4-4425-bcc1-5222255eb6b0\"}]";
744+
745+
final Path inputFile = Files.write(tempDir.resolve("ops.json"),
746+
Collections.singletonList(sourceContent));
747+
748+
final int exitCode = new CommandLine(
749+
new ManageUsersCommand()
750+
)
751+
.execute(
752+
"--mojang-api-base-url", wmInfo.getHttpBaseUrl(),
753+
"--user-api-provider", "mojang",
754+
"--type", Type.JAVA_OPS.name(),
755+
"--output-directory", tempDir.toString(),
756+
"--input-is-file",
757+
inputFile.toString()
758+
);
759+
760+
assertThat(exitCode).isEqualTo(ExitCode.OK);
761+
}
740762
}
741763

742764
private static void setupUserStubs() {

0 commit comments

Comments
 (0)