command.executableSubcommand.search.test.cjs
This file
Path
tests/command.executableSubcommand.search.test.cjs
317 lines · 12.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–317why, one step back1const childProcess = require('child_process');2const fs = require('fs');3const path = require('path');4const commander = require('../');5const { test, describe } = require('node:test');6const assert = require('node:assert/strict');78// Using mocking of child_process.spawn. Not fully supported yet in esm by node:test so run as cjs.910// This file does in-process mocking. Bit clumsy but faster and less external clutter than using fixtures.11// See also command.executableSubcommand.lookup.test.js for tests using fixtures.1213const gLocalDirectory = path.resolve(__dirname, 'fixtures'); // Real directory, although not actually searching for files in it.1415function extractMockSpawnArgs(spawnSpy) {16 assert.equal(spawnSpy.mock.callCount() > 0, true);17 // This is very fragile on implementation details of how subcommand spawned.18 // non-Win Jest, launchWithNode: childProcess.spawn(process.argv[0], args, { stdio: 'inherit' });19 // Win Jest always: childProcess.spawn(process.execPath, args, { stdio: 'inherit' });20 // node:test passes wild number of node args on front!2122 const callArgs = spawnSpy.mock.calls[0].arguments;23 return callArgs[1].slice(-1); // fingers crossed last arg is filename!24}2526function extractMockSpawnCommand(spawnSpy) {27 assert.equal(spawnSpy.mock.callCount() > 0, true);28 // child_process.spawn(command[, args][, options])29 const callArgs = spawnSpy.mock.calls[0].arguments;30 return callArgs[0];31}3233const describeOrSkipOnWindows =34 process.platform === 'win32' ? describe.skip : describe;3536describe('executable subcommand search behaviour', () => {37 function makeSpies(t, existsFn) {38 const spawnSpy = t.mock.method(childProcess, 'spawn', () => {39 return {40 on: () => {},41 killed: true,42 };43 });44 const existsSpy = t.mock.method(fs, 'existsSync', existsFn);45 return { spawnSpy, existsSpy };46 }4748 // fs.existsSync gets called on Windows outside the search, so skip the tests (or come up with a different way of checking).49 describe('whether perform search for local files', () => {50 test('when no script arg or executableDir then no search for local file', (t) => {51 const { existsSpy } = makeSpies(t, () => false);52 const program = new commander.Command();53 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn54 program.name('pm');55 program.command('sub', 'executable description');56 program.parse(['sub'], { from: 'user' });5758 assert.equal(existsSpy.mock.callCount(), 0);59 });6061 test('when script arg then search for local files', (t) => {62 const { existsSpy } = makeSpies(t, () => false);63 const program = new commander.Command();64 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn65 program.name('pm');66 program.command('sub', 'executable description');67 program.parse(['node', 'script-name', 'sub']);6869 assert.equal(existsSpy.mock.callCount() > 0, true);70 });7172 test('when executableDir then search for local files)', (t) => {73 const { existsSpy } = makeSpies(t, () => false);74 const program = new commander.Command();75 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn76 program.name('pm');77 program.executableDir(__dirname);78 program.command('sub', 'executable description');79 program.parse(['sub'], { from: 'user' });8081 assert.equal(existsSpy.mock.callCount() > 0, true);82 });83 });8485 // We always use node on Windows, and don't spawn executable as the command (which may be a feature or a shortcoming!?).86 describeOrSkipOnWindows(87 'subcommand command name with no matching local file (non-Windows)',88 () => {89 test('when named pm and no script arg or executableDir then spawn pm-sub as command', (t) => {90 const { spawnSpy } = makeSpies(t, () => false);91 const program = new commander.Command();92 program.name('pm');93 program.command('sub', 'executable description');94 program.parse(['sub'], { from: 'user' });9596 assert.equal(extractMockSpawnCommand(spawnSpy), 'pm-sub');97 });9899 test('when named pm and script arg then still spawn pm-sub as command', (t) => {100 const { spawnSpy } = makeSpies(t, () => false);101 const program = new commander.Command();102 program.name('pm');103 program.command('sub', 'executable description');104 program.parse(['node', 'script-name', 'sub']);105106 assert.equal(extractMockSpawnCommand(spawnSpy), 'pm-sub');107 });108109 test('when no name and script arg then spawn script-sub as command', (t) => {110 const { spawnSpy } = makeSpies(t, () => false);111 const program = new commander.Command();112 program.command('sub', 'executable description');113 program.parse(['node', 'script.js', 'sub']);114115 assert.equal(extractMockSpawnCommand(spawnSpy), 'script-sub');116 });117118 test('when named pm and script arg and executableFile then spawn executableFile as command', (t) => {119 const { spawnSpy } = makeSpies(t, () => false);120 const program = new commander.Command();121 program.command('sub', 'executable description', {122 executableFile: 'myExecutable',123 });124 program.parse(['node', 'script.js', 'sub']);125126 assert.equal(extractMockSpawnCommand(spawnSpy), 'myExecutable');127 });128 },129 );130131 describe('subcommand command name with matching local file', () => {132 test('when construct with name pm and script arg then spawn local pm-sub.js', (t) => {133 const program = new commander.Command('pm');134 program.command('sub', 'executable description');135136 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');137 const { spawnSpy } = makeSpies(t, (path) => path === localPath);138 program.parse([139 'node',140 path.resolve(gLocalDirectory, 'script.js'),141 'sub',142 ]);143144 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);145 });146147 test('when name pm and script arg then spawn local pm-sub.js', (t) => {148 const program = new commander.Command();149 program.name('pm');150 program.command('sub', 'executable description');151152 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');153 const { spawnSpy } = makeSpies(t, (path) => path === localPath);154 program.parse([155 'node',156 path.resolve(gLocalDirectory, 'script.js'),157 'sub',158 ]);159160 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);161 });162163 test('when script arg then spawn local script-sub.js', (t) => {164 const program = new commander.Command();165 program.command('sub', 'executable description');166167 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');168 const { spawnSpy } = makeSpies(t, (path) => path === localPath);169 program.parse([170 'node',171 path.resolve(gLocalDirectory, 'script.js'),172 'sub',173 ]);174175 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);176 });177178 test('when name pm and script arg and only script-sub.js then fallback to spawn local script-sub.js', (t) => {179 const program = new commander.Command();180 program.name('pm');181 program.command('sub', 'executable description');182183 // Fallback for compatibility with Commander <= v8184 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');185 const { spawnSpy } = makeSpies(t, (path) => path === localPath);186 program.parse([187 'node',188 path.resolve(gLocalDirectory, 'script.js'),189 'sub',190 ]);191192 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);193 });194195 test('when name pm and executableDir then spawn local pm-sub.js', (t) => {196 const program = new commander.Command();197 program.name('pm');198 program.command('sub', 'executable description');199200 const execDir = path.resolve(gLocalDirectory, 'exec-dir');201 program.executableDir(execDir);202 const localPath = path.resolve(execDir, 'pm-sub.js');203 const { spawnSpy } = makeSpies(t, (path) => path === localPath);204 program.parse(['sub'], { from: 'user' });205206 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);207 });208209 test('when script arg and relative executableDir then spawn relative script-sub.js', (t) => {210 const program = new commander.Command();211 program.command('sub', 'executable description');212213 const execDir = 'exec-dir';214 program.executableDir(execDir);215 const localPath = path.resolve(gLocalDirectory, execDir, 'script-sub.js');216 const { spawnSpy } = makeSpies(t, (path) => path === localPath);217 program.parse(['node', path.resolve(gLocalDirectory, 'script'), 'sub']);218219 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);220 });221222 test('when script arg and absolute executableDir then spawn absolute script-sub.js', (t) => {223 const program = new commander.Command();224 program.command('sub', 'executable description');225226 const execDir = path.resolve(gLocalDirectory, 'exec-dir');227 program.executableDir(execDir);228 const localPath = path.resolve(execDir, 'script-sub.js');229 const { spawnSpy } = makeSpies(t, (path) => path === localPath);230 program.parse([231 'node',232 path.resolve(gLocalDirectory, 'script-Dir', 'script'),233 'sub',234 ]);235236 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);237 });238239 test('when script arg is link and and link-sub relative to link target then spawn local link-sub', (t) => {240 const program = new commander.Command();241 program.command('sub', 'executable description');242243 const linkPath = path.resolve(gLocalDirectory, 'link', 'link');244 const scriptPath = path.resolve(gLocalDirectory, 'script', 'script.js');245 const scriptSubPath = path.resolve(246 gLocalDirectory,247 'script',248 'link-sub.js',249 );250 t.mock.method(fs, 'realpathSync', (path) => {251 return path === linkPath ? scriptPath : linkPath;252 });253 const { spawnSpy } = makeSpies(t, (path) => path === scriptSubPath);254 program.parse(['node', linkPath, 'sub']);255256 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [scriptSubPath]);257 });258259 test('when name pm and script arg and relative executableFile then spawn local exec.js', (t) => {260 const program = new commander.Command('pm');261 const localPath = path.join('relative', 'exec.js');262 const absolutePath = path.resolve(gLocalDirectory, localPath);263 program.command('sub', 'executable description', {264 executableFile: localPath,265 });266267 const { spawnSpy } = makeSpies(t, (path) => path === absolutePath);268 program.parse([269 'node',270 path.resolve(gLocalDirectory, 'script.js'),271 'sub',272 ]);273274 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [absolutePath]);275 });276277 test('when name pm and script arg and absolute executableFile then spawn local exec.js', (t) => {278 const program = new commander.Command('pm');279 const localPath = path.resolve(gLocalDirectory, 'absolute', 'exec.js');280 program.command('sub', 'executable description', {281 executableFile: localPath,282 });283284 const { spawnSpy } = makeSpies(t, (path) => path === localPath);285 program.parse([286 'node',287 path.resolve(gLocalDirectory, 'script.js'),288 'sub',289 ]);290291 assert.deepEqual(extractMockSpawnArgs(spawnSpy), [localPath]);292 });293 });294295 describe('search for local file', () => {296 test('when script arg then search for local script-sub.js, .ts, .tsx, .mpjs, .cjs', (t) => {297 const { existsSpy } = makeSpies(t, () => false);298 const program = new commander.Command();299 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn300 program.command('sub', 'executable description');301 const scriptPath = path.resolve(gLocalDirectory, 'script');302 program.parse(['node', scriptPath, 'sub']);303 const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];304 sourceExt.forEach((ext) => {305 const expectedPath = path.resolve(gLocalDirectory, `script-sub${ext}`);306 const wasCalled = existsSpy.mock.calls.some(307 (call) => call.arguments[0] === expectedPath,308 );309 assert.equal(310 wasCalled,311 true,312 `Expected existsSpy to be called with ${expectedPath}`,313 );314 });315 });316 });317});