diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ModelBuilder.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ModelBuilder.java index 5198c26814e..9adc6bda1f2 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ModelBuilder.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ModelBuilder.java @@ -298,11 +298,9 @@ private static void discoverModelsWithClasspath(String rawClasspath, ModelAssemb } // Determine a default severity if one wasn't given, by inspecting if there is a --severity option. - private Severity resolveMinSeverity(StandardOptions standardOptions, ValidatorOptions validatorOption) { - if (defaultSeverity != null && validatorOption.severity() == null) { - validatorOption.severity(defaultSeverity); - } - return validatorOption.detectAndGetSeverity(standardOptions); + private void resolveMinSeverity(StandardOptions standardOptions, ValidatorOptions validatorOption) { + validatorOption.severityOverride(defaultSeverity); + validatorOption.detectAndSetSeverity(standardOptions); } static ModelAssembler createModelAssembler(ClassLoader classLoader) { diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java index 3d15694d3ec..663741f1408 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/ValidatorOptions.java @@ -24,6 +24,7 @@ final class ValidatorOptions implements ArgumentReceiver { static final String SHOW_VALIDATORS = "--show-validators"; static final String HIDE_VALIDATORS = "--hide-validators"; + private Severity severityOverride; private Severity severity; private List showValidators = Collections.emptyList(); private List hideValidators = Collections.emptyList(); @@ -107,13 +108,21 @@ void severity(Severity severity) { this.severity = severity; } + /** + * Set a severity override + * + * @param severity Severity to set. + */ + void severityOverride(Severity severity) { + this.severityOverride = severity; + } + /** * Set and get the severity level, taking into account standard options that affect the default. * * @param standardOptions Standard options to query if no severity is explicitly set. - * @return Returns the resolved severity option. */ - Severity detectAndGetSeverity(StandardOptions standardOptions) { + void detectAndSetSeverity(StandardOptions standardOptions) { if (severity == null) { if (standardOptions.quiet()) { severity = Severity.DANGER; @@ -121,7 +130,6 @@ Severity detectAndGetSeverity(StandardOptions standardOptions) { severity = Severity.WARNING; } } - return severity; } /** @@ -160,6 +168,18 @@ void hideValidators(List validators) { this.hideValidators = validators; } + /** + * Returns the current severity. + * + * @return The validation severity + */ + Severity getSeverity() { + if (severityOverride != null) { + return severityOverride; + } + return severity; + } + /** * Check if the given validation event matches the show/hide settings. * @@ -169,7 +189,8 @@ void hideValidators(List validators) { * @return Return true if the event can be seen. */ boolean isVisible(ValidationEvent event) { - if (event.getSeverity().ordinal() < severity.ordinal()) { + + if (event.getSeverity().ordinal() < getSeverity().ordinal()) { return false; } diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.java index 76c2bb5cd62..435e57060bc 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.java @@ -38,4 +38,38 @@ public void canOutputCsv() throws Exception { assertThat(lines[0], containsString("severity,id,shape,file,line,column,message,hint,suppressionReason")); assertThat(lines[1], containsString("\"ERROR\",\"ChangedShapeType\",\"smithy.example#Hello\"")); } + + @Test + public void showsWarningEventsByDefault() throws Exception { + Path oldModel = Paths.get(getClass().getResource("diff/old2.smithy").toURI()); + Path newModel = Paths.get(getClass().getResource("diff/new2.smithy").toURI()); + CliUtils.Result result = CliUtils.runSmithy("diff", + "--old", + oldModel.toString(), + "--new", + newModel.toString()); + + assertThat(result.code(), is(0)); + + String[] lines = result.stdout().split("(\\r\\n|\\r|\\n)"); + assertThat(lines[1], containsString("WARNING")); + assertThat(lines[1], containsString("TraitBreakingChange.Add.smithy.api#pattern")); + } + + @Test + public void doesNotShowWarningEventsWhenSeverityIsSetToDanger() throws Exception { + Path oldModel = Paths.get(getClass().getResource("diff/old2.smithy").toURI()); + Path newModel = Paths.get(getClass().getResource("diff/new2.smithy").toURI()); + CliUtils.Result result = CliUtils.runSmithy("diff", + // warning events won't be shown in the output + "--severity", + "DANGER", + "--old", + oldModel.toString(), + "--new", + newModel.toString()); + + assertThat(result.code(), is(0)); + assertThat(result.stdout(), is("")); + } } diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/new2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/new2.smithy new file mode 100644 index 00000000000..0708f41668c --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/new2.smithy @@ -0,0 +1,14 @@ +$version: "2.0" + +namespace smithy.example + + +structure StructureOne { + + @pattern("^.*$") + stringMember: String + + @clientOptional + @required + intMember: Integer +} diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/old2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/old2.smithy new file mode 100644 index 00000000000..4a93ce012b5 --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/old2.smithy @@ -0,0 +1,10 @@ +$version: "2.0" + +namespace smithy.example + +structure StructureOne { + + stringMember: String + + intMember: Integer +}