Skip to content
Myr

command.executableSubcommand.search.test.js

This file

Path

tests/command.executableSubcommand.search.test.js

269 lines · 10.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–269why, 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}2324const describeOrSkipOnWindows = (process.platform === 'win32') ? describe.skip : describe;2526describe('search for subcommand', () => {27 let spawnSpy;28 let existsSpy;2930 beforeAll(() => {31 spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => {32 return {33 on: () => {},34 killed: true35 };36 });37 });3839 beforeEach(() => {40 existsSpy = jest.spyOn(fs, 'existsSync');41 });4243 afterEach(() => {44 spawnSpy.mockClear();45 existsSpy.mockRestore();46 });4748 afterAll(() => {49 spawnSpy.mockRestore();50 });5152 describe('whether perform search for local files', () => {53 beforeEach(() => {54 existsSpy.mockImplementation(() => false);55 });5657 test('when no script arg or executableDir then no search for local file', () => {58 const program = new commander.Command();59 program.name('pm');60 program.command('sub', 'executable description');61 program.parse(['sub'], { from: 'user' });6263 expect(existsSpy).not.toHaveBeenCalled();64 });6566 test('when script arg then search for local files', () => {67 const program = new commander.Command();68 program.name('pm');69 program.command('sub', 'executable description');70 program.parse(['node', 'script-name', 'sub']);7172 expect(existsSpy).toHaveBeenCalled();73 });7475 test('when executableDir then search for local files)', () => {76 const program = new commander.Command();77 program.name('pm');78 program.executableDir(__dirname);79 program.command('sub', 'executable description');80 program.parse(['sub'], { from: 'user' });8182 expect(existsSpy).toHaveBeenCalled();83 });84 });8586 // We always use node on Windows, and don't spawn executable as the command (which may be a feature or a shortcoming!?).87 describeOrSkipOnWindows('subcommand command name with no matching local file (non-Windows)', () => {88 beforeEach(() => {89 existsSpy.mockImplementation(() => false);90 });9192 test('when named pm and no script arg or executableDir then spawn pm-sub as command', () => {93 const program = new commander.Command();94 program.name('pm');95 program.command('sub', 'executable description');96 program.parse(['sub'], { from: 'user' });9798 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');99 });100101 test('when named pm and script arg then still spawn pm-sub as command', () => {102 const program = new commander.Command();103 program.name('pm');104 program.command('sub', 'executable description');105 program.parse(['node', 'script-name', 'sub']);106107 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');108 });109110 test('when no name and script arg then spawn script-sub as command', () => {111 const program = new commander.Command();112 program.command('sub', 'executable description');113 program.parse(['node', 'script.js', 'sub']);114115 expect(extractMockSpawnCommand(spawnSpy)).toEqual('script-sub');116 });117118 test('when named pm and script arg and executableFile then spawn executableFile as command', () => {119 const program = new commander.Command();120 program.command('sub', 'executable description', { executableFile: 'myExecutable' });121 program.parse(['node', 'script.js', 'sub']);122123 expect(extractMockSpawnCommand(spawnSpy)).toEqual('myExecutable');124 });125 });126127 describe('subcommand command name with matching local file', () => {128 test('when construct with name pm and script arg then spawn local pm-sub.js', () => {129 const program = new commander.Command('pm');130 program.command('sub', 'executable description');131132 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');133 existsSpy.mockImplementation((path) => path === localPath);134 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);135136 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);137 });138139 test('when name pm and script arg then spawn local pm-sub.js', () => {140 const program = new commander.Command();141 program.name('pm');142 program.command('sub', 'executable description');143144 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');145 existsSpy.mockImplementation((path) => path === localPath);146 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);147148 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);149 });150151 test('when script arg then spawn local script-sub.js', () => {152 const program = new commander.Command();153 program.command('sub', 'executable description');154155 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');156 existsSpy.mockImplementation((path) => path === localPath);157 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);158159 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);160 });161162 test('when name pm and script arg and only script-sub.js then fallback to spawn local script-sub.js', () => {163 const program = new commander.Command();164 program.name('pm');165 program.command('sub', 'executable description');166167 // Fallback for compatibility with Commander <= v8168 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');169 existsSpy.mockImplementation((path) => path === localPath);170 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);171172 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);173 });174175 test('when name pm and executableDir then spawn local pm-sub.js', () => {176 const program = new commander.Command();177 program.name('pm');178 program.command('sub', 'executable description');179180 const execDir = path.resolve(gLocalDirectory, 'exec-dir');181 program.executableDir(execDir);182 const localPath = path.resolve(execDir, 'pm-sub.js');183 existsSpy.mockImplementation((path) => path === localPath);184 program.parse(['sub'], { from: 'user' });185186 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);187 });188189 test('when script arg and relative executableDir then spawn relative script-sub.js', () => {190 const program = new commander.Command();191 program.command('sub', 'executable description');192193 const execDir = 'exec-dir';194 program.executableDir(execDir);195 const localPath = path.resolve(gLocalDirectory, execDir, 'script-sub.js');196 existsSpy.mockImplementation((path) => path === localPath);197 program.parse(['node', path.resolve(gLocalDirectory, 'script'), 'sub']);198199 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);200 });201202 test('when script arg and absolute executableDir then spawn absolute script-sub.js', () => {203 const program = new commander.Command();204 program.command('sub', 'executable description');205206 const execDir = path.resolve(gLocalDirectory, 'exec-dir');207 program.executableDir(execDir);208 const localPath = path.resolve(execDir, 'script-sub.js');209 existsSpy.mockImplementation((path) => path === localPath);210 program.parse(['node', path.resolve(gLocalDirectory, 'script-Dir', 'script'), 'sub']);211212 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);213 });214215 test('when script arg is link and and link-sub relative to link target then spawn local link-sub', () => {216 const program = new commander.Command();217 program.command('sub', 'executable description');218219 const linkPath = path.resolve(gLocalDirectory, 'link', 'link');220 const scriptPath = path.resolve(gLocalDirectory, 'script', 'script.js');221 const scriptSubPath = path.resolve(gLocalDirectory, 'script', 'link-sub.js');222 const realPathSyncSpy = jest.spyOn(fs, 'realpathSync').mockImplementation((path) => {223 return path === linkPath ? scriptPath : linkPath;224 });225 existsSpy.mockImplementation((path) => path === scriptSubPath);226 program.parse(['node', linkPath, 'sub']);227 realPathSyncSpy.mockRestore();228229 expect(extractMockSpawnArgs(spawnSpy)).toEqual([scriptSubPath]);230 });231232 test('when name pm and script arg and relative executableFile then spawn local exec.js', () => {233 const program = new commander.Command('pm');234 const localPath = path.join('relative', 'exec.js');235 const absolutePath = path.resolve(gLocalDirectory, localPath);236 program.command('sub', 'executable description', { executableFile: localPath });237238 existsSpy.mockImplementation((path) => path === absolutePath);239 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);240241 expect(extractMockSpawnArgs(spawnSpy)).toEqual([absolutePath]);242 });243244 test('when name pm and script arg and absolute executableFile then spawn local exec.js', () => {245 const program = new commander.Command('pm');246 const localPath = path.resolve(gLocalDirectory, 'absolute', 'exec.js');247 program.command('sub', 'executable description', { executableFile: localPath });248249 existsSpy.mockImplementation((path) => path === localPath);250 program.parse(['node', path.resolve(gLocalDirectory, 'script.js'), 'sub']);251252 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);253 });254 });255256 describe('search for local file', () => {257 test('when script arg then search for local script-sub.js, .ts, .tsx, .mpjs, .cjs', () => {258 existsSpy.mockImplementation((path) => false);259 const program = new commander.Command();260 program.command('sub', 'executable description');261 const scriptPath = path.resolve(gLocalDirectory, 'script');262 program.parse(['node', scriptPath, 'sub']);263 const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];264 sourceExt.forEach((ext) => {265 expect(existsSpy).toHaveBeenCalledWith(path.resolve(gLocalDirectory, `script-sub${ext}`));266 });267 });268 });269});