Skip to content

Commit 357f86c

Browse files
authored
Fix a bug that prevented warning events to show up for diff CLI command (#2749)
* Fix a bug that prevented warning events to show up for diff CLI command The severity is set to DANGER to load the old and new models to avoid events of lesser severity to show up during loading. After this was done the option was mutated and the attempt to restore the default severity didn't work. Now, the validation option keeps a different field to distinguish the value read from the CLI from overrides to be able to tell them apart and properly reset these when needed. * Make spotless happy
1 parent 7ac0887 commit 357f86c

5 files changed

Lines changed: 86 additions & 9 deletions

File tree

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ModelBuilder.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,9 @@ private static void discoverModelsWithClasspath(String rawClasspath, ModelAssemb
298298
}
299299

300300
// Determine a default severity if one wasn't given, by inspecting if there is a --severity option.
301-
private Severity resolveMinSeverity(StandardOptions standardOptions, ValidatorOptions validatorOption) {
302-
if (defaultSeverity != null && validatorOption.severity() == null) {
303-
validatorOption.severity(defaultSeverity);
304-
}
305-
return validatorOption.detectAndGetSeverity(standardOptions);
301+
private void resolveMinSeverity(StandardOptions standardOptions, ValidatorOptions validatorOption) {
302+
validatorOption.severityOverride(defaultSeverity);
303+
validatorOption.detectAndSetSeverity(standardOptions);
306304
}
307305

308306
static ModelAssembler createModelAssembler(ClassLoader classLoader) {

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class ValidatorOptions implements ArgumentReceiver {
2424
static final String SHOW_VALIDATORS = "--show-validators";
2525
static final String HIDE_VALIDATORS = "--hide-validators";
2626

27+
private Severity severityOverride;
2728
private Severity severity;
2829
private List<String> showValidators = Collections.emptyList();
2930
private List<String> hideValidators = Collections.emptyList();
@@ -107,21 +108,28 @@ void severity(Severity severity) {
107108
this.severity = severity;
108109
}
109110

111+
/**
112+
* Set a severity override
113+
*
114+
* @param severity Severity to set.
115+
*/
116+
void severityOverride(Severity severity) {
117+
this.severityOverride = severity;
118+
}
119+
110120
/**
111121
* Set and get the severity level, taking into account standard options that affect the default.
112122
*
113123
* @param standardOptions Standard options to query if no severity is explicitly set.
114-
* @return Returns the resolved severity option.
115124
*/
116-
Severity detectAndGetSeverity(StandardOptions standardOptions) {
125+
void detectAndSetSeverity(StandardOptions standardOptions) {
117126
if (severity == null) {
118127
if (standardOptions.quiet()) {
119128
severity = Severity.DANGER;
120129
} else {
121130
severity = Severity.WARNING;
122131
}
123132
}
124-
return severity;
125133
}
126134

127135
/**
@@ -160,6 +168,18 @@ void hideValidators(List<String> validators) {
160168
this.hideValidators = validators;
161169
}
162170

171+
/**
172+
* Returns the current severity.
173+
*
174+
* @return The validation severity
175+
*/
176+
Severity getSeverity() {
177+
if (severityOverride != null) {
178+
return severityOverride;
179+
}
180+
return severity;
181+
}
182+
163183
/**
164184
* Check if the given validation event matches the show/hide settings.
165185
*
@@ -169,7 +189,8 @@ void hideValidators(List<String> validators) {
169189
* @return Return true if the event can be seen.
170190
*/
171191
boolean isVisible(ValidationEvent event) {
172-
if (event.getSeverity().ordinal() < severity.ordinal()) {
192+
193+
if (event.getSeverity().ordinal() < getSeverity().ordinal()) {
173194
return false;
174195
}
175196

smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,38 @@ public void canOutputCsv() throws Exception {
3838
assertThat(lines[0], containsString("severity,id,shape,file,line,column,message,hint,suppressionReason"));
3939
assertThat(lines[1], containsString("\"ERROR\",\"ChangedShapeType\",\"smithy.example#Hello\""));
4040
}
41+
42+
@Test
43+
public void showsWarningEventsByDefault() throws Exception {
44+
Path oldModel = Paths.get(getClass().getResource("diff/old2.smithy").toURI());
45+
Path newModel = Paths.get(getClass().getResource("diff/new2.smithy").toURI());
46+
CliUtils.Result result = CliUtils.runSmithy("diff",
47+
"--old",
48+
oldModel.toString(),
49+
"--new",
50+
newModel.toString());
51+
52+
assertThat(result.code(), is(0));
53+
54+
String[] lines = result.stdout().split("(\\r\\n|\\r|\\n)");
55+
assertThat(lines[1], containsString("WARNING"));
56+
assertThat(lines[1], containsString("TraitBreakingChange.Add.smithy.api#pattern"));
57+
}
58+
59+
@Test
60+
public void doesNotShowWarningEventsWhenSeverityIsSetToDanger() throws Exception {
61+
Path oldModel = Paths.get(getClass().getResource("diff/old2.smithy").toURI());
62+
Path newModel = Paths.get(getClass().getResource("diff/new2.smithy").toURI());
63+
CliUtils.Result result = CliUtils.runSmithy("diff",
64+
// warning events won't be shown in the output
65+
"--severity",
66+
"DANGER",
67+
"--old",
68+
oldModel.toString(),
69+
"--new",
70+
newModel.toString());
71+
72+
assertThat(result.code(), is(0));
73+
assertThat(result.stdout(), is(""));
74+
}
4175
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
$version: "2.0"
2+
3+
namespace smithy.example
4+
5+
6+
structure StructureOne {
7+
8+
@pattern("^.*$")
9+
stringMember: String
10+
11+
@clientOptional
12+
@required
13+
intMember: Integer
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$version: "2.0"
2+
3+
namespace smithy.example
4+
5+
structure StructureOne {
6+
7+
stringMember: String
8+
9+
intMember: Integer
10+
}

0 commit comments

Comments
 (0)