Skip to content

Commit 1483dbf

Browse files
committed
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
1 parent 2507175 commit 1483dbf

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

src/formatter/cli.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,11 @@ function usage() {
9999
}
100100

101101
const touched = [];
102-
if (!write) {
102+
if (!write && !check) {
103103
const input = await fs.readFile(files[0], 'utf8');
104104
const printed = await printDocument(input);
105-
if (check) {
106-
if (printed !== input) {
107-
touched.push(files[0]);
108-
}
109-
} else {
110-
// printDocument includes a newline
111-
process.stdout.write(printed);
112-
}
105+
// printDocument includes a newline
106+
process.stdout.write(printed);
113107
} else {
114108
for (const file of files) {
115109
console.log(`Processing ${file}`);

test/cli.js

+28
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,31 @@ describe('ecmarkup#cli', function () {
3838
});
3939
});
4040
});
41+
42+
describe('emu-format --check', function () {
43+
this.timeout(4000);
44+
45+
it('exits cleanly if the file needs no formatting', () => {
46+
execSync(`${execPath} ./bin/emu-format.js --check test/format-good.html`, {
47+
encoding: 'utf8',
48+
});
49+
});
50+
51+
it('exits with an error if the file needs formatting', () => {
52+
assert.throws(() => {
53+
execSync(`${execPath} ./bin/emu-format.js --check test/format-bad.html`, {
54+
encoding: 'utf8',
55+
});
56+
});
57+
});
58+
59+
it('exits with an error if any files in the list need formatting', () => {
60+
assert.throws(() => {
61+
execSync(
62+
`${execPath} ./bin/emu-format.js --check test/format-good.html test/format-bad.html`, {
63+
encoding: 'utf8',
64+
},
65+
);
66+
});
67+
});
68+
});

test/format-bad.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<link rel="stylesheet" href="css/elements.css">
4+
<script src="ecmarkup.js"></script>
5+
6+
<emu-clause id="c1">
7+
8+
<h1>Clause Foo( _a_, _b_ )</h1>
9+
10+
<emu-alg>
11+
1. Call Foo(_a_).
12+
1. Call Bar(`toString`).
13+
1. Call Baz(*true*).
14+
1. Do something else.
15+
1. And again.
16+
</emu-alg>
17+
</emu-clause>

test/format-good.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<link rel="stylesheet" href="css/elements.css">
4+
<script src="ecmarkup.js"></script>
5+
6+
<emu-clause id="c1">
7+
<h1>Clause Foo(_a_, _b_)</h1>
8+
9+
<emu-alg>
10+
1. Call Foo(_a_).
11+
1. Call Bar(`toString`).
12+
1. Call Baz(*true*).
13+
1. Do something else.
14+
1. And again.
15+
</emu-alg>
16+
</emu-clause>

0 commit comments

Comments
 (0)