diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java index 6e17e8df7e7..54d0471f5bb 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/BuildCommand.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; import java.util.function.Consumer; +import java.util.function.Supplier; import software.amazon.smithy.build.FileManifest; import software.amazon.smithy.build.ProjectionResult; import software.amazon.smithy.build.SmithyBuild; @@ -40,6 +41,7 @@ import software.amazon.smithy.cli.Style; import software.amazon.smithy.cli.dependencies.DependencyResolver; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.loader.ModelAssembler; import software.amazon.smithy.model.validation.Severity; final class BuildCommand extends ClasspathCommand { @@ -102,7 +104,14 @@ int runWithClassLoader(SmithyBuildConfig config, Arguments arguments, Env env, L // Configure whether the build is quiet or not based on the --quiet option. Model model = CommandUtils.buildModel(arguments, models, env, env.stderr(), standardOptions.quiet(), config); - SmithyBuild smithyBuild = SmithyBuild.create(classLoader) + Supplier modelAssemblerSupplier = () -> { + ModelAssembler assembler = Model.assembler(classLoader); + if (buildOptions.allowUnknownTraits()) { + assembler.putProperty(ModelAssembler.ALLOW_UNKNOWN_TRAITS, true); + } + return assembler; + }; + SmithyBuild smithyBuild = SmithyBuild.create(classLoader, modelAssemblerSupplier) .config(config) .model(model); diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java index 2efcc3e77d8..6946c3ae4bb 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/BuildCommandTest.java @@ -52,6 +52,16 @@ public void dumpsOutValidationErrorsAndFails() throws Exception { assertThat(result.stderr(), containsString("FAILURE")); } + @Test + public void allowsUnknownTraitWithFlag() throws Exception { + String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString(); + CliUtils.Result result = CliUtils.runSmithy("build", "--allow-unknown-traits", model); + + assertThat(result.code(), equalTo(0)); + assertThat(result.stderr(), containsString("Completed projection source")); + assertThat(result.stderr(), containsString("Smithy built ")); + } + @Test public void printsSuccessfulProjections() throws Exception { String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString(); @@ -74,6 +84,26 @@ public void validationFailuresCausedByProjectionsAreDetected() throws Exception containsString("The following 1 Smithy build projection(s) failed: [exampleProjection]")); } + @Test + public void projectionUnknownTraitsAreDisallowed() throws Exception { + String config = Paths.get(getClass().getResource("projection-model-import.json").toURI()).toString(); + CliUtils.Result result = CliUtils.runSmithy("build", "--config", config); + + assertThat(result.code(), not(0)); + assertThat(result.stderr(), containsString("Unable to resolve trait `some.unknown#trait`")); + assertThat(result.stderr(), containsString("Smithy build projection(s) failed: [exampleProjection]\n")); + } + + @Test + public void projectionUnknownTraitsAreAllowedWithFlag() throws Exception { + String config = Paths.get(getClass().getResource("projection-model-import.json").toURI()).toString(); + CliUtils.Result result = CliUtils.runSmithy("build", "--allow-unknown-traits", "--config", config); + + assertThat(result.code(), equalTo(0)); + assertThat(result.stderr(), containsString("Completed projection exampleProjection")); + assertThat(result.stderr(), containsString("Smithy built ")); + } + @Test public void exceptionsThrownByProjectionsAreDetected() { // TODO: need to make a plugin throw an exception diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/projection-model-import.json b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/projection-model-import.json new file mode 100644 index 00000000000..dd5ce49f170 --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/projection-model-import.json @@ -0,0 +1,8 @@ +{ + "version": "1.0", + "projections": { + "exampleProjection": { + "imports": ["unknown-trait.smithy"] + } + } +}