1
1
import { jest } from '@jest/globals'
2
- import cp from 'child_process'
2
+ import cp from 'node: child_process'
3
3
import fse from 'fs-extra'
4
4
import { fileURLToPath } from 'node:url'
5
- import { dirname , resolve } from 'path'
5
+ import { dirname , resolve } from 'node:path'
6
+ import process from 'node:process'
6
7
import parseArguments from 'yargs-parser'
7
8
8
- import { ICmdInvokeOptions } from '../../main/ts/interface'
9
+ import { ICmdInvokeOptions , TFlags } from '../../main/ts/interface'
9
10
import {
10
11
formatArgs ,
11
12
getClosestBin ,
@@ -17,12 +18,20 @@ import {
17
18
const dotcmd = process . platform === 'win32' ? '.cmd' : ''
18
19
const __filename = fileURLToPath ( import . meta. url )
19
20
const __dirname = dirname ( __filename )
21
+ const spawnResult = ( { status = 0 , stdout = '' , stderr = '' } = { } ) : ReturnType < typeof cp . spawnSync > => ( {
22
+ status,
23
+ stdout : Buffer . from ( stdout ) ,
24
+ stderr : Buffer . from ( stderr ) ,
25
+ pid : process . pid ,
26
+ signal : null ,
27
+ output : [ ] ,
28
+ } )
20
29
const fakeExistsSync = jest . fn ( ( cmd ) => cmd !== 'not-found' + dotcmd )
21
30
const fakeSpawnSync = jest . fn ( ( cmd : string ) => {
22
- const results : Record < string , any > = {
23
- error : { status : 1 , stderr : 'some error' } ,
24
- def : { status : 0 , stdout : 'foobar' } ,
25
- null : { status : 0 , stdout : null } ,
31
+ const results : Record < string , ReturnType < typeof cp . spawnSync > > = {
32
+ error : spawnResult ( { status : 1 , stderr : 'some error' } ) ,
33
+ def : spawnResult ( { stdout : 'foobar' } ) ,
34
+ empty : spawnResult ( { stdout : '' } ) ,
26
35
}
27
36
28
37
return results [ cmd . replace ( dotcmd , '' ) ] || results . def
@@ -31,10 +40,7 @@ const fakeSpawnSync = jest.fn((cmd: string) => {
31
40
beforeAll ( ( ) => {
32
41
jest . spyOn ( cp , 'spawnSync' ) . mockImplementation ( fakeSpawnSync )
33
42
jest . spyOn ( fse , 'existsSync' ) . mockImplementation ( fakeExistsSync )
34
- // @ts -ignore
35
- jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => {
36
- /* noop */
37
- } )
43
+ jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => undefined as never )
38
44
} )
39
45
afterEach ( jest . clearAllMocks )
40
46
afterAll ( jest . resetAllMocks )
@@ -48,7 +54,7 @@ describe('util', () => {
48
54
ICmdInvokeOptions ,
49
55
string ?,
50
56
( string | null ) ?,
51
- any ?,
57
+ ( Parameters < typeof spawnResult > [ 0 ] | null ) ?,
52
58
] [ ] = [
53
59
[ 'uses global cmd ref' , { cmd : 'tsc' } , 'tsc' ] ,
54
60
[
@@ -65,10 +71,10 @@ describe('util', () => {
65
71
'foobar' ,
66
72
] ,
67
73
[
68
- 'returns null if stdout is null ' ,
69
- { cmd : 'null ' , silent : true } ,
70
- 'null ' ,
71
- null ,
74
+ 'returns "" if stdout is empty ' ,
75
+ { cmd : 'empty ' , silent : true } ,
76
+ 'empty ' ,
77
+ '' ,
72
78
] ,
73
79
[
74
80
'throws error if result.signal is not equal 0' ,
@@ -89,7 +95,7 @@ describe('util', () => {
89
95
}
90
96
91
97
if ( expectedError ) {
92
- expect ( ( ) => invoke ( opts ) ) . toThrowError ( new Error ( expectedError ) )
98
+ expect ( ( ) => invoke ( opts ) ) . toThrowError ( new Error ( expectedError . stderr ?. toString ( ) ) )
93
99
} else {
94
100
expect ( invoke ( opts ) ) . toEqual (
95
101
expectedResult === null ? expectedResult : expect . any ( String ) ,
@@ -109,7 +115,7 @@ describe('util', () => {
109
115
110
116
describe ( '#formatArgs' , ( ) => {
111
117
it ( 'return proper values' , ( ) => {
112
- const cases : [ Record < string , any > , string [ ] , string [ ] ] [ ] = [
118
+ const cases : [ TFlags , string [ ] , string [ ] ] [ ] = [
113
119
[ { _ : [ ] , '--' : [ ] } , [ ] , [ ] ] ,
114
120
[ { foo : 'bar' } , [ ] , [ '--foo' , 'bar' ] ] ,
115
121
[ { f : true } , [ ] , [ '-f' ] ] ,
0 commit comments