Skip to content

Commit

Permalink
Fix emu-format --check with multiple files (#607)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptomato authored Sep 12, 2024
1 parent 2507175 commit f9f05bf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/formatter/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
29 changes: 29 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
);
});
});
});
17 changes: 17 additions & 0 deletions test/format-bad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="css/elements.css">
<script src="ecmarkup.js"></script>

<emu-clause id="c1">

<h1>Clause Foo( _a_, _b_ )</h1>

<emu-alg>
1. Call Foo(_a_).
1. Call Bar(`toString`).
1. Call Baz(*true*).
1. Do something else.
1. And again.
</emu-alg>
</emu-clause>
16 changes: 16 additions & 0 deletions test/format-good.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link rel="stylesheet" href="css/elements.css">
<script src="ecmarkup.js"></script>

<emu-clause id="c1">
<h1>Clause Foo(_a_, _b_)</h1>

<emu-alg>
1. Call Foo(_a_).
1. Call Bar(`toString`).
1. Call Baz(*true*).
1. Do something else.
1. And again.
</emu-alg>
</emu-clause>

0 comments on commit f9f05bf

Please sign in to comment.