Skip to content

Commit 21976be

Browse files
authored
Support dry-run and build-environment flags in Nomulus creation commands (#3149)
When checking in a brand new premium list (production/*.txt) or reserved list (reserved/production/*.txt), presubmit testing commands in gradle-runner.sh fall back from update_premium_list / update_reserved_list (which require existing database entities) to create_premium_list / create_reserved_list. However, because --dry_run (-d) and --build_environment flags were previously only defined in UpdatePremiumListCommand and UpdateReservedListCommand, passing them during non-interactive presubmit creation validation resulted in ParameterException (Was passed main parameter '-d' but no main parameter was defined...) or NullPointerException when ConfirmingCommand attempted to prompt on a null system console. This change moves --dry_run and --build_environment parameters and dontRunCommand() behavior up into CreateOrUpdatePremiumListCommand and CreateOrUpdateReservedListCommand, enabling safe dry-run presubmit validation for newly created lists in CI environments. BUG= http://b/534511633, http://b/529397845
1 parent c2bd11c commit 21976be

8 files changed

Lines changed: 71 additions & 42 deletions

‎core/src/main/java/google/registry/tools/CreateOrUpdatePremiumListCommand.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package google.registry.tools;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
1718
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
1819

1920
import com.beust.jcommander.Parameter;
@@ -39,6 +40,19 @@ abstract class CreateOrUpdatePremiumListCommand extends ConfirmingCommand {
3940
protected List<String> inputData;
4041
protected CurrencyUnit currency;
4142

43+
@Parameter(
44+
names = {"-d", "--dry_run"},
45+
description = "Does not execute the entity mutation")
46+
boolean dryRun;
47+
48+
@Parameter(
49+
names = {"--build_environment"},
50+
description =
51+
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
52+
+ " by the build environment tools. This flag should never be used by a human user"
53+
+ " from the command line.")
54+
boolean buildEnv;
55+
4256
@Nullable
4357
@Parameter(
4458
names = {"-n", "--name"},
@@ -68,4 +82,17 @@ public String execute() throws Exception {
6882
}
6983
return message;
7084
}
85+
86+
@Override
87+
protected boolean dontRunCommand() {
88+
return dryRun;
89+
}
90+
91+
@Override
92+
protected boolean checkExecutionState() {
93+
checkArgument(
94+
!RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
95+
"The --build_environment flag must be used when running in production");
96+
return super.checkExecutionState();
97+
}
7198
}

‎core/src/main/java/google/registry/tools/CreateOrUpdateReservedListCommand.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package google.registry.tools;
1616

17+
import static com.google.common.base.Preconditions.checkArgument;
18+
1719
import com.beust.jcommander.Parameter;
1820
import com.google.common.flogger.FluentLogger;
1921
import google.registry.model.tld.label.ReservedList;
@@ -35,6 +37,19 @@ public abstract class CreateOrUpdateReservedListCommand extends ConfirmingComman
3537

3638
static final FluentLogger logger = FluentLogger.forEnclosingClass();
3739

40+
@Parameter(
41+
names = {"-d", "--dry_run"},
42+
description = "Does not execute the entity mutation")
43+
boolean dryRun;
44+
45+
@Parameter(
46+
names = {"--build_environment"},
47+
description =
48+
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
49+
+ " by the build environment tools. This flag should never be used by a human user"
50+
+ " from the command line.")
51+
boolean buildEnv;
52+
3853
@Nullable
3954
@Parameter(
4055
names = {"-n", "--name"},
@@ -74,4 +89,17 @@ String outputReservedListEntries(ReservedList rl) {
7489
.collect(Collectors.joining(", "))
7590
+ "]";
7691
}
92+
93+
@Override
94+
protected boolean dontRunCommand() {
95+
return dryRun;
96+
}
97+
98+
@Override
99+
protected boolean checkExecutionState() {
100+
checkArgument(
101+
!RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
102+
"The --build_environment flag must be used when running in production");
103+
return super.checkExecutionState();
104+
}
77105
}

‎core/src/main/java/google/registry/tools/UpdatePremiumListCommand.java‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static google.registry.util.ListNamingUtils.convertFilePathToName;
1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

21-
import com.beust.jcommander.Parameter;
2221
import com.beust.jcommander.Parameters;
2322
import com.google.common.base.Strings;
2423
import google.registry.model.tld.label.PremiumList;
@@ -30,27 +29,11 @@
3029
@Parameters(separators = " =", commandDescription = "Update a PremiumList in Database.")
3130
class UpdatePremiumListCommand extends CreateOrUpdatePremiumListCommand {
3231

33-
@Parameter(
34-
names = {"-d", "--dry_run"},
35-
description = "Does not execute the entity mutation")
36-
boolean dryRun;
37-
38-
@Parameter(
39-
names = {"--build_environment"},
40-
description =
41-
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
42-
+ " by the build environment tools. This flag should never be used by a human user"
43-
+ " from the command line.")
44-
boolean buildEnv;
45-
4632
// Indicates if there is a new change made by this command
4733
private boolean newChange = false;
4834

4935
@Override
5036
protected String prompt() throws Exception {
51-
checkArgument(
52-
!RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
53-
"The --build_environment flag must be used when running update_premium_list in production");
5437
name = Strings.isNullOrEmpty(name) ? convertFilePathToName(inputFile) : name;
5538
PremiumList existingList =
5639
PremiumListDao.getLatestRevision(name)

‎core/src/main/java/google/registry/tools/UpdateReservedListCommand.java‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
package google.registry.tools;
1616

17-
import static com.google.common.base.Preconditions.checkArgument;
1817
import static google.registry.util.DiffUtils.prettyPrintEntityDeepDiff;
1918
import static google.registry.util.ListNamingUtils.convertFilePathToName;
2019
import static java.nio.charset.StandardCharsets.UTF_8;
2120

22-
import com.beust.jcommander.Parameter;
2321
import com.beust.jcommander.Parameters;
2422
import com.google.common.base.Strings;
2523
import google.registry.model.tld.label.ReservedList;
@@ -30,28 +28,11 @@
3028
@Parameters(separators = " =", commandDescription = "Update a ReservedList.")
3129
final class UpdateReservedListCommand extends CreateOrUpdateReservedListCommand {
3230

33-
@Parameter(
34-
names = {"-d", "--dry_run"},
35-
description = "Does not execute the entity mutation")
36-
boolean dryRun;
37-
38-
@Parameter(
39-
names = {"--build_environment"},
40-
description =
41-
"DO NOT USE THIS FLAG ON THE COMMAND LINE! This flag indicates the command is being run"
42-
+ " by the build environment tools. This flag should never be used by a human user"
43-
+ " from the command line.")
44-
boolean buildEnv;
45-
4631
// indicates if there is a new change made by this command
4732
private boolean newChange = true;
4833

4934
@Override
5035
protected String prompt() throws Exception {
51-
checkArgument(
52-
!RegistryToolEnvironment.get().equals(RegistryToolEnvironment.PRODUCTION) || buildEnv,
53-
"The --build_environment flag must be used when running update_reserved_list in"
54-
+ " production");
5536
name = Strings.isNullOrEmpty(name) ? convertFilePathToName(input) : name;
5637
ReservedList existingReservedList =
5738
ReservedList.get(name)

‎core/src/test/java/google/registry/tools/CreatePremiumListCommandTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,11 @@ void commandPrompt_failureMismatchedTldName_noOverride() {
119119
+ "yet TLD %s does not exist",
120120
fileName));
121121
}
122+
123+
@Test
124+
void testDryRun_doesNotCreateList() throws Exception {
125+
runCommandForced(
126+
"--name=" + TLD_TEST, "--input=" + premiumTermsPath, "--currency=USD", "--dry_run");
127+
assertThat(PremiumListDao.getLatestRevision(TLD_TEST)).isEmpty();
128+
}
122129
}

‎core/src/test/java/google/registry/tools/CreateReservedListCommandTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,11 @@ void testStageEntityChange_succeedsWithEmptyFile() throws Exception {
180180
command.init();
181181
assertThat(command.prompt()).contains("reservedListMap=[]");
182182
}
183+
184+
@Test
185+
void testDryRun_doesNotCreateList() throws Exception {
186+
runCommandForced(
187+
"--name=xn--q9jyb4c_common-reserved", "--input=" + reservedTermsPath, "--dry_run");
188+
assertThat(ReservedList.get("xn--q9jyb4c_common-reserved")).isEmpty();
189+
}
183190
}

‎core/src/test/java/google/registry/tools/UpdatePremiumListCommandTest.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,7 @@ void testFailure_runCommandOnProduction_noFlag() throws Exception {
209209
"--name=" + TLD_TEST,
210210
"--input=" + Paths.get(tmpFile.getPath())));
211211
assertThat(thrown.getMessage())
212-
.isEqualTo(
213-
"The --build_environment flag must be used when running update_premium_list in"
214-
+ " production");
212+
.isEqualTo("The --build_environment flag must be used when running in production");
215213
}
216214

217215
@Test

‎core/src/test/java/google/registry/tools/UpdateReservedListCommandTest.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ void testFailure_runCommandOnProduction_noFlag() throws Exception {
139139
"--name=xn--q9jyb4c_common-reserved",
140140
"--input=" + reservedTermsPath));
141141
assertThat(thrown.getMessage())
142-
.isEqualTo(
143-
"The --build_environment flag must be used when running update_reserved_list in"
144-
+ " production");
142+
.isEqualTo("The --build_environment flag must be used when running in production");
145143
}
146144

147145
@Test

0 commit comments

Comments
 (0)