Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by John Gee · committed by GitHub · parent 02c603e · Browse files · 3 files+83 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+83 added−40 removed

  • tests:+53 added−32 removed
  • other:+30 added−8 removed

3 files+83 added−40 removed

modified:lib/command.js+30 added-8 removed
1102 unmodified lines
1103 return this;1104 }11051106 /**1107 * Throw if expected executable is missing. Add lots of help for author.1108 *1109 * @param {string} executableFile1110 * @param {string} executableDir1111 * @param {string} subcommandName1112 */1113 _checkForMissingExecutable(executableFile, executableDir, subcommandName) {1114 if (fs.existsSync(executableFile)) return;11151116 const executableDirMessage = executableDir1117 ? `searched for local subcommand relative to directory '${executableDir}'`1118 : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory';1119 const executableMissing = `'${executableFile}' does not exist1120 - if '${subcommandName}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead1121 - if the default executable name is not suitable, use the executableFile option to supply a custom name or path1122 - ${executableDirMessage}`;1123 throw new Error(executableMissing);1124 }11251126 /**1127 * Execute a sub-command executable.1128 *1129 * @private1130 */
74 unmodified lines
1205 } else {1206 proc = childProcess.spawn(executableFile, args, { stdio: 'inherit' });1207 }1208 } else {1209 this._checkForMissingExecutable(1210 executableFile,1211 executableDir,1212 subcommand._name,1213 );1214 args.unshift(executableFile);1215 // add executable arguments to spawn1216 args = incrementNodeInspectorPort(process.execArgv).concat(args);1217 proc = childProcess.spawn(process.execPath, args, { stdio: 'inherit' });
30 unmodified lines
1248 });1249 proc.on('error', (err) => {1250 // @ts-ignore: because err.code is an unknown property1251 if (err.code === 'ENOENT') {1227 const executableDirMessage = executableDir1228 ? `searched for local subcommand relative to directory '${executableDir}'`1229 : 'no directory for search for local subcommand, use .executableDir() to supply a custom directory';1230 const executableMissing = `'${executableFile}' does not exist1231 - if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead1232 - if the default executable name is not suitable, use the executableFile option to supply a custom name or path1233 - ${executableDirMessage}`;1234 throw new Error(executableMissing);1252 this._checkForMissingExecutable(1253 executableFile,1254 executableDir,1255 subcommand._name,1256 );1257 // @ts-ignore: because err.code is an unknown property1258 } else if (err.code === 'EACCES') {1259 throw new Error(`'${executableFile}' not executable`);1260 }
1347 unmodified lines
modified:tests/command.executableSubcommand.mock.test.js+48 added-32 removed
modified:tests/command.executableSubcommand.search.test.js+5 added
48 unmodified lines
49 afterAll(() => {50 spawnSpy.mockRestore();51 });5253 // fs.existsSync gets called on Windows outside the search, so skip the tests (or come up with a different way of checking).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();61 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn62 program.name('pm');63 program.command('sub', 'executable description');64 program.parse(['sub'], { from: 'user' });65
1 unmodified line
67 });6869 test('when script arg then search for local files', () => {70 const program = new commander.Command();71 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn72 program.name('pm');73 program.command('sub', 'executable description');74 program.parse(['node', 'script-name', 'sub']);75
1 unmodified line
77 });7879 test('when executableDir then search for local files)', () => {80 const program = new commander.Command();81 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn82 program.name('pm');83 program.executableDir(__dirname);84 program.command('sub', 'executable description');85 program.parse(['sub'], { from: 'user' });
218 unmodified lines
304 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();308 program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn309 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'];
8 unmodified lines