|
| 1 | +const commander = require('../'); |
| 2 | +const childProcess = require('child_process'); |
| 3 | +const EventEmitter = require('events'); |
| 4 | + |
| 5 | +// Using mock to allow try/catch around what is otherwise out-of-stack error handling. |
| 6 | +// Injecting errors, these are not end-to-end tests. |
| 7 | + |
| 8 | +function makeSystemError(code) { |
| 9 | + // We can not make an actual SystemError, but our usage is lightweight so easy to match. |
| 10 | + const err = new Error(); |
| 11 | + err.code = code; |
| 12 | + return err; |
| 13 | +} |
| 14 | + |
| 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(() => { return mockProcess; }); |
| 20 | + const program = new commander.Command(); |
| 21 | + program.exitOverride(); |
| 22 | + program.command('executable', 'executable description'); |
| 23 | + program.parse(['executable'], { from: 'user' }); |
| 24 | + expect(() => { |
| 25 | + mockProcess.emit('error', makeSystemError('ENOENT')); |
| 26 | + }).toThrow('use the executableFile option to supply a custom name'); // part of custom message |
| 27 | + spawnSpy.mockRestore(); |
| 28 | +}); |
| 29 | + |
| 30 | +test('when subcommand executable not executable (EACCES) then throw custom message', () => { |
| 31 | + // Side note: this error does not actually happen on Windows! But we can still simulate the behaviour on other platforms. |
| 32 | + const mockProcess = new EventEmitter(); |
| 33 | + const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; }); |
| 34 | + const program = new commander.Command(); |
| 35 | + program.exitOverride(); |
| 36 | + program.command('executable', 'executable description'); |
| 37 | + program.parse(['executable'], { from: 'user' }); |
| 38 | + expect(() => { |
| 39 | + mockProcess.emit('error', makeSystemError('EACCES')); |
| 40 | + }).toThrow('not executable'); // part of custom message |
| 41 | + spawnSpy.mockRestore(); |
| 42 | +}); |
| 43 | + |
| 44 | +test('when subcommand executable fails with other error then return in custom wrapper', () => { |
| 45 | + // The existing behaviour is to just silently fail for unexpected errors, as it is happening |
| 46 | + // asynchronously in spawned process and client can not catch errors. |
| 47 | + const mockProcess = new EventEmitter(); |
| 48 | + const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; }); |
| 49 | + const program = new commander.Command(); |
| 50 | + program.exitOverride((err) => { |
| 51 | + throw err; |
| 52 | + }); |
| 53 | + program.command('executable', 'executable description'); |
| 54 | + program.parse(['executable'], { from: 'user' }); |
| 55 | + let caughtErr; |
| 56 | + try { |
| 57 | + mockProcess.emit('error', makeSystemError('OTHER')); |
| 58 | + } catch (err) { |
| 59 | + caughtErr = err; |
| 60 | + } |
| 61 | + expect(caughtErr.code).toEqual('commander.executeSubCommandAsync'); |
| 62 | + expect(caughtErr.nestedError.code).toEqual('OTHER'); |
| 63 | + spawnSpy.mockRestore(); |
| 64 | +}); |
0 commit comments