|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | +// Requirements |
| 5 | +//------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +const assert = require("assert"); |
| 8 | +const { execFile } = require("child_process"); |
| 9 | +const { promisify } = require("util"); |
| 10 | + |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | +// Helpers |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +/** |
| 16 | + * Runs check-rule-examples on the specified files. |
| 17 | + * @param {...string} filenames Files to be passed to check-rule-examples. |
| 18 | + * @returns {Promise<ChildProcess>} An object with properties `stdout` and `stderr` on success. |
| 19 | + * @throws An object with properties `code`, `stdout` and `stderr` on success. |
| 20 | + */ |
| 21 | +async function runCheckRuleExamples(...filenames) { |
| 22 | + return await promisify(execFile)( |
| 23 | + process.execPath, |
| 24 | + ["--no-deprecation", "tools/check-rule-examples.js", ...filenames], |
| 25 | + { env: { FORCE_COLOR: "3" } } // 24-bit color mode |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | +// Tests |
| 31 | +//------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +describe("check-rule-examples", () => { |
| 34 | + |
| 35 | + it("succeeds when not passed any files", async () => { |
| 36 | + const childProcess = await runCheckRuleExamples(); |
| 37 | + |
| 38 | + assert.strictEqual(childProcess.stdout, ""); |
| 39 | + assert.strictEqual(childProcess.stderr, ""); |
| 40 | + }); |
| 41 | + |
| 42 | + it("succeeds when passed a syntax error free file", async () => { |
| 43 | + const childProcess = await runCheckRuleExamples("tests/fixtures/good-examples.md"); |
| 44 | + |
| 45 | + assert.strictEqual(childProcess.stdout, ""); |
| 46 | + assert.strictEqual(childProcess.stderr, ""); |
| 47 | + }); |
| 48 | + |
| 49 | + it("fails when passed a file with a syntax error", async () => { |
| 50 | + const promise = runCheckRuleExamples("tests/fixtures/good-examples.md", "tests/fixtures/bad-examples.md"); |
| 51 | + |
| 52 | + await assert.rejects( |
| 53 | + promise, |
| 54 | + { |
| 55 | + code: 1, |
| 56 | + stdout: "", |
| 57 | + stderr: |
| 58 | + "\x1B[0m\x1B[0m\n" + |
| 59 | + "\x1B[0m\x1B[4mtests/fixtures/bad-examples.md\x1B[24m\x1B[0m\n" + |
| 60 | + "\x1B[0m \x1B[2m11:4\x1B[22m \x1B[31merror\x1B[39m Missing language tag: use one of 'javascript', 'js' or 'jsx'\x1B[0m\n" + |
| 61 | + "\x1B[0m \x1B[2m12:1\x1B[22m \x1B[31merror\x1B[39m Syntax error: 'import' and 'export' may appear only with 'sourceType: module'\x1B[0m\n" + |
| 62 | + "\x1B[0m \x1B[2m20:5\x1B[22m \x1B[31merror\x1B[39m Nonstandard language tag 'ts': use one of 'javascript', 'js' or 'jsx'\x1B[0m\n" + |
| 63 | + "\x1B[0m \x1B[2m23:7\x1B[22m \x1B[31merror\x1B[39m Syntax error: Identifier 'foo' has already been declared\x1B[0m\n" + |
| 64 | + "\x1B[0m\x1B[0m\n" + |
| 65 | + "\x1B[0m\x1B[31m\x1B[1m✖ 4 problems (4 errors, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n" + |
| 66 | + "\x1B[0m\x1B[31m\x1B[1m\x1B[22m\x1B[39m\x1B[0m\n" |
| 67 | + } |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it("fails when a file cannot be processed", async () => { |
| 72 | + const promise = runCheckRuleExamples("tests/fixtures/non-existing-examples.md"); |
| 73 | + |
| 74 | + await assert.rejects( |
| 75 | + promise, |
| 76 | + ({ code, stdout, stderr }) => { |
| 77 | + assert.strictEqual(code, 1); |
| 78 | + assert.strictEqual(stdout, ""); |
| 79 | + const expectedStderr = |
| 80 | + "\x1B[0m\x1B[0m\n" + |
| 81 | + "\x1B[0m\x1B[4mtests/fixtures/non-existing-examples.md\x1B[24m\x1B[0m\n" + |
| 82 | + "\x1B[0m \x1B[2m0:0\x1B[22m \x1B[31merror\x1B[39m Error checking file: ENOENT: no such file or directory, open <FILE>\x1B[0m\n" + |
| 83 | + "\x1B[0m\x1B[0m\n" + |
| 84 | + "\x1B[0m\x1B[31m\x1B[1m✖ 1 problem (1 error, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n" + |
| 85 | + "\x1B[0m\x1B[31m\x1B[1m\x1B[22m\x1B[39m\x1B[0m\n"; |
| 86 | + |
| 87 | + // Replace filename as it's OS-dependent. |
| 88 | + const normalizedStderr = stderr.replace(/'.+'/u, "<FILE>"); |
| 89 | + |
| 90 | + assert.strictEqual(normalizedStderr, expectedStderr); |
| 91 | + return true; |
| 92 | + } |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments