Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allowUnknownTraits for projection with import #1685

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<ModelAssembler> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to dumpsOutValidationErrorsAndFails above, but with the --allow-unknown-traits option making it succeed.


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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": "1.0",
"projections": {
"exampleProjection": {
"imports": ["unknown-trait.smithy"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}