command.executableSubcommand.search.test.js
This file
Path
tests/command.executableSubcommand.search.test.js
315 lines · 10.9 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
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;Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
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,Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 36–87why, 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 });5253 describe('whether perform search for local files', () => {54 beforeEach(() => {55 existsSpy.mockImplementation(() => false);56 });5758 test('when no script arg or executableDir then no search for local file', () => {59 const program = new commander.Command();60 program.name('pm');61 program.command('sub', 'executable description');62 program.parse(['sub'], { from: 'user' });6364 expect(existsSpy).not.toHaveBeenCalled();65 });6667 test('when script arg then search for local files', () => {68 const program = new commander.Command();69 program.name('pm');70 program.command('sub', 'executable description');71 program.parse(['node', 'script-name', 'sub']);7273 expect(existsSpy).toHaveBeenCalled();74 });7576 test('when executableDir then search for local files)', () => {77 const program = new commander.Command();78 program.name('pm');79 program.executableDir(__dirname);80 program.command('sub', 'executable description');81 program.parse(['sub'], { from: 'user' });8283 expect(existsSpy).toHaveBeenCalled();84 });85 });8687 // 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 88–93why, one step back88 describeOrSkipOnWindows(89 'subcommand command name with no matching local file (non-Windows)',90 () => {91 beforeEach(() => {92 existsSpy.mockImplementation(() => false);93 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 94why, one step back94Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 95–99why, one step back95 test('when named pm and no script arg or executableDir then spawn pm-sub as command', () => {96 const program = new commander.Command();97 program.name('pm');98 program.command('sub', 'executable description');99 program.parse(['sub'], { from: 'user' });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 100why, one step back100Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 101–102why, one step back101 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');102 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 103why, one step back103Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 104–108why, one step back104 test('when named pm and script arg then still spawn pm-sub as command', () => {105 const program = new commander.Command();106 program.name('pm');107 program.command('sub', 'executable description');108 program.parse(['node', 'script-name', 'sub']);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 109why, one step back109Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 110–111why, one step back110 expect(extractMockSpawnCommand(spawnSpy)).toEqual('pm-sub');111 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 112why, one step back112Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 113–116why, one step back113 test('when no name and script arg then spawn script-sub as command', () => {114 const program = new commander.Command();115 program.command('sub', 'executable description');116 program.parse(['node', 'script.js', 'sub']);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 117why, one step back117Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 118–119why, one step back118 expect(extractMockSpawnCommand(spawnSpy)).toEqual('script-sub');119 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 120why, one step back120Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 121–126why, one step back121 test('when named pm and script arg and executableFile then spawn executableFile as command', () => {122 const program = new commander.Command();123 program.command('sub', 'executable description', {124 executableFile: 'myExecutable',125 });126 program.parse(['node', 'script.js', 'sub']);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
line 127why, one step back127Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 128–131why, one step back128 expect(extractMockSpawnCommand(spawnSpy)).toEqual('myExecutable');129 });130 },131 );Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 132–139why, one step back132133 describe('subcommand command name with matching local file', () => {134 test('when construct with name pm and script arg then spawn local pm-sub.js', () => {135 const program = new commander.Command('pm');136 program.command('sub', 'executable description');137138 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');139 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 140–144why, one step back140 program.parse([141 'node',142 path.resolve(gLocalDirectory, 'script.js'),143 'sub',144 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 145–155why, one step back145146 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);147 });148149 test('when name pm and script arg then spawn local pm-sub.js', () => {150 const program = new commander.Command();151 program.name('pm');152 program.command('sub', 'executable description');153154 const localPath = path.resolve(gLocalDirectory, 'pm-sub.js');155 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 156–160why, one step back156 program.parse([157 'node',158 path.resolve(gLocalDirectory, 'script.js'),159 'sub',160 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 161–170why, one step back161162 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);163 });164165 test('when script arg then spawn local script-sub.js', () => {166 const program = new commander.Command();167 program.command('sub', 'executable description');168169 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');170 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 171–175why, one step back171 program.parse([172 'node',173 path.resolve(gLocalDirectory, 'script.js'),174 'sub',175 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 176–187why, one step back176177 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);178 });179180 test('when name pm and script arg and only script-sub.js then fallback to spawn local script-sub.js', () => {181 const program = new commander.Command();182 program.name('pm');183 program.command('sub', 'executable description');184185 // Fallback for compatibility with Commander <= v8186 const localPath = path.resolve(gLocalDirectory, 'script-sub.js');187 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 188–192why, one step back188 program.parse([189 'node',190 path.resolve(gLocalDirectory, 'script.js'),191 'sub',192 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 193–231why, one step back193194 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);195 });196197 test('when name pm and executableDir then spawn local pm-sub.js', () => {198 const program = new commander.Command();199 program.name('pm');200 program.command('sub', 'executable description');201202 const execDir = path.resolve(gLocalDirectory, 'exec-dir');203 program.executableDir(execDir);204 const localPath = path.resolve(execDir, 'pm-sub.js');205 existsSpy.mockImplementation((path) => path === localPath);206 program.parse(['sub'], { from: 'user' });207208 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);209 });210211 test('when script arg and relative executableDir then spawn relative script-sub.js', () => {212 const program = new commander.Command();213 program.command('sub', 'executable description');214215 const execDir = 'exec-dir';216 program.executableDir(execDir);217 const localPath = path.resolve(gLocalDirectory, execDir, 'script-sub.js');218 existsSpy.mockImplementation((path) => path === localPath);219 program.parse(['node', path.resolve(gLocalDirectory, 'script'), 'sub']);220221 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);222 });223224 test('when script arg and absolute executableDir then spawn absolute script-sub.js', () => {225 const program = new commander.Command();226 program.command('sub', 'executable description');227228 const execDir = path.resolve(gLocalDirectory, 'exec-dir');229 program.executableDir(execDir);230 const localPath = path.resolve(execDir, 'script-sub.js');231 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 232–236why, one step back232 program.parse([233 'node',234 path.resolve(gLocalDirectory, 'script-Dir', 'script'),235 'sub',236 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 237–246why, one step back237238 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);239 });240241 test('when script arg is link and and link-sub relative to link target then spawn local link-sub', () => {242 const program = new commander.Command();243 program.command('sub', 'executable description');244245 const linkPath = path.resolve(gLocalDirectory, 'link', 'link');246 const scriptPath = path.resolve(gLocalDirectory, 'script', 'script.js');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 247–256why, one step back247 const scriptSubPath = path.resolve(248 gLocalDirectory,249 'script',250 'link-sub.js',251 );252 const realPathSyncSpy = jest253 .spyOn(fs, 'realpathSync')254 .mockImplementation((path) => {255 return path === linkPath ? scriptPath : linkPath;256 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 257–267why, one step back257 existsSpy.mockImplementation((path) => path === scriptSubPath);258 program.parse(['node', linkPath, 'sub']);259 realPathSyncSpy.mockRestore();260261 expect(extractMockSpawnArgs(spawnSpy)).toEqual([scriptSubPath]);262 });263264 test('when name pm and script arg and relative executableFile then spawn local exec.js', () => {265 const program = new commander.Command('pm');266 const localPath = path.join('relative', 'exec.js');267 const absolutePath = path.resolve(gLocalDirectory, localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 268–270why, one step back268 program.command('sub', 'executable description', {269 executableFile: localPath,270 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 271–272why, one step back271272 existsSpy.mockImplementation((path) => path === absolutePath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 273–277why, one step back273 program.parse([274 'node',275 path.resolve(gLocalDirectory, 'script.js'),276 'sub',277 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 278–284why, one step back278279 expect(extractMockSpawnArgs(spawnSpy)).toEqual([absolutePath]);280 });281282 test('when name pm and script arg and absolute executableFile then spawn local exec.js', () => {283 const program = new commander.Command('pm');284 const localPath = path.resolve(gLocalDirectory, 'absolute', 'exec.js');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 285–287why, one step back285 program.command('sub', 'executable description', {286 executableFile: localPath,287 });Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 288–289why, one step back288289 existsSpy.mockImplementation((path) => path === localPath);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 290–294why, one step back290 program.parse([291 'node',292 path.resolve(gLocalDirectory, 'script.js'),293 'sub',294 ]);Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 295–308why, one step back295296 expect(extractMockSpawnArgs(spawnSpy)).toEqual([localPath]);297 });298 });299300 describe('search for local file', () => {301 test('when script arg then search for local script-sub.js, .ts, .tsx, .mpjs, .cjs', () => {302 existsSpy.mockImplementation((path) => false);303 const program = new commander.Command();304 program.command('sub', 'executable description');305 const scriptPath = path.resolve(gLocalDirectory, 'script');306 program.parse(['node', scriptPath, 'sub']);307 const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];308 sourceExt.forEach((ext) => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 309–311why, one step back309 expect(existsSpy).toHaveBeenCalledWith(310 path.resolve(gLocalDirectory, `script-sub${ext}`),311 );Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c
lines 312–315why, one step back312 });313 });314 });315});