@@ -12,47 +12,62 @@ function makeSystemError(code) {
1212 return err ;
1313}
1414
15- test ( 'when subcommand executable missing (ENOENT) then throw custom message' , ( ) => {
16- // If the command is not found, we show a custom error with an explanation and offer
17- // some advice for possible fixes.
18- const mockProcess = new EventEmitter ( ) ;
19- const spawnSpy = jest . spyOn ( childProcess , 'spawn' ) . mockImplementation ( ( ) => {
20- return mockProcess ;
21- } ) ;
22- const program = new commander . Command ( ) ;
23- program . exitOverride ( ) ;
24- program . command ( 'executable' , 'executable description' ) ;
25- program . parse ( [ 'executable' ] , { from : 'user' } ) ;
26- expect ( ( ) => {
27- mockProcess . emit ( 'error' , makeSystemError ( 'ENOENT' ) ) ;
28- } ) . toThrow ( 'use the executableFile option to supply a custom name' ) ; // part of custom message
29- spawnSpy . mockRestore ( ) ;
30- } ) ;
15+ // Suppress false positive warnings due to use of testOrSkipOnWindows
16+ /* eslint-disable jest/no-standalone-expect */
3117
32- test ( 'when subcommand executable not executable (EACCES) then throw custom message' , ( ) => {
33- // Side note: this error does not actually happen on Windows! But we can still simulate the behaviour on other platforms.
34- const mockProcess = new EventEmitter ( ) ;
35- const spawnSpy = jest . spyOn ( childProcess , 'spawn' ) . mockImplementation ( ( ) => {
36- return mockProcess ;
37- } ) ;
38- const program = new commander . Command ( ) ;
39- program . exitOverride ( ) ;
40- program . command ( 'executable' , 'executable description' ) ;
41- program . parse ( [ 'executable' ] , { from : 'user' } ) ;
42- expect ( ( ) => {
43- mockProcess . emit ( 'error' , makeSystemError ( 'EACCES' ) ) ;
44- } ) . toThrow ( 'not executable' ) ; // part of custom message
45- spawnSpy . mockRestore ( ) ;
46- } ) ;
18+ const testOrSkipOnWindows = process . platform === 'win32' ? test . skip : test ;
19+
20+ testOrSkipOnWindows (
21+ 'when subcommand executable missing (ENOENT) then throw custom message' ,
22+ ( ) => {
23+ // If the command is not found, we show a custom error with an explanation and offer
24+ // some advice for possible fixes.
25+ const mockProcess = new EventEmitter ( ) ;
26+ const spawnSpy = jest
27+ . spyOn ( childProcess , 'spawn' )
28+ . mockImplementation ( ( ) => {
29+ return mockProcess ;
30+ } ) ;
31+ const program = new commander . Command ( ) ;
32+ program . exitOverride ( ) ;
33+ program . command ( 'executable' , 'executable description' ) ;
34+ program . parse ( [ 'executable' ] , { from : 'user' } ) ;
35+ expect ( ( ) => {
36+ mockProcess . emit ( 'error' , makeSystemError ( 'ENOENT' ) ) ;
37+ } ) . toThrow ( 'use the executableFile option to supply a custom name' ) ; // part of custom message
38+ spawnSpy . mockRestore ( ) ;
39+ } ,
40+ ) ;
41+
42+ testOrSkipOnWindows (
43+ 'when subcommand executable not executable (EACCES) then throw custom message' ,
44+ ( ) => {
45+ const mockProcess = new EventEmitter ( ) ;
46+ const spawnSpy = jest
47+ . spyOn ( childProcess , 'spawn' )
48+ . mockImplementation ( ( ) => {
49+ return mockProcess ;
50+ } ) ;
51+ const program = new commander . Command ( ) ;
52+ program . exitOverride ( ) ;
53+ program . command ( 'executable' , 'executable description' ) ;
54+ program . parse ( [ 'executable' ] , { from : 'user' } ) ;
55+ expect ( ( ) => {
56+ mockProcess . emit ( 'error' , makeSystemError ( 'EACCES' ) ) ;
57+ } ) . toThrow ( 'not executable' ) ; // part of custom message
58+ spawnSpy . mockRestore ( ) ;
59+ } ,
60+ ) ;
4761
48- test ( 'when subcommand executable fails with other error and exitOverride then return in custom wrapper' , ( ) => {
62+ test ( 'when subcommand executable fails with other error and exitOverride then return in custom wrapper' , ( ) => {
4963 // The existing behaviour is to just silently fail for unexpected errors, as it is happening
5064 // asynchronously in spawned process and client can not catch errors.
5165 const mockProcess = new EventEmitter ( ) ;
5266 const spawnSpy = jest . spyOn ( childProcess , 'spawn' ) . mockImplementation ( ( ) => {
5367 return mockProcess ;
5468 } ) ;
5569 const program = new commander . Command ( ) ;
70+ program . _checkForMissingExecutable = ( ) => { } ; // suppress error, call mocked spawn
5671 program . exitOverride ( ( err ) => {
5772 throw err ;
5873 } ) ;
@@ -78,6 +93,7 @@ test('when subcommand executable fails with other error then exit', () => {
7893 } ) ;
7994 const exitSpy = jest . spyOn ( process , 'exit' ) . mockImplementation ( ( ) => { } ) ;
8095 const program = new commander . Command ( ) ;
96+ program . _checkForMissingExecutable = ( ) => { } ; // suppress error, call mocked spawn
8197 program . command ( 'executable' , 'executable description' ) ;
8298 program . parse ( [ 'executable' ] , { from : 'user' } ) ;
8399 mockProcess . emit ( 'error' , makeSystemError ( 'OTHER' ) ) ;
0 commit comments