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
Provenance
- Commit
a8ef5cf- Tree
def0b67- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
02c603e
3 files changed+83 added−40 removed
modified:lib/command.js+30 added-8 removed
1102 unmodified lines1103 return this;1104 }1105 1106 /**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;1115+1116+ 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+ }1125+1126+ /**1127 * Execute a sub-command executable.1128 *1129 * @private1130 */74 unmodified lines1205 } 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 lines1248 });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 linesmodified:tests/command.executableSubcommand.mock.test.js+48 added-32 removed
10 unmodified lines11121314151617181920212223242526272829301516173233343536373839404142434445461819202122232425262728293031323334353637383940414243444546474849505152535455565758596061486263646566676869707172737417 unmodified lines92939495969798991003 unmodified lines10 unmodified lines err.code = code; return err;}
test('when subcommand executable missing (ENOENT) then throw custom message', () => { // If the command is not found, we show a custom error with an explanation and offer // some advice for possible fixes. const mockProcess = new EventEmitter(); const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; }); const program = new commander.Command(); program.exitOverride(); program.command('executable', 'executable description'); program.parse(['executable'], { from: 'user' }); expect(() => { mockProcess.emit('error', makeSystemError('ENOENT')); }).toThrow('use the executableFile option to supply a custom name'); // part of custom message spawnSpy.mockRestore();});// Suppress false positive warnings due to use of testOrSkipOnWindows/* eslint-disable jest/no-standalone-expect */
test('when subcommand executable not executable (EACCES) then throw custom message', () => { // Side note: this error does not actually happen on Windows! But we can still simulate the behaviour on other platforms. const mockProcess = new EventEmitter(); const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; }); const program = new commander.Command(); program.exitOverride(); program.command('executable', 'executable description'); program.parse(['executable'], { from: 'user' }); expect(() => { mockProcess.emit('error', makeSystemError('EACCES')); }).toThrow('not executable'); // part of custom message spawnSpy.mockRestore();});const testOrSkipOnWindows = process.platform === 'win32' ? test.skip : test;
testOrSkipOnWindows( 'when subcommand executable missing (ENOENT) then throw custom message', () => { // If the command is not found, we show a custom error with an explanation and offer // some advice for possible fixes. const mockProcess = new EventEmitter(); const spawnSpy = jest .spyOn(childProcess, 'spawn') .mockImplementation(() => { return mockProcess; }); const program = new commander.Command(); program.exitOverride(); program.command('executable', 'executable description'); program.parse(['executable'], { from: 'user' }); expect(() => { mockProcess.emit('error', makeSystemError('ENOENT')); }).toThrow('use the executableFile option to supply a custom name'); // part of custom message spawnSpy.mockRestore(); },);
testOrSkipOnWindows( 'when subcommand executable not executable (EACCES) then throw custom message', () => { const mockProcess = new EventEmitter(); const spawnSpy = jest .spyOn(childProcess, 'spawn') .mockImplementation(() => { return mockProcess; }); const program = new commander.Command(); program.exitOverride(); program.command('executable', 'executable description'); program.parse(['executable'], { from: 'user' }); expect(() => { mockProcess.emit('error', makeSystemError('EACCES')); }).toThrow('not executable'); // part of custom message spawnSpy.mockRestore(); },);
test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', () => {test('when subcommand executable fails with other error and exitOverride then return in custom wrapper', () => { // The existing behaviour is to just silently fail for unexpected errors, as it is happening // asynchronously in spawned process and client can not catch errors. const mockProcess = new EventEmitter(); const spawnSpy = jest.spyOn(childProcess, 'spawn').mockImplementation(() => { return mockProcess; }); const program = new commander.Command(); program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn program.exitOverride((err) => { throw err; }); program.command('executable', 'executable description');17 unmodified lines return mockProcess; }); const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); const program = new commander.Command(); program._checkForMissingExecutable = () => {}; // suppress error, call mocked spawn program.command('executable', 'executable description'); program.parse(['executable'], { from: 'user' }); mockProcess.emit('error', makeSystemError('OTHER')); expect(exitSpy).toHaveBeenCalledWith(1);3 unmodified linesmodified:tests/command.executableSubcommand.search.test.js+5 added
48 unmodified lines49 afterAll(() => {50 spawnSpy.mockRestore();51 });52 53+ // 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 });58 59 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 line67 });68 69 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 line77 });78 79 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 lines304 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