1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { exec } = require('child_process');
6const fixtures = require('../common/fixtures');
7
8const node = process.execPath;
9
10// Test both sets of arguments that check syntax
11const 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.
18const 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                         `code ${err.code} !== 1 for error:\n\n${err}`);
37
38      // No stdout should be produced
39      assert.strictEqual(stdout, '');
40
41      // Stderr should have a syntax error message
42      assert.match(stderr, syntaxErrorRE);
43
44      // stderr should include the filename
45      assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
46    }));
47  });
48});
49