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 fileTake it
The walk stopped after 200 commits: lines older than cc2db5b point at it.
Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 1–23why, one step back1const 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}23Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 24–25why, one step back24const describeOrSkipOnWindows =25 process.platform === 'win32' ? describe.skip : describe;Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 26–34why, one step back2627describe('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 back35 killed: true,Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 36–52why, one step back36 };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 });52Add informative message for missing executable on Windows (#2291)John Geebefore adoptiona8ef5cf
line 53why, one step back53 // fs.existsSync gets called on Windows outside the search, so skip the tests (or come up with a different way of checking).Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 54–60why, one step back54 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 back61 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnAdd optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 62–70why, one step back62 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 back71 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnAdd optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 72–80why, one step back72 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 back81 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnAdd optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 82–91why, one step back82 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 back92 describeOrSkipOnWindows(93 'subcommand command name with no matching local file (non-Windows)',94 () => {95 beforeEach(() => {96 existsSpy.mockImplementation(() => false);97 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 98why, one step back98Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 99–103why, one step back99 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' });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 104why, one step back104Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 105–106why, one step back105 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');106 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 107why, one step back107Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 108–112why, one step back108 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']);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 113why, one step back113Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 114–115why, one step back114 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');115 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 116why, one step back116Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 117–120why, one step back117 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']);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 121why, one step back121Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 122–123why, one step back122 expect(extractMockSpawnCommand(spawnSpy)).toEqual('script-sub');123 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 124why, one step back124Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 125–130why, one step back125 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']);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 131why, one step back131Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 132–135why, one step back132 expect(extractMockSpawnCommand(spawnSpy)).toEqual('myExecutable');133 });134 },135 );Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 136–143why, one step back136137 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 back144 program.parse([145 'node',146 path.resolve(gLocalDirectory, 'script.js'),147 'sub',148 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 149–159why, one step back149150 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 back160 program.parse([161 'node',162 path.resolve(gLocalDirectory, 'script.js'),163 'sub',164 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 165–174why, one step back165166 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 back175 program.parse([176 'node',177 path.resolve(gLocalDirectory, 'script.js'),178 'sub',179 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 180–191why, one step back180181 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 back192 program.parse([193 'node',194 path.resolve(gLocalDirectory, 'script.js'),195 'sub',196 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 197–235why, one step back197198 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 back236 program.parse([237 'node',238 path.resolve(gLocalDirectory, 'script-Dir', 'script'),239 'sub',240 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 241–250why, one step back241242 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 back251 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 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 261–271why, one step back261 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 back272 program.command('sub', 'executable description', {273 executableFile: localPath,274 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 275–276why, one step back275276 existsSpy.mockImplementation((path) => path === absolutePath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 277–281why, one step back277 program.parse([278 'node',279 path.resolve(gLocalDirectory, 'script.js'),280 'sub',281 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 282–288why, one step back282283 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 back289 program.command('sub', 'executable description', {290 executableFile: localPath,291 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 292–293why, one step back292293 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 294–298why, one step back294 program.parse([295 'node',296 path.resolve(gLocalDirectory, 'script.js'),297 'sub',298 ]);Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 299–307why, one step back299300 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 back308 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawnAdd optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 309–313why, one step back309 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 back314 expect(existsSpy).toHaveBeenCalledWith(315 path.resolve(gLocalDirectory, `script-sub${ext}`),316 );Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 317–320why, one step back317 });318 });319 });320});