-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add csv output format to validate and diff command
You can now pass `--format text|csv` to the validate and diff commands to get CSV or text output. Text continues to be the default when not specified. This commit also updates the diff command to now correctly output events to stdout rather than stderr.
- Loading branch information
Showing
9 changed files
with
245 additions
and
3 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
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
97 changes: 97 additions & 0 deletions
97
...y-cli/src/main/java/software/amazon/smithy/cli/commands/ValidationEventFormatOptions.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,97 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.cli.commands; | ||
|
||
import java.util.function.Consumer; | ||
import software.amazon.smithy.cli.ArgumentReceiver; | ||
import software.amazon.smithy.cli.CliError; | ||
import software.amazon.smithy.cli.CliPrinter; | ||
import software.amazon.smithy.cli.HelpPrinter; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
import software.amazon.smithy.model.validation.ValidationEventFormatter; | ||
|
||
final class ValidationEventFormatOptions implements ArgumentReceiver { | ||
|
||
private static final String VALIDATION_FORMAT = "--format"; | ||
|
||
enum Format { | ||
TEXT { | ||
@Override | ||
void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event) { | ||
printer.println(formatter.format(event)); | ||
} | ||
}, | ||
|
||
CSV { | ||
@Override | ||
void beginPrinting(CliPrinter printer) { | ||
printer.println("severity,id,shape,file,message,hint,suppressionReason"); | ||
} | ||
|
||
@Override | ||
void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event) { | ||
printer.println( | ||
String.format("\"%s\",\"%s\",\"%s\",\"%s\",%d,%d,\"%s\",\"%s\",\"%s\"", | ||
event.getSeverity().toString(), | ||
formatCsv(event.getId()), | ||
event.getShapeId().map(ShapeId::toString).orElse(""), | ||
formatCsv(event.getSourceLocation().getFilename()), | ||
event.getSourceLocation().getLine(), | ||
event.getSourceLocation().getColumn(), | ||
formatCsv(event.getMessage()), | ||
formatCsv(event.getHint().orElse("")), | ||
formatCsv(event.getSuppressionReason().orElse("")))); | ||
} | ||
}; | ||
|
||
void beginPrinting(CliPrinter printer) {} | ||
|
||
abstract void print(CliPrinter printer, ValidationEventFormatter formatter, ValidationEvent event); | ||
|
||
void endPrinting(CliPrinter printer) {} | ||
|
||
private static String formatCsv(String value) { | ||
// Replace DQUOTE with DQUOTEDQUOTE, escape newlines, and escape carriage returns. | ||
return value.replace("\"", "\"\"").replace("\n", "\\n").replace("\r", "\\r"); | ||
} | ||
} | ||
|
||
private Format format = Format.TEXT; | ||
|
||
@Override | ||
public void registerHelp(HelpPrinter printer) { | ||
printer.param(VALIDATION_FORMAT, null, "text|csv", | ||
"Specifies the format to write validation events (text or csv). Defaults to text."); | ||
} | ||
|
||
@Override | ||
public Consumer<String> testParameter(String name) { | ||
if (name.equals(VALIDATION_FORMAT)) { | ||
return s -> { | ||
switch (s) { | ||
case "csv": | ||
format(Format.CSV); | ||
break; | ||
case "text": | ||
format(Format.TEXT); | ||
break; | ||
default: | ||
throw new CliError("Unexpected " + VALIDATION_FORMAT + ": `" + s + "`"); | ||
} | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
void format(Format format) { | ||
this.format = format; | ||
} | ||
|
||
Format format() { | ||
return format; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
smithy-cli/src/test/java/software/amazon/smithy/cli/commands/DiffCommandTest.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,39 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.cli.commands; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.cli.CliUtils; | ||
|
||
public class DiffCommandTest { | ||
@Test | ||
public void canOutputCsv() throws Exception { | ||
Path oldModel = Paths.get(getClass().getResource("diff/old.smithy").toURI()); | ||
Path newModel = Paths.get(getClass().getResource("diff/new.smithy").toURI()); | ||
CliUtils.Result result = CliUtils.runSmithy("diff", | ||
"--old", oldModel.toString(), | ||
"--new", newModel.toString(), | ||
"--format", "csv"); | ||
|
||
assertThat(result.code(), not(0)); | ||
|
||
// Make sure FAILURE is sent to stderr. | ||
assertThat(result.stderr(), containsString("FAILURE")); | ||
assertThat(result.stdout(), not(containsString("FAILURE"))); | ||
|
||
String[] lines = result.stdout().split("(\\r\\n|\\r|\\n)"); | ||
assertThat(lines.length, is(2)); | ||
assertThat(lines[0], containsString("severity,id,shape,file,message,hint,suppressionReason")); | ||
assertThat(lines[1], containsString("\"ERROR\",\"ChangedShapeType\",\"smithy.example#Hello\"")); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/new.smithy
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,10 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
@deprecated | ||
string Hello | ||
|
||
structure Foo { | ||
hello: Hello | ||
} |
10 changes: 10 additions & 0 deletions
10
smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/diff/old.smithy
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,10 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
@deprecated | ||
integer Hello | ||
|
||
structure Foo { | ||
hello: Hello | ||
} |