-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ast
command and indent JSON 4 spaces
This commit adds support for an `ast` command in the CLI to convert 0 or more Smithy models into a JSON AST. This can be used to more easily combine multiple models or for simply testing parser implementations. This change also updates the default indentation for JSON pretty printing to use 4 spaces instead of 2 since that's what is used throughout the Smithy specification.
- Loading branch information
Showing
7 changed files
with
172 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
smithy-cli/src/main/java/software/amazon/smithy/cli/commands/AstCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.cli.commands; | ||
|
||
import software.amazon.smithy.cli.Arguments; | ||
import software.amazon.smithy.cli.Cli; | ||
import software.amazon.smithy.cli.Command; | ||
import software.amazon.smithy.cli.Parser; | ||
import software.amazon.smithy.cli.SmithyCli; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.ModelSerializer; | ||
import software.amazon.smithy.utils.SetUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
@SmithyInternalApi | ||
public final class AstCommand implements Command { | ||
@Override | ||
public String getName() { | ||
return "ast"; | ||
} | ||
|
||
@Override | ||
public String getSummary() { | ||
return "Reads Smithy models in and writes out a single JSON AST model"; | ||
} | ||
|
||
@Override | ||
public Parser getParser() { | ||
return Parser.builder() | ||
.option(SmithyCli.ALLOW_UNKNOWN_TRAITS, "Ignores unknown traits when validating models") | ||
.option(SmithyCli.DISCOVER, "-d", "Enables model discovery, merging in models found inside of jars") | ||
.parameter(SmithyCli.DISCOVER_CLASSPATH, "Enables model discovery using a custom classpath for models") | ||
.positional("<MODELS>", "Path to Smithy models or directories") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void execute(Arguments arguments, ClassLoader classLoader) { | ||
// Don't write the summary to STDOUT, but do write errors to STDERR. | ||
Model model = CommandUtils.buildModel(arguments, classLoader, SetUtils.of(Validator.Feature.QUIET)); | ||
ModelSerializer serializer = ModelSerializer.builder().build(); | ||
Cli.stdout(Node.prettyPrintJson(serializer.serialize(model))); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
smithy-cli/src/test/java/software/amazon/smithy/cli/commands/AstCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package software.amazon.smithy.cli.commands; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.cli.CliError; | ||
import software.amazon.smithy.cli.SmithyCli; | ||
|
||
public class AstCommandTest { | ||
@Test | ||
public void hasAstCommand() throws Exception { | ||
PrintStream out = System.out; | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
PrintStream printStream = new PrintStream(outputStream); | ||
System.setOut(printStream); | ||
SmithyCli.create().run("ast", "--help"); | ||
System.setOut(out); | ||
String help = outputStream.toString("UTF-8"); | ||
|
||
assertThat(help, containsString("Reads")); | ||
} | ||
|
||
@Test | ||
public void usesModelDiscoveryWithCustomValidClasspath() throws Exception { | ||
PrintStream out = System.out; | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
PrintStream printStream = new PrintStream(outputStream); | ||
System.setOut(printStream); | ||
|
||
String dir = getClass().getResource("valid.jar").getPath(); | ||
SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir); | ||
System.setOut(out); | ||
|
||
String output = outputStream.toString("UTF-8"); | ||
assertThat(output, containsString("{")); | ||
} | ||
|
||
@Test | ||
public void usesModelDiscoveryWithCustomInvalidClasspath() { | ||
CliError e = Assertions.assertThrows(CliError.class, () -> { | ||
String dir = getClass().getResource("invalid.jar").getPath(); | ||
SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir); | ||
}); | ||
|
||
assertThat(e.getMessage(), containsString("1 ERROR(s)")); | ||
} | ||
|
||
@Test | ||
public void failsOnUnknownTrait() { | ||
CliError e = Assertions.assertThrows(CliError.class, () -> { | ||
String model = getClass().getResource("unknown-trait.smithy").getPath(); | ||
SmithyCli.create().run("ast", model); | ||
}); | ||
|
||
assertThat(e.getMessage(), containsString("1 ERROR(s)")); | ||
} | ||
|
||
@Test | ||
public void allowsUnknownTrait() { | ||
String model = getClass().getResource("unknown-trait.smithy").getPath(); | ||
SmithyCli.create().run("ast", "--allow-unknown-traits", model); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters