Skip to content
Myr

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 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');

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

lines 4–5why, one step back
4const { 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 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 informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf

lines 21–22why, one step back
21testOrSkipOnWindows(
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 back
23 (t) => {

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

lines 24–26why, one step back
24 // If the command is not found, we show a custom error with an explanation and offer
25 // 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 back
27 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 back
30 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 back
34 assert.throws(() => {

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

line 35why, one step back
35 mockProcess.emit('error', makeSystemError('ENOENT'));

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

line 36why, one step back
36 }, /use the executableFile option to supply a custom name/); // part of custom message

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

lines 37–41why, one step back
37 },
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 back
42 (t) => {

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

line 43why, one step back
43 const mockProcess = new EventEmitter();

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

lines 44–46why, one step back
44 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 back
47 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 back
51 assert.throws(() => {

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

line 52why, one step back
52 mockProcess.emit('error', makeSystemError('EACCES'));

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

line 53why, one step back
53 }, /not executable/); // part of custom message

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

lines 54–56why, one step back
54 },
55);56

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

line 57why, one step back
57test('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 back
58 // The existing behaviour is to just silently fail for unexpected errors, as it is happening
59 // 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 back
61 t.mock.method(childProcess, 'spawn', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 62–63why, one step back
62 return mockProcess;
63 });

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

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

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

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

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

lines 66–70why, one step back
66 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 back
71 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 back
81});
82

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

line 83why, one step back
83test('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 back
84 // The existing behaviour is to just silently fail for unexpected errors, as it is happening
85 // 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 back
87 t.mock.method(childProcess, 'spawn', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 88–89why, one step back
88 return mockProcess;
89 });

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

line 90why, one step back
90 const exitSpy = t.mock.method(process, 'exit', () => {});

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

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

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

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

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

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