1
1
'use strict' ;
2
2
3
- const common = require ( '../common' ) ;
4
- const assert = require ( 'assert' ) ;
3
+ require ( '../common' ) ;
5
4
const { exec } = require ( 'child_process' ) ;
5
+ const { test } = require ( 'node:test' ) ;
6
6
const fixtures = require ( '../common/fixtures' ) ;
7
7
8
8
const node = process . execPath ;
@@ -23,26 +23,39 @@ const syntaxErrorRE = /^SyntaxError: \b/m;
23
23
'syntax/bad_syntax' ,
24
24
'syntax/bad_syntax_shebang.js' ,
25
25
'syntax/bad_syntax_shebang' ,
26
- ] . forEach ( function ( file ) {
27
- file = fixtures . path ( file ) ;
26
+ ] . forEach ( ( file ) => {
27
+ const path = fixtures . path ( file ) ;
28
28
29
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
- } ) ) ;
30
+ syntaxArgs . forEach ( ( args ) => {
31
+ test ( `Checking syntax for ${ file } with ${ args . join ( ' ' ) } ` , async ( t ) => {
32
+ const _args = args . concat ( path ) ;
33
+ const cmd = [ node , ..._args ] . join ( ' ' ) ;
34
+
35
+ try {
36
+ const { stdout, stderr } = await execPromise ( cmd ) ;
37
+
38
+ // No stdout should be produced
39
+ t . assert . strictEqual ( stdout , '' ) ;
40
+
41
+ // Stderr should have a syntax error message
42
+ t . assert . match ( stderr , syntaxErrorRE ) ;
43
+
44
+ // stderr should include the filename
45
+ t . assert . ok ( stderr . startsWith ( path ) ) ;
46
+ } catch ( err ) {
47
+ t . assert . strictEqual ( err . code , 1 ) ;
48
+ }
49
+ } ) ;
47
50
} ) ;
48
51
} ) ;
52
+
53
+ // Helper function to promisify exec
54
+ function execPromise ( cmd ) {
55
+ const { promise, resolve, reject } = Promise . withResolvers ( ) ;
56
+ exec ( cmd , ( err , stdout , stderr ) => {
57
+ if ( err ) return reject ( { ...err , stdout, stderr } ) ;
58
+ resolve ( { stdout, stderr } ) ;
59
+ } ) ;
60
+ return promise ;
61
+ }
0 commit comments