diff --git a/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java b/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java index 2d9cc4eae..489a59ac4 100644 --- a/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java +++ b/core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java @@ -35,11 +35,24 @@ class UpdatePremiumListCommand extends CreateOrUpdatePremiumListCommand { description = "Does not execute the entity mutation") boolean dryRun; - // indicates if there is a new change made by this command + @Parameter( + names = {"--build_environment"}, + description = + "DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run" + + " by the build environment tools. This flag should never be used by a human user" + + " from the command line.") + boolean buildEnv; + + // Indicates if there is a new change made by this command private boolean newChange = false; @Override protected String prompt() throws Exception { + // TODO(sarahbot): uncomment once go/r3pr/2292 is deployed + // checkArgument( + // !RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv, + // "The --build_environment flag must be used when running update_premium_list in + // production"); name = Strings.isNullOrEmpty(name) ? convertFilePathToName(inputFile) : name; PremiumList existingList = PremiumListDao.getLatestRevision(name) diff --git a/core/src/main/java/google/registry/tools/UpdateReservedListCommand.java b/core/src/main/java/google/registry/tools/UpdateReservedListCommand.java index c712046ff..d86e5943e 100644 --- a/core/src/main/java/google/registry/tools/UpdateReservedListCommand.java +++ b/core/src/main/java/google/registry/tools/UpdateReservedListCommand.java @@ -18,6 +18,7 @@ import static google.registry.util.DiffUtils.prettyPrintEntityDeepDiff; import static google.registry.util.ListNamingUtils.convertFilePathToName; import static java.nio.charset.StandardCharsets.UTF_8; +import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.common.base.Strings; import google.registry.model.tld.label.ReservedList; @@ -28,8 +29,29 @@ import java.util.List; @Parameters(separators = " =", commandDescription = "Update a ReservedList.") final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand { + @Parameter( + names = {"-d", "--dry_run"}, + description = "Does not execute the entity mutation") + boolean dryRun; + + @Parameter( + names = {"--build_environment"}, + description = + "DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run" + + " by the build environment tools. This flag should never be used by a human user" + + " from the command line.") + boolean buildEnv; + + // indicates if there is a new change made by this command + private boolean newChange = true; + @Override protected String prompt() throws Exception { + // TODO(sarahbot): uncomment once go/r3pr/2292 is deployed + // checkArgument( + // !RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv, + // "The --build_environment flag must be used when running update_reserved_list in" + // + " production"); name = Strings.isNullOrEmpty(name) ? convertFilePathToName(input) : name; ReservedList existingReservedList = ReservedList.get(name) @@ -54,6 +76,7 @@ final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand .getReservedListEntries() .equals(reservedList.getReservedListEntries()); if (!shouldPublishChanged && !reservedListEntriesChanged) { + newChange = false; return "No entity changes to apply."; } String result = String.format("Update reserved list for %s?\n", name); @@ -70,4 +93,9 @@ final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand } return result; } + + @Override + protected boolean dontRunCommand() { + return dryRun || !newChange; + } } diff --git a/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java b/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java index d043a7f2e..2bce908d6 100644 --- a/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java +++ b/core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java @@ -205,4 +205,40 @@ class UpdatePremiumListCommandTest .comparingElementsUsing(immutableObjectCorrespondence("revisionId")) .containsExactly(PremiumEntry.create(0L, new BigDecimal("9090.00"), "doge")); } + + // TODO(sarahbot): uncomment once go/r3pr/2292 is deployed + // @Test + // void testFailure_runCommandOnProduction_noFlag() throws Exception { + // File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile(); + // String newPremiumListData = "eth,USD 9999"; + // Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData); + // IllegalArgumentException thrown = + // assertThrows( + // IllegalArgumentException.class, + // () -> + // runCommandInEnvironment( + // RegistryToolEnvironment.PRODUCTION, + // "--name=" + TLD_TEST, + // "--input=" + Paths.get(tmpFile.getPath()))); + // assertThat(thrown.getMessage()) + // .isEqualTo( + // "The --build_environment flag must be used when running update_premium_list in" + // + " production"); + // } + // + // @Test + // void testSuccess_runCommandOnProduction_buildEnvFlag() throws Exception { + // File tmpFile = tmpDir.resolve(String.format("%s.txt", TLD_TEST)).toFile(); + // String newPremiumListData = "eth,USD 9999"; + // Files.asCharSink(tmpFile, UTF_8).write(newPremiumListData); + // runCommandInEnvironment( + // RegistryToolEnvironment.PRODUCTION, + // "--name=" + TLD_TEST, + // "--input=" + Paths.get(tmpFile.getPath()), + // "--build_environment", + // "-f"); + // assertThat(PremiumListDao.loadAllPremiumEntries(TLD_TEST)) + // .comparingElementsUsing(immutableObjectCorrespondence("revisionId")) + // .containsExactly(PremiumEntry.create(0L, new BigDecimal("9999.00"), "eth")); + // } } diff --git a/core/src/test/java/google/registry/tools/UpdateReservedListCommandTest.java b/core/src/test/java/google/registry/tools/UpdateReservedListCommandTest.java index be3c05dff..5348425ea 100644 --- a/core/src/test/java/google/registry/tools/UpdateReservedListCommandTest.java +++ b/core/src/test/java/google/registry/tools/UpdateReservedListCommandTest.java @@ -142,4 +142,51 @@ class UpdateReservedListCommandTest assertThat(command.prompt()).contains("baddies: null -> baddies,FULLY_BLOCKED"); assertThat(command.prompt()).contains("ford: null -> ford,FULLY_BLOCKED # random comment"); } + + @Test + void testSuccess_dryRun() throws Exception { + runCommandForced("--input=" + reservedTermsPath, "--dry_run"); + assertThat(command.prompt()).contains("Update reserved list for xn--q9jyb4c_common-reserved?"); + assertThat(ReservedList.get("xn--q9jyb4c_common-reserved")).isPresent(); + ReservedList reservedList = ReservedList.get("xn--q9jyb4c_common-reserved").get(); + assertThat(reservedList.getReservedListEntries()).hasSize(1); + assertThat(reservedList.getReservationInList("helicopter")).hasValue(FULLY_BLOCKED); + } + + // TODO(sarahbot): uncomment once go/r3pr/2292 is deployed + // @Test + // void testFailure_runCommandOnProduction_noFlag() throws Exception { + // IllegalArgumentException thrown = + // assertThrows( + // IllegalArgumentException.class, + // () -> + // runCommandInEnvironment( + // RegistryToolEnvironment.PRODUCTION, + // "--name=xn--q9jyb4c_common-reserved", + // "--input=" + reservedTermsPath)); + // assertThat(thrown.getMessage()) + // .isEqualTo( + // "The --build_environment flag must be used when running update_reserved_list in" + // + " production"); + // } + // + // @Test + // void testSuccess_runCommandOnProduction_buildEnvFlag() throws Exception { + // runCommandInEnvironment( + // RegistryToolEnvironment.PRODUCTION, + // "--name=xn--q9jyb4c_common-reserved", + // "--input=" + reservedTermsPath, + // "--build_environment", + // "-f"); + // assertThat(ReservedList.get("xn--q9jyb4c_common-reserved")).isPresent(); + // ReservedList reservedList = ReservedList.get("xn--q9jyb4c_common-reserved").get(); + // assertThat(reservedList.getReservedListEntries()).hasSize(2); + // assertThat(reservedList.getReservationInList("baddies")).hasValue(FULLY_BLOCKED); + // assertThat(reservedList.getReservationInList("ford")).hasValue(FULLY_BLOCKED); + // assertThat(reservedList.getReservationInList("helicopter")).isEmpty(); + // assertInStdout("Update reserved list for xn--q9jyb4c_common-reserved?"); + // assertInStdout("helicopter: helicopter,FULLY_BLOCKED -> null"); + // assertInStdout("baddies: null -> baddies,FULLY_BLOCKED"); + // assertInStdout("ford: null -> ford,FULLY_BLOCKED # random comment"); + // } }