|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { exec } = require('child_process'); |
| 6 | +const fixtures = require('../common/fixtures'); |
| 7 | + |
| 8 | +const node = process.execPath; |
| 9 | + |
| 10 | +// test both sets of arguments that check syntax |
| 11 | +const syntaxArgs = [ |
| 12 | + ['-c'], |
| 13 | + ['--check'] |
| 14 | +]; |
| 15 | + |
| 16 | +// Match on the name of the `Error` but not the message as it is different |
| 17 | +// depending on the JavaScript engine. |
| 18 | +const syntaxErrorRE = /^SyntaxError: \b/m; |
| 19 | + |
| 20 | +// test bad syntax with and without shebang |
| 21 | +[ |
| 22 | + 'syntax/bad_syntax.js', |
| 23 | + 'syntax/bad_syntax', |
| 24 | + 'syntax/bad_syntax_shebang.js', |
| 25 | + 'syntax/bad_syntax_shebang' |
| 26 | +].forEach(function(file) { |
| 27 | + file = fixtures.path(file); |
| 28 | + |
| 29 | + // loop each possible option, `-c` or `--check` |
| 30 | + syntaxArgs.forEach(function(args) { |
| 31 | + const _args = args.concat(file); |
| 32 | + const cmd = [node, ..._args].join(' '); |
| 33 | + exec(cmd, common.mustCall((err, stdout, stderr) => { |
| 34 | + assert.strictEqual(err instanceof Error, true); |
| 35 | + assert.strictEqual(err.code, 1); |
| 36 | + |
| 37 | + // no stdout should be produced |
| 38 | + assert.strictEqual(stdout, ''); |
| 39 | + |
| 40 | + // stderr should have a syntax error message |
| 41 | + assert(syntaxErrorRE.test(stderr), `${syntaxErrorRE} === ${stderr}`); |
| 42 | + |
| 43 | + // stderr should include the filename |
| 44 | + assert(stderr.startsWith(file), `${stderr} starts with ${file}`); |
| 45 | + })); |
| 46 | + }); |
| 47 | +}); |
0 commit comments