Skip to content

Commit 6d3f8aa

Browse files
committed
Add support for the --check parameter
1 parent 8b50748 commit 6d3f8aa

6 files changed

Lines changed: 45 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ The tipical location of the installed extension (your actual version might diffe
3333
Available features from the command line:
3434
- To format a file: `npm run --silent prettify-md < input.md`.
3535
- To format a file and save the output: `npm run --silent prettify-md < input.md > output.md`.
36-
- To check whether a file is prettyfied or not: `npm run --silent prettify-md --check < input.md`. This will fail if the file is not prettyfied.
36+
- To check whether a file is prettyfied or not: `npm run --silent check-md < input.md`. This will fail with an exception and return code `1` if the file is not prettyfied.
37+
38+
> Note: the `--silent` switch sets the npm log level to silent, which is useful to hide the executed file name and concentrate on the actual output.
3739
3840
## Extension Settings
3941

cli/cliPrettify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export class CliPrettify {
2323
return prettyfier.formatTables(text);
2424
}
2525

26+
public static check(text: string): void {
27+
if (this.prettify(text) !== text) {
28+
throw new Error("The input file is not prettyfied!");
29+
}
30+
}
31+
2632
private static createPrettyfier(): MultiTablePrettyfier {
2733
const logger = new ConsoleLogger();
2834
return new MultiTablePrettyfier(

cli/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { CliPrettify } from "./cliPrettify";
22
import { InputReader } from "./inputReader";
33

4-
InputReader.subscribe(input => process.stdout.write(CliPrettify.prettify(input)));
4+
const checkOnly = process.argv.length > 2 || process.argv.find(arg => arg === "--check");
5+
6+
InputReader.subscribe(input =>
7+
checkOnly
8+
? CliPrettify.check(input)
9+
: process.stdout.write(CliPrettify.prettify(input))
10+
);

cli/inputReader.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export class InputReader {
22
static subscribe(readDone): void {
3-
//TODO: read the "--check" argument
43
if (process.stdin.isTTY) {
54
readDone(process.argv[2] || "");
65
} else {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"compile": "tsc -p ./",
4949
"pretest": "npm run compile",
5050
"test": "gulp copy-systemTest-resources && node ./out/test/index.js",
51-
"prettify-md": "node ./out/cli/index.js"
51+
"prettify-md": "node ./out/cli/index.js",
52+
"check-md": "node ./out/cli/index.js --check"
5253
},
5354
"devDependencies": {
5455
"@types/mocha": "^7.0.2",

test/systemTests/cliPrettyfierSystemTests.test.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,41 @@ import * as assert from 'assert';
44
import { CliPrettify } from '../../cli/cliPrettify';
55

66
fs.readdir(path.resolve(__dirname, "resources/"), function(err, files) {
7-
suite("CLI Prettyfier system tests", () => {
7+
suite("CLI Prettyfier system tests - prettyfied input files match prepared expected files", () => {
88
const distinctTests: string[] = getTestFileNames(files);
9-
109
for (let fileNameRoot of distinctTests) {
1110
test(`[${fileNameRoot}]`, () => {
1211
const input = fs.readFileSync(pathFor(`${fileNameRoot}-input.md`), "utf8");
1312
const expected = fs.readFileSync(pathFor(`${fileNameRoot}-expected.md`), "utf8");
1413

1514
const actual = CliPrettify.prettify(input);
1615

17-
assert.equal(actual, expected);
16+
assert.strictEqual(actual, expected);
17+
});
18+
}
19+
});
20+
});
21+
22+
fs.readdir(path.resolve(__dirname, "resources/"), function(err, files) {
23+
suite("CLI Prettyfier system tests - check() for non pretty files throws error", () => {
24+
const distinctTests: string[] = getTestFileNames(files);
25+
for (let fileNameRoot of distinctTests) {
26+
test(`[${fileNameRoot}]`, () => {
27+
const input = fs.readFileSync(pathFor(`${fileNameRoot}-input.md`), "utf8");
28+
assert.throws(() => CliPrettify.check(input));
29+
});
30+
}
31+
});
32+
});
33+
34+
35+
fs.readdir(path.resolve(__dirname, "resources/"), function(err, files) {
36+
suite("CLI Prettyfier system tests - check() for pretty files does not throw", () => {
37+
const distinctTests: string[] = getTestFileNames(files);
38+
for (let fileNameRoot of distinctTests) {
39+
test(`[${fileNameRoot}]`, () => {
40+
const expected = fs.readFileSync(pathFor(`${fileNameRoot}-expected.md`), "utf8");
41+
assert.doesNotThrow(() => CliPrettify.check(expected));
1842
});
1943
}
2044
});

0 commit comments

Comments
 (0)