From 6ec7f36f3ca898fef65a7ac1233d7e395ff6431d Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Thu, 12 Sep 2024 10:23:03 -0700 Subject: [PATCH] Fix emu-format --check with multiple files Previously, running emu-format --check with multiple files would only check the first file. Fix the CLI arguments processing logic so that the only special case for a list of one file is when it needs to be printed to stdout. Move checking into the loop through the file list, as is done with writing. Closes: #606 --- src/formatter/cli.ts | 12 +++--------- test/cli.js | 29 +++++++++++++++++++++++++++++ test/format-bad.html | 17 +++++++++++++++++ test/format-good.html | 16 ++++++++++++++++ 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 test/format-bad.html create mode 100644 test/format-good.html diff --git a/src/formatter/cli.ts b/src/formatter/cli.ts index c20babf6..1e9b2af0 100644 --- a/src/formatter/cli.ts +++ b/src/formatter/cli.ts @@ -99,17 +99,11 @@ function usage() { } const touched = []; - if (!write) { + if (!write && !check) { const input = await fs.readFile(files[0], 'utf8'); const printed = await printDocument(input); - if (check) { - if (printed !== input) { - touched.push(files[0]); - } - } else { - // printDocument includes a newline - process.stdout.write(printed); - } + // printDocument includes a newline + process.stdout.write(printed); } else { for (const file of files) { console.log(`Processing ${file}`); diff --git a/test/cli.js b/test/cli.js index c71fe665..2a5d6cbf 100644 --- a/test/cli.js +++ b/test/cli.js @@ -38,3 +38,32 @@ describe('ecmarkup#cli', function () { }); }); }); + +describe('emu-format --check', function () { + this.timeout(4000); + + it('exits cleanly if the file needs no formatting', () => { + execSync(`${execPath} ./bin/emu-format.js --check test/format-good.html`, { + encoding: 'utf8', + }); + }); + + it('exits with an error if the file needs formatting', () => { + assert.throws(() => { + execSync(`${execPath} ./bin/emu-format.js --check test/format-bad.html`, { + encoding: 'utf8', + }); + }); + }); + + it('exits with an error if any files in the list need formatting', () => { + assert.throws(() => { + execSync( + `${execPath} ./bin/emu-format.js --check test/format-good.html test/format-bad.html`, + { + encoding: 'utf8', + }, + ); + }); + }); +}); diff --git a/test/format-bad.html b/test/format-bad.html new file mode 100644 index 00000000..02102ca4 --- /dev/null +++ b/test/format-bad.html @@ -0,0 +1,17 @@ + + + + + + + +

Clause Foo( _a_, _b_ )

+ + + 1. Call Foo(_a_). + 1. Call Bar(`toString`). + 1. Call Baz(*true*). + 1. Do something else. + 1. And again. + +
diff --git a/test/format-good.html b/test/format-good.html new file mode 100644 index 00000000..e5e99bbf --- /dev/null +++ b/test/format-good.html @@ -0,0 +1,16 @@ + + + + + + +

Clause Foo(_a_, _b_)

+ + + 1. Call Foo(_a_). + 1. Call Bar(`toString`). + 1. Call Baz(*true*). + 1. Do something else. + 1. And again. + +