summaryrefslogtreecommitdiff
path: root/test/parallel/test-cli-syntax-piped-bad.js
blob: abd924848fc417e342450eb8e0a1378a6264c516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';

require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');

const node = process.execPath;

// Test both sets of arguments that check syntax
const syntaxArgs = [
  '-c',
  '--check'
];

// Match on the name of the `Error` but not the message as it is different
// depending on the JavaScript engine.
const syntaxErrorRE = /^SyntaxError: Unexpected identifier\b/m;

// Should throw if code piped from stdin with --check has bad syntax
// loop each possible option, `-c` or `--check`
syntaxArgs.forEach(function(arg) {
  const stdin = 'var foo bar;';
  const c = spawnSync(node, [arg], { encoding: 'utf8', input: stdin });

  // stderr should include '[stdin]' as the filename
  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);

  // No stdout should be produced
  assert.strictEqual(c.stdout, '');

  // stderr should have a syntax error message
  assert(syntaxErrorRE.test(c.stderr), `${syntaxErrorRE} === ${c.stderr}`);

  assert.strictEqual(c.status, 1);
});

// Check --input-type=module
syntaxArgs.forEach(function(arg) {
  const stdin = 'export var p = 5; var foo bar;';
  const c = spawnSync(
    node,
    ['--input-type=module', '--no-warnings', arg],
    { encoding: 'utf8', input: stdin }
  );

  // stderr should include '[stdin]' as the filename
  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);

  // No stdout should be produced
  assert.strictEqual(c.stdout, '');

  // stderr should have a syntax error message
  assert(syntaxErrorRE.test(c.stderr), `${syntaxErrorRE} === ${c.stderr}`);

  assert.strictEqual(c.status, 1);
});