Skip to content
Myr

command.executableSubcommand.search.test.js

This file

Path

tests/command.executableSubcommand.search.test.js

320 lines · 11.4 KiB · JavaScript

Last changed by

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

History of this file

Take it

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 1–23why, one step back
1const childProcess = require('child_process');
2const fs = require('fs');3const path = require('path');4const commander = require('../');56// This file does in-process mocking. Bit clumsy but faster and less external clutter than using fixtures.7// See also command.executableSubcommand.lookup.test.js for tests using fixtures.89const gLocalDirectory = path.resolve(__dirname, 'fixtures'); // Real directory, although not actually searching for files in it.1011function extractMockSpawnArgs(mock) {12 expect(mock).toHaveBeenCalled();13 // non-Win, launchWithNode: childProcess.spawn(process.argv[0], args, { stdio: 'inherit' });14 // Win always: childProcess.spawn(process.execPath, args, { stdio: 'inherit' });15 return mock.mock.calls[0][1];16}1718function extractMockSpawnCommand(mock) {19 expect(mock).toHaveBeenCalled();20 // child_process.spawn(command[, args][, options])21 return mock.mock.calls[0][0];22}23

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 24–25why, one step back
24const describeOrSkipOnWindows =
25 process.platform === 'win32' ? describe.skip : describe;

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 26–34why, one step back
26
27describe('search for subcommand', () => {28 let spawnSpy;29 let existsSpy;3031 beforeAll(() => {32 spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {33 return {34 on: () => {},

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 35why, one step back
35 killed: true,

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 36–52why, one step back
36 };
37 });38 });3940 beforeEach(() => {41 existsSpy = jest.spyOn(fs, 'existsSync');42 });4344 afterEach(() => {45 spawnSpy.mockClear();46 existsSpy.mockRestore();47 });4849 afterAll(() => {50 spawnSpy.mockRestore();51 });52

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

line 53why, one step back
53 // fs.existsSync gets called on Windows outside the search, so skip the tests (or come up with a different way of checking).

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 54–60why, one step back
54 describe('whether perform search for local files', () => {
55 beforeEach(() => {56 existsSpy.mockImplementation(() => false);57 });5859 test('when no script arg or executableDir then no search for local file', () => {60 const program = new commander.Command();

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

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

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 62–70why, one step back
62 program.name('pm');
63 program.command('sub', 'executable description');64 program.parse(['sub'], { from: 'user' });6566 expect(existsSpy).not.toHaveBeenCalled();67 });6869 test('when script arg then search for local files', () => {70 const program = new commander.Command();

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

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

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 72–80why, one step back
72 program.name('pm');
73 program.command('sub', 'executable description');74 program.parse(['node', 'script-name', 'sub']);7576 expect(existsSpy).toHaveBeenCalled();77 });7879 test('when executableDir then search for local files)', () => {80 const program = new commander.Command();

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

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

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 82–91why, one step back
82 program.name('pm');
83 program.executableDir(__dirname);84 program.command('sub', 'executable description');85 program.parse(['sub'], { from: 'user' });8687 expect(existsSpy).toHaveBeenCalled();88 });89 });9091 // We always use node on Windows, and don't spawn executable as the command (which may be a feature or a shortcoming!?).

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 92–97why, one step back
92 describeOrSkipOnWindows(
93 'subcommand command name with no matching local file (non-Windows)',94 () => {95 beforeEach(() => {96 existsSpy.mockImplementation(() => false);97 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 98why, one step back
98

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 99–103why, one step back
99 test('when named pm and no script arg or executableDir then spawn pm-sub as command', () => {
100 const program = new commander.Command();101 program.name('pm');102 program.command('sub', 'executable description');103 program.parse(['sub'], { from: 'user' });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 104why, one step back
104

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 105–106why, one step back
105 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');
106 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 107why, one step back
107

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 108–112why, one step back
108 test('when named pm and script arg then still spawn pm-sub as command', () => {
109 const program = new commander.Command();110 program.name('pm');111 program.command('sub', 'executable description');112 program.parse(['node', 'script-name', 'sub']);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 113why, one step back
113

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 114–115why, one step back
114 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');
115 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 116why, one step back
116

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 117–120why, one step back
117 test('when no name and script arg then spawn script-sub as command', () => {
118 const program = new commander.Command();119 program.command('sub', 'executable description');120 program.parse(['node', 'script.js', 'sub']);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 121why, one step back
121

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 122–123why, one step back
122 expect(extractMockSpawnCommand(spawnSpy)).toEqual('script-sub');
123 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 124why, one step back
124

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 125–130why, one step back
125 test('when named pm and script arg and executableFile then spawn executableFile as command', () => {
126 const program = new commander.Command();127 program.command('sub', 'executable description', {128 executableFile: 'myExecutable',129 });130 program.parse(['node', 'script.js', 'sub']);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 131why, one step back
131

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 132–135why, one step back
132 expect(extractMockSpawnCommand(spawnSpy)).toEqual('myExecutable');
133 });134 },135 );

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 136–143why, one step back
136
137 describe('subcommand command name with matching local file', () => {138 test('when construct with name pm and script arg then spawn local pm-sub.js', () => {139 const program = new commander.Command('pm');140 program.command('sub', 'executable description');141142 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');143 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 144–148why, one step back
144 program.parse([
145 'node',146 path.resolve(gLocalDirectory, 'script.js'),147 'sub',148 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 149–159why, one step back
149
150 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);151 });152153 test('when name pm and script arg then spawn local pm-sub.js', () => {154 const program = new commander.Command();155 program.name('pm');156 program.command('sub', 'executable description');157158 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');159 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 160–164why, one step back
160 program.parse([
161 'node',162 path.resolve(gLocalDirectory, 'script.js'),163 'sub',164 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 165–174why, one step back
165
166 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);167 });168169 test('when script arg then spawn local script-sub.js', () => {170 const program = new commander.Command();171 program.command('sub', 'executable description');172173 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');174 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 175–179why, one step back
175 program.parse([
176 'node',177 path.resolve(gLocalDirectory, 'script.js'),178 'sub',179 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 180–191why, one step back
180
181 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);182 });183184 test('when name pm and script arg and only script-sub.js then fallback to spawn local script-sub.js', () => {185 const program = new commander.Command();186 program.name('pm');187 program.command('sub', 'executable description');188189 // Fallback for compatibility with Commander <= v8190 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');191 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 192–196why, one step back
192 program.parse([
193 'node',194 path.resolve(gLocalDirectory, 'script.js'),195 'sub',196 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 197–235why, one step back
197
198 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);199 });200201 test('when name pm and executableDir then spawn local pm-sub.js', () => {202 const program = new commander.Command();203 program.name('pm');204 program.command('sub', 'executable description');205206 const execDir = path.resolve(gLocalDirectory, 'exec-dir');207 program.executableDir(execDir);208 const localPath = path.resolve(execDir, 'pm-sub.js');209 existsSpy.mockImplementation((path) => path === localPath);210 program.parse(['sub'], { from: 'user' });211212 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);213 });214215 test('when script arg and relative executableDir then spawn relative script-sub.js', () => {216 const program = new commander.Command();217 program.command('sub', 'executable description');218219 const execDir = 'exec-dir';220 program.executableDir(execDir);221 const localPath = path.resolve(gLocalDirectory, execDir, 'script-sub.js');222 existsSpy.mockImplementation((path) => path === localPath);223 program.parse(['node', path.resolve(gLocalDirectory, 'script'), 'sub']);224225 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);226 });227228 test('when script arg and absolute executableDir then spawn absolute script-sub.js', () => {229 const program = new commander.Command();230 program.command('sub', 'executable description');231232 const execDir = path.resolve(gLocalDirectory, 'exec-dir');233 program.executableDir(execDir);234 const localPath = path.resolve(execDir, 'script-sub.js');235 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 236–240why, one step back
236 program.parse([
237 'node',238 path.resolve(gLocalDirectory, 'script-Dir', 'script'),239 'sub',240 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 241–250why, one step back
241
242 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);243 });244245 test('when script arg is link and and link-sub relative to link target then spawn local link-sub', () => {246 const program = new commander.Command();247 program.command('sub', 'executable description');248249 const linkPath = path.resolve(gLocalDirectory, 'link', 'link');250 const scriptPath = path.resolve(gLocalDirectory, 'script', 'script.js');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 251–260why, one step back
251 const scriptSubPath = path.resolve(
252 gLocalDirectory,253 'script',254 'link-sub.js',255 );256 const realPathSyncSpy = jest257 .spyOn(fs, 'realpathSync')258 .mockImplementation((path) => {259 return path === linkPath ? scriptPath : linkPath;260 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 261–271why, one step back
261 existsSpy.mockImplementation((path) => path === scriptSubPath);
262 program.parse(['node', linkPath, 'sub']);263 realPathSyncSpy.mockRestore();264265 expect(extractMockSpawnArgs(spawnSpy)).toEqual([scriptSubPath]);266 });267268 test('when name pm and script arg and relative executableFile then spawn local exec.js', () => {269 const program = new commander.Command('pm');270 const localPath = path.join('relative', 'exec.js');271 const absolutePath = path.resolve(gLocalDirectory, localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 272–274why, one step back
272 program.command('sub', 'executable description', {
273 executableFile: localPath,274 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 275–276why, one step back
275
276 existsSpy.mockImplementation((path) => path === absolutePath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 277–281why, one step back
277 program.parse([
278 'node',279 path.resolve(gLocalDirectory, 'script.js'),280 'sub',281 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 282–288why, one step back
282
283 expect(extractMockSpawnArgs(spawnSpy)).toEqual([absolutePath]);284 });285286 test('when name pm and script arg and absolute executableFile then spawn local exec.js', () => {287 const program = new commander.Command('pm');288 const localPath = path.resolve(gLocalDirectory, 'absolute', 'exec.js');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 289–291why, one step back
289 program.command('sub', 'executable description', {
290 executableFile: localPath,291 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 292–293why, one step back
292
293 existsSpy.mockImplementation((path) => path === localPath);

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 294–298why, one step back
294 program.parse([
295 'node',296 path.resolve(gLocalDirectory, 'script.js'),297 'sub',298 ]);

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 299–307why, one step back
299
300 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);301 });302 });303304 describe('search for local file', () => {305 test('when script arg then search for local script-sub.js, .ts, .tsx, .mpjs, .cjs', () => {306 existsSpy.mockImplementation((path) => false);307 const program = new commander.Command();

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

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

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 309–313why, one step back
309 program.command('sub', 'executable description');
310 const scriptPath = path.resolve(gLocalDirectory, 'script');311 program.parse(['node', scriptPath, 'sub']);312 const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];313 sourceExt.forEach((ext) => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 314–316why, one step back
314 expect(existsSpy).toHaveBeenCalledWith(
315 path.resolve(gLocalDirectory, `script-sub${ext}`),316 );

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 317–320why, one step back
317 });
318 });319 });320});