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.
+
+