Skip to content
Myr

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 file

Take 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 back
1const 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 back
4const { test, describe } = require('node:test');

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 5why, one step back
5const assert = require('node:assert/strict');

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

lines 6–16why, one step back
6
7// 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}16

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

line 17why, one step back
17// Suppress false positive warnings due to use of testOrSkipOnWindows

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

line 18why, one step back
18

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

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

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

line 20why, one step back
20

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 21–60why, one step back
21describe('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 back
61 const mockProcess = new EventEmitter();

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 62–64why, one step back
62 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 back
65 const program = new commander.Command();

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 66–69why, one step back
66 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn
67 program.exitOverride((err) => {68 throw err;69 });

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

lines 70–71why, one step back
70 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 back
72 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 back
83

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 84–86why, one step back
84 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 back
87 const mockProcess = new EventEmitter();

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 88–90why, one step back
88 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 back
91 const exitSpy = t.mock.method(process, 'exit', () => {});

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

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

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

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

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

lines 94–95why, one step back
94 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 back
96 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 back
100 });

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

line 101why, one step back
101});