command.executableSubcommand.mock.test.cjs
This file
Path
tests/command.executableSubcommand.mock.test.cjs
103 lines · 4.0 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–103why, one step back1const commander = require('../');2const childProcess = require('child_process');3const EventEmitter = require('events');4const { test, describe } = require('node:test');5const assert = require('node:assert/strict');67// Using mocking of child_process.spawn. Not fully supported yet in esm by node:test so run as cjs.89// Using mock to allow try/catch around what is otherwise out-of-stack error handling.10// Injecting errors, these are not end-to-end tests.1112function makeSystemError(code) {13 // We can not make an actual SystemError, but our usage is lightweight so easy to match.14 const err = new Error();15 err.code = code;16 return err;17}1819// Suppress false positive warnings due to use of testOrSkipOnWindows2021const testOrSkipOnWindows = process.platform === 'win32' ? test.skip : test;2223describe('executable subcommand missing file handling ', () => {24 testOrSkipOnWindows(25 'when subcommand executable missing (ENOENT) then throw custom message',26 (t) => {27 // If the command is not found, we show a custom error with an explanation and offer28 // some advice for possible fixes.29 const mockProcess = new EventEmitter();30 t.mock.method(childProcess, 'spawn', () => {31 return mockProcess;32 });33 const program = new commander.Command();34 program.exitOverride();35 program.command('executable', 'executable description');36 program.parse(['executable'], { from: 'user' });37 assert.throws(() => {38 mockProcess.emit('error', makeSystemError('ENOENT'));39 }, /use the executableFile option to supply a custom name/); // part of custom message40 },41 );4243 testOrSkipOnWindows(44 'when subcommand executable not executable (EACCES) then throw custom message',45 (t) => {46 const mockProcess = new EventEmitter();47 t.mock.method(childProcess, 'spawn', () => {48 return mockProcess;49 });50 const program = new commander.Command();51 program.exitOverride();52 program.command('executable', 'executable description');53 program.parse(['executable'], { from: 'user' });54 assert.throws(() => {55 mockProcess.emit('error', makeSystemError('EACCES'));56 }, /not executable/); // part of custom message57 },58 );5960 test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', (t) => {61 // The existing behaviour is to just silently fail for unexpected errors, as it is happening62 // asynchronously in spawned process and client can not catch errors.63 const mockProcess = new EventEmitter();64 t.mock.method(childProcess, 'spawn', () => {65 return mockProcess;66 });67 const program = new commander.Command();68 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn69 program.exitOverride((err) => {70 throw err;71 });72 program.command('executable', 'executable description');73 program.parse(['executable'], { from: 'user' });74 assert.throws(75 () => {76 mockProcess.emit('error', makeSystemError('OTHER'));77 },78 (err) => {79 assert.equal(err.code, 'commander.executeSubCommandAsync');80 assert.equal(err.nestedError.code, 'OTHER');81 return true;82 },83 );84 });8586 test('when subcommand executable fails with other error then exit', (t) => {87 // The existing behaviour is to just silently fail for unexpected errors, as it is happening88 // asynchronously in spawned process and client can not catch errors.89 const mockProcess = new EventEmitter();90 t.mock.method(childProcess, 'spawn', () => {91 return mockProcess;92 });93 const exitSpy = t.mock.method(process, 'exit', () => {});94 const program = new commander.Command();95 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn96 program.command('executable', 'executable description');97 program.parse(['executable'], { from: 'user' });98 mockProcess.emit('error', makeSystemError('OTHER'));99 assert.equal(exitSpy.mock.callCount(), 1);100 const callArgs = exitSpy.mock.calls[0].arguments;101 assert.equal(callArgs[0], 1);102 });103});