Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix emu-format --check with multiple files #607

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading