|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +constcommon=require('../common'); |
| 4 | +constassert=require('assert'); |
| 5 | +const{ exec }=require('child_process'); |
| 6 | +constfixtures=require('../common/fixtures'); |
| 7 | + |
| 8 | +constnode=process.execPath; |
| 9 | + |
| 10 | +// test both sets of arguments that check syntax |
| 11 | +constsyntaxArgs=[ |
| 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 | +constsyntaxErrorRE=/^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 | +constcmd=[node, ..._args].join(' '); |
| 33 | +exec(cmd,common.mustCall((err,stdout,stderr)=>{ |
| 34 | +assert.strictEqual(errinstanceofError,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