command.executableSubcommand.mock.test.js
This file
Path
tests/command.executableSubcommand.mock.test.js
99 lines · 3.7 KiB · JavaScript
Last changed by
Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
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');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 4–5why, one step back4const { test } = require('node:test');5const 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 informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 21–22why, one step back21testOrSkipOnWindows(22 'when subcommand executable missing (ENOENT) then throw custom message',Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 23why, one step back23 (t) => {Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 24–26why, one step back24 // If the command is not found, we show a custom error with an explanation and offer25 // some advice for possible fixes.26 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 27–29why, one step back27 t.mock.method(childProcess, 'spawn', () => {28 return mockProcess;29 });Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 30–33why, one step back30 const program = new commander.Command();31 program.exitOverride();32 program.command('executable', 'executable description');33 program.parse(['executable'], { from: 'user' });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 34why, one step back34 assert.throws(() => {Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 35why, one step back35 mockProcess.emit('error', makeSystemError('ENOENT'));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 36why, one step back36 }, /use the executableFile option to supply a custom name/); // part of custom messageAdd informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 37–41why, one step back37 },38);3940testOrSkipOnWindows(41 'when subcommand executable not executable (EACCES) then throw custom message',Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 42why, one step back42 (t) => {Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 43why, one step back43 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 44–46why, one step back44 t.mock.method(childProcess, 'spawn', () => {45 return mockProcess;46 });Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 47–50why, one step back47 const program = new commander.Command();48 program.exitOverride();49 program.command('executable', 'executable description');50 program.parse(['executable'], { from: 'user' });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 51why, one step back51 assert.throws(() => {Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 52why, one step back52 mockProcess.emit('error', makeSystemError('EACCES'));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 53why, one step back53 }, /not executable/); // part of custom messageAdd informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
lines 54–56why, one step back54 },55);56Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 57why, one step back57test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', (t) => {Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 58–60why, one step back58 // The existing behaviour is to just silently fail for unexpected errors, as it is happening59 // asynchronously in spawned process and client can not catch errors.60 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 61why, one step back61 t.mock.method(childProcess, 'spawn', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 62–63why, one step back62 return mockProcess;63 });Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 64why, one step back64 const program = new commander.Command();Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 65why, one step back65 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnCheck for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 66–70why, one step back66 program.exitOverride((err) => {67 throw err;68 });69 program.command('executable', 'executable description');70 program.parse(['executable'], { from: 'user' });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 71–80why, one step back71 assert.throws(72 () => {73 mockProcess.emit('error', makeSystemError('OTHER'));74 },75 (err) => {76 assert.equal(err.code, 'commander.executeSubCommandAsync');77 assert.equal(err.nestedError.code, 'OTHER');78 return true;79 },80 );Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 81–82why, one step back81});82Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 83why, one step back83test('when subcommand executable fails with other error then exit', (t) => {Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 84–86why, one step back84 // The existing behaviour is to just silently fail for unexpected errors, as it is happening85 // asynchronously in spawned process and client can not catch errors.86 const mockProcess = new EventEmitter();Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 87why, one step back87 t.mock.method(childProcess, 'spawn', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 88–89why, one step back88 return mockProcess;89 });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 90why, one step back90 const exitSpy = t.mock.method(process, 'exit', () => {});Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 91why, one step back91 const program = new commander.Command();Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 92why, one step back92 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnCheck for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
lines 93–95why, one step back93 program.command('executable', 'executable description');94 program.parse(['executable'], { from: 'user' });95 mockProcess.emit('error', makeSystemError('OTHER'));Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 96–98why, one step back96 assert.equal(exitSpy.mock.callCount(), 1);97 const callArgs = exitSpy.mock.calls[0].arguments;98 assert.equal(callArgs[0], 1);Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1
line 99why, one step back99});