Skip to content

Commit 0f6e880

Browse files
authored
Merge pull request #308 from hildjj/dash-stdin-test
Make `-T -` read a test from stdin.
2 parents e648738 + 1ff77ac commit 0f6e880

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Released: TBD
3838
editors, from @Mingun
3939
- [#299](https://github.com/peggyjs/peggy/issues/299) Add example grammar for a
4040
[SemVer.org](https://semver.org) semantic version string, from @dselman
41+
- [#308](https://github.com/peggyjs/peggy/pull/308) Add support for reading test data from stdin using `-T -`, from @hildjj.
4142

4243
### Bug Fixes
4344

bin/peggy-cli.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class PeggyCLI extends Command {
191191
)
192192
.addOption(new Option(
193193
"-T, --test-file <filename>",
194-
"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"
194+
"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."
195195
).conflicts("test"))
196196
.option("--trace", "Enable tracing in generated parser", false)
197197
.addOption(
@@ -548,9 +548,13 @@ class PeggyCLI extends Command {
548548
});
549549
}
550550

551-
test(source) {
551+
async test(source) {
552552
if (this.testFile) {
553-
this.testText = fs.readFileSync(this.testFile, "utf8");
553+
if (this.testFile === "-") {
554+
this.testText = await readStream(this.std.in);
555+
} else {
556+
this.testText = fs.readFileSync(this.testFile, "utf8");
557+
}
554558
}
555559
if (typeof this.testText === "string") {
556560
this.verbose("TEST TEXT:", this.testText);
@@ -654,7 +658,7 @@ class PeggyCLI extends Command {
654658

655659
exitCode = 2;
656660
this.verbose("CLI", errorText = "running test");
657-
this.test(mappedSource);
661+
await this.test(mappedSource);
658662
}
659663
} catch (error) {
660664
// Will either exit or throw.

test/cli/run.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ Options:
370370
given file, outputting the result of running
371371
the parser instead of the parser itself. If
372372
the input to be tested is not parsed, the
373-
CLI will exit with code 2
373+
CLI will exit with code 2. A filename of '-'
374+
will read from stdin.
374375
--trace Enable tracing in generated parser (default:
375376
false)
376377
-h, --help display help for command
@@ -1040,6 +1041,12 @@ bar = '2'
10401041
expected: /name: 'peggy',$/m, // Output is JS, not JSON
10411042
});
10421043

1044+
await exec({
1045+
args: [grammarFile, "-T", "-"],
1046+
stdin: '{"foo": null}',
1047+
expected: "{ foo: null }\n", // Still JS, not JSON
1048+
});
1049+
10431050
await exec({
10441051
args: ["-T", "____ERROR____FILE_DOES_NOT_EXIST.js", grammarFile],
10451052
errorCode: "peggy.cli",

0 commit comments

Comments
 (0)