11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { exec } = require('child_process');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst node = process.execPath;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Test both sets of arguments that check syntax
111cb0ef41Sopenharmony_ciconst syntaxArgs = [
121cb0ef41Sopenharmony_ci  ['-c'],
131cb0ef41Sopenharmony_ci  ['--check'],
141cb0ef41Sopenharmony_ci];
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Match on the name of the `Error` but not the message as it is different
171cb0ef41Sopenharmony_ci// depending on the JavaScript engine.
181cb0ef41Sopenharmony_ciconst syntaxErrorRE = /^SyntaxError: \b/m;
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci// Test bad syntax with and without shebang
211cb0ef41Sopenharmony_ci[
221cb0ef41Sopenharmony_ci  'syntax/bad_syntax.js',
231cb0ef41Sopenharmony_ci  'syntax/bad_syntax',
241cb0ef41Sopenharmony_ci  'syntax/bad_syntax_shebang.js',
251cb0ef41Sopenharmony_ci  'syntax/bad_syntax_shebang',
261cb0ef41Sopenharmony_ci].forEach(function(file) {
271cb0ef41Sopenharmony_ci  file = fixtures.path(file);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  // Loop each possible option, `-c` or `--check`
301cb0ef41Sopenharmony_ci  syntaxArgs.forEach(function(args) {
311cb0ef41Sopenharmony_ci    const _args = args.concat(file);
321cb0ef41Sopenharmony_ci    const cmd = [node, ..._args].join(' ');
331cb0ef41Sopenharmony_ci    exec(cmd, common.mustCall((err, stdout, stderr) => {
341cb0ef41Sopenharmony_ci      assert.strictEqual(err instanceof Error, true);
351cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 1,
361cb0ef41Sopenharmony_ci                         `code ${err.code} !== 1 for error:\n\n${err}`);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci      // No stdout should be produced
391cb0ef41Sopenharmony_ci      assert.strictEqual(stdout, '');
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci      // Stderr should have a syntax error message
421cb0ef41Sopenharmony_ci      assert.match(stderr, syntaxErrorRE);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci      // stderr should include the filename
451cb0ef41Sopenharmony_ci      assert(stderr.startsWith(file), `${stderr} starts with ${file}`);
461cb0ef41Sopenharmony_ci    }));
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci});
49