|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const spawnSync = require('child_process').spawnSync; |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const common = require('../common'); |
| 8 | + |
| 9 | +var node = process.execPath; |
| 10 | + |
| 11 | +// test both sets of arguments that check syntax |
| 12 | +var syntaxArgs = [ |
| 13 | + ['-c'], |
| 14 | + ['--check'] |
| 15 | +]; |
| 16 | + |
| 17 | +// test good syntax with and without shebang |
| 18 | +[ |
| 19 | + 'syntax/good_syntax.js', |
| 20 | + 'syntax/good_syntax', |
| 21 | + 'syntax/good_syntax_shebang.js', |
| 22 | + 'syntax/good_syntax_shebang', |
| 23 | +].forEach(function(file) { |
| 24 | + file = path.join(common.fixturesDir, file); |
| 25 | + |
| 26 | + // loop each possible option, `-c` or `--check` |
| 27 | + syntaxArgs.forEach(function(args) { |
| 28 | + var _args = args.concat(file); |
| 29 | + var c = spawnSync(node, _args, {encoding: 'utf8'}); |
| 30 | + |
| 31 | + // no output should be produced |
| 32 | + assert.equal(c.stdout, '', 'stdout produced'); |
| 33 | + assert.equal(c.stderr, '', 'stderr produced'); |
| 34 | + assert.equal(c.status, 0, 'code == ' + c.status); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +// test bad syntax with and without shebang |
| 39 | +[ |
| 40 | + 'syntax/bad_syntax.js', |
| 41 | + 'syntax/bad_syntax', |
| 42 | + 'syntax/bad_syntax_shebang.js', |
| 43 | + 'syntax/bad_syntax_shebang' |
| 44 | +].forEach(function(file) { |
| 45 | + file = path.join(common.fixturesDir, file); |
| 46 | + |
| 47 | + // loop each possible option, `-c` or `--check` |
| 48 | + syntaxArgs.forEach(function(args) { |
| 49 | + var _args = args.concat(file); |
| 50 | + var c = spawnSync(node, _args, {encoding: 'utf8'}); |
| 51 | + |
| 52 | + // no stdout should be produced |
| 53 | + assert.equal(c.stdout, '', 'stdout produced'); |
| 54 | + |
| 55 | + // stderr should have a syntax error message |
| 56 | + var match = c.stderr.match(/^SyntaxError: Unexpected identifier$/m); |
| 57 | + assert(match, 'stderr incorrect'); |
| 58 | + |
| 59 | + assert.equal(c.status, 1, 'code == ' + c.status); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +// test file not found |
| 64 | +[ |
| 65 | + 'syntax/file_not_found.js', |
| 66 | + 'syntax/file_not_found' |
| 67 | +].forEach(function(file) { |
| 68 | + file = path.join(common.fixturesDir, file); |
| 69 | + |
| 70 | + // loop each possible option, `-c` or `--check` |
| 71 | + syntaxArgs.forEach(function(args) { |
| 72 | + var _args = args.concat(file); |
| 73 | + var c = spawnSync(node, _args, {encoding: 'utf8'}); |
| 74 | + |
| 75 | + // no stdout should be produced |
| 76 | + assert.equal(c.stdout, '', 'stdout produced'); |
| 77 | + |
| 78 | + // stderr should have a module not found error message |
| 79 | + var match = c.stderr.match(/^Error: Cannot find module/m); |
| 80 | + assert(match, 'stderr incorrect'); |
| 81 | + |
| 82 | + assert.equal(c.status, 1, 'code == ' + c.status); |
| 83 | + }); |
| 84 | +}); |
0 commit comments