command.executableSubcommand.mock.test.js
This file
Path
tests/command.executableSubcommand.mock.test.js
101 lines · 3.9 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
The walk stopped after 200 commits: lines older than bf205d1 point at it.
Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 1–3why, one step back1const commander = require('../');2const childProcess = require('child_process');3const EventEmitter = require('events');Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 4why, one step back4const { test, describe } = require('node:test');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 5why, one step back5const assert = require('node:assert/strict');Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 6–16why, one step back67// Using mock to allow try/catch around what is otherwise out-of-stack error handling.8// Injecting errors, these are not end-to-end tests.910function makeSystemError(code) {11 // We can not make an actual SystemError, but our usage is lightweight so easy to match.12 const err = new Error();13 err.code = code;14 return err;15}16Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 17why, one step back17// Suppress false positive warnings due to use of testOrSkipOnWindowsCheck for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 18why, one step back18Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 19why, one step back19const testOrSkipOnWindows = process.platform === 'win32' ? test.skip : test;Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 20why, one step back20Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 21–60why, one step back21describe('executable subcommand missing file handling ', () => {22 testOrSkipOnWindows(23 'when subcommand executable missing (ENOENT) then throw custom message',24 (t) => {25 // If the command is not found, we show a custom error with an explanation and offer26 // some advice for possible fixes.27 const mockProcess = new EventEmitter();28 t.mock.method(childProcess, 'spawn', () => {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 assert.throws(() => {36 mockProcess.emit('error', makeSystemError('ENOENT'));37 }, /use the executableFile option to supply a custom name/); // part of custom message38 },39 );4041 testOrSkipOnWindows(42 'when subcommand executable not executable (EACCES) then throw custom message',43 (t) => {44 const mockProcess = new EventEmitter();45 t.mock.method(childProcess, 'spawn', () => {46 return mockProcess;47 });48 const program = new commander.Command();49 program.exitOverride();50 program.command('executable', 'executable description');51 program.parse(['executable'], { from: 'user' });52 assert.throws(() => {53 mockProcess.emit('error', makeSystemError('EACCES'));54 }, /not executable/); // part of custom message55 },56 );5758 test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', (t) => {59 // The existing behaviour is to just silently fail for unexpected errors, as it is happening60 // asynchronously in spawned process and client can not catch errors.Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 61why, one step back61 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 62–64why, one step back62 t.mock.method(childProcess, 'spawn', () => {63 return mockProcess;64 });Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 65why, one step back65 const program = new commander.Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 66–69why, one step back66 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn67 program.exitOverride((err) => {68 throw err;69 });Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 70–71why, one step back70 program.command('executable', 'executable description');71 program.parse(['executable'], { from: 'user' });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 72–82why, one step back72 assert.throws(73 () => {74 mockProcess.emit('error', makeSystemError('OTHER'));75 },76 (err) => {77 assert.equal(err.code, 'commander.executeSubCommandAsync');78 assert.equal(err.nestedError.code, 'OTHER');79 return true;80 },81 );82 });Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 83why, one step back83Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 84–86why, one step back84 test('when subcommand executable fails with other error then exit', (t) => {85 // The existing behaviour is to just silently fail for unexpected errors, as it is happening86 // asynchronously in spawned process and client can not catch errors.Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 87why, one step back87 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 88–90why, one step back88 t.mock.method(childProcess, 'spawn', () => {89 return mockProcess;90 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 91why, one step back91 const exitSpy = t.mock.method(process, 'exit', () => {});Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 92why, one step back92 const program = new commander.Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 93why, one step back93 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnAdd informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 94–95why, one step back94 program.command('executable', 'executable description');95 program.parse(['executable'], { from: 'user' });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 96–99why, one step back96 mockProcess.emit('error', makeSystemError('OTHER'));97 assert.equal(exitSpy.mock.callCount(), 1);98 const callArgs = exitSpy.mock.calls[0].arguments;99 assert.equal(callArgs[0], 1);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 100why, one step back100 });Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 101why, one step back101});