Skip to content
Myr

command.executableSubcommand.mock.test.js

This file

Path

tests/command.executableSubcommand.mock.test.js

103 lines · 3.9 KiB · JavaScript

Last changed by

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

History of this file

Take it

The walk stopped after 200 commits: lines older than 950f742 point at it.

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 1–14why, one step back
1const commander = require('../');
2const childProcess = require('child_process');3const EventEmitter = require('events');45// 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.78function 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

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

lines 15–16why, one step back
15// Suppress false positive warnings due to use of testOrSkipOnWindows
16/* eslint-disable jest/no-standalone-expect */

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

line 17why, one step back
17

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

line 18why, one step back
18const testOrSkipOnWindows = process.platform === 'win32' ? test.skip : test;

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

line 19why, one step back
19

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

lines 20–62why, one step back
20testOrSkipOnWindows(
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 offer24 // some advice for possible fixes.25 const mockProcess = new EventEmitter();26 const spawnSpy = jest27 .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 message38 spawnSpy.mockRestore();39 },40);4142testOrSkipOnWindows(43 'when subcommand executable not executable (EACCES) then throw custom message',44 () => {45 const mockProcess = new EventEmitter();46 const spawnSpy = jest47 .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 message58 spawnSpy.mockRestore();59 },60);6162test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', () => {

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 63–65why, one step back
63 // The existing behaviour is to just silently fail for unexpected errors, as it is happening
64 // asynchronously in spawned process and client can not catch errors.65 const mockProcess = new EventEmitter();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 66–68why, one step back
66 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
67 return mockProcess;68 });

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

line 69why, one step back
69 const program = new commander.Command();

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

line 70why, one step back
70 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 71–90why, one step back
71 program.exitOverride((err) => {
72 throw err;73 });74 program.command('executable', 'executable description');75 program.parse(['executable'], { from: 'user' });76 let caughtErr;77 try {78 mockProcess.emit('error', makeSystemError('OTHER'));79 } catch (err) {80 caughtErr = err;81 }82 expect(caughtErr.code).toEqual('commander.executeSubCommandAsync');83 expect(caughtErr.nestedError.code).toEqual('OTHER');84 spawnSpy.mockRestore();85});8687test('when subcommand executable fails with other error then exit', () => {88 // The existing behaviour is to just silently fail for unexpected errors, as it is happening89 // asynchronously in spawned process and client can not catch errors.90 const mockProcess = new EventEmitter();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 91–94why, one step back
91 const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {
92 return mockProcess;93 });94 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

line 95why, one step back
95 const program = new commander.Command();

Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

line 96why, one step back
96 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn

Update CHANGELOG for one-last-thing for 8.2.0John Geebefore adoption950f742

lines 97–103why, one step back
97 program.command('executable', 'executable description');
98 program.parse(['executable'], { from: 'user' });99 mockProcess.emit('error', makeSystemError('OTHER'));100 expect(exitSpy).toHaveBeenCalledWith(1);101 exitSpy.mockRestore();102 spawnSpy.mockRestore();103});