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 1 commit
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 @@ -70,7 +70,23 @@ public SmithyBuild(SmithyBuildConfig config) {
* @return Returns the created {@code SmithyBuild} object.
*/
public static SmithyBuild create(ClassLoader classLoader) {
return create(classLoader, false);
}

/**
* Creates a {@code SmithyBuild} implementation that is configured to
* discover various Smithy service providers using the given
* {@code ClassLoader}.
*
* @param classLoader ClassLoader used to discover service providers.
* @param allowUnknownTraits Set to true to allow unknown traits.
* @return Returns the created {@code SmithyBuild} object.
*/
public static SmithyBuild create(ClassLoader classLoader, boolean allowUnknownTraits) {
ModelAssembler assembler = Model.assembler(classLoader);
if (allowUnknownTraits) {
assembler.putProperty(ModelAssembler.ALLOW_UNKNOWN_TRAITS, true);
}
return create(classLoader, assembler::copy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ 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)
SmithyBuild smithyBuild = SmithyBuild.create(classLoader, buildOptions.allowUnknownTraits())
.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.

}
}
}