Skip to content

Commit

Permalink
Merge pull request #308 from hildjj/dash-stdin-test
Browse files Browse the repository at this point in the history
Make `-T -` read a test from stdin.
  • Loading branch information
hildjj authored Jul 1, 2022
2 parents e648738 + 1ff77ac commit 0f6e880
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
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

0 comments on commit 0f6e880

Please sign in to comment.