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

Make -T - read a test from stdin. #308

Merged
merged 2 commits into from
Jul 1, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Released: TBD
editors, from @Mingun
- [#299](https://github.com/peggyjs/peggy/issues/299) Add example grammar for a
[SemVer.org](https://semver.org) semantic version string, from @dselman
- [#308](https://github.com/peggyjs/peggy/pull/308) Add support for reading test data from stdin using `-T -`, from @hildjj.

### Bug Fixes

Expand Down
12 changes: 8 additions & 4 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class PeggyCLI extends Command {
)
.addOption(new Option(
"-T, --test-file <filename>",
"Test the parser with the contents of the given file, outputting the result of running the parser instead of the parser itself. If the input to be tested is not parsed, the CLI will exit with code 2"
"Test the parser with the contents of the given file, outputting the result of running the parser instead of the parser itself. If the input to be tested is not parsed, the CLI will exit with code 2. A filename of '-' will read from stdin."
).conflicts("test"))
.option("--trace", "Enable tracing in generated parser", false)
.addOption(
Expand Down Expand Up @@ -548,9 +548,13 @@ class PeggyCLI extends Command {
});
}

test(source) {
async test(source) {
if (this.testFile) {
this.testText = fs.readFileSync(this.testFile, "utf8");
if (this.testFile === "-") {
this.testText = await readStream(this.std.in);
} else {
this.testText = fs.readFileSync(this.testFile, "utf8");
}
}
if (typeof this.testText === "string") {
this.verbose("TEST TEXT:", this.testText);
Expand Down Expand Up @@ -654,7 +658,7 @@ class PeggyCLI extends Command {

exitCode = 2;
this.verbose("CLI", errorText = "running test");
this.test(mappedSource);
await this.test(mappedSource);
}
} catch (error) {
// Will either exit or throw.
Expand Down
9 changes: 8 additions & 1 deletion test/cli/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,8 @@ Options:
given file, outputting the result of running
the parser instead of the parser itself. If
the input to be tested is not parsed, the
CLI will exit with code 2
CLI will exit with code 2. A filename of '-'
will read from stdin.
--trace Enable tracing in generated parser (default:
false)
-h, --help display help for command
Expand Down Expand Up @@ -1040,6 +1041,12 @@ bar = '2'
expected: /name: 'peggy',$/m, // Output is JS, not JSON
});

await exec({
args: [grammarFile, "-T", "-"],
stdin: '{"foo": null}',
expected: "{ foo: null }\n", // Still JS, not JSON
});

await exec({
args: ["-T", "____ERROR____FILE_DOES_NOT_EXIST.js", grammarFile],
errorCode: "peggy.cli",
Expand Down