command.executableSubcommand.lookup.test.js
This file
Path
tests/command.executableSubcommand.lookup.test.js
96 lines · 3.8 KiB · JavaScript
Last changed by
Merge branch 'master' into release/7.xJohn Geebefore adoption5fd6e88
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697const childProcess = require('child_process');const path = require('path');const util = require('util');const execFileAsync = util.promisify(childProcess.execFile);
// Calling node explicitly so pm works without file suffix cross-platform.
// Get false positives due to use of testOrSkipOnWindows/* eslint-disable jest/no-standalone-expect */
const testOrSkipOnWindows = (process.platform === 'win32') ? test.skip : test;const pm = path.join(__dirname, './fixtures/pm');
test('when subcommand file missing then error', () => { expect.assertions(1); return execFileAsync('node', [pm, 'list']).catch((err) => { if (process.platform === 'win32') { // Get uncaught thrown error on Windows // eslint-disable-next-line jest/no-conditional-expect expect(err.stderr).toBeDefined(); } else { // eslint-disable-next-line jest/no-conditional-expect expect(err.stderr).toMatch(/Error: 'pm-list' does not exist/); } });});
test('when alias subcommand file missing then error', () => { expect.assertions(1); return execFileAsync('node', [pm, 'lst']).catch((err) => { if (process.platform === 'win32') { // Get uncaught thrown error on Windows // eslint-disable-next-line jest/no-conditional-expect expect(err.stderr).toBeDefined(); } else { // eslint-disable-next-line jest/no-conditional-expect expect(err.stderr).toMatch(/Error: 'pm-list' does not exist/); } });});
test('when subcommand file has no suffix then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'install']); expect(stdout).toBe('install\n');});
test('when alias subcommand file has no suffix then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'i']); expect(stdout).toBe('install\n');});
test('when subcommand target executablefile has no suffix then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'specifyInstall']); expect(stdout).toBe('install\n');});
test('when subcommand file suffix .js then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'publish']); expect(stdout).toBe('publish\n');});
test('when alias subcommand file suffix .js then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'p']); expect(stdout).toBe('publish\n');});
test('when subcommand target executablefile has suffix .js then lookup succeeds', async() => { const { stdout } = await execFileAsync('node', [pm, 'specifyPublish']); expect(stdout).toBe('publish\n');});
testOrSkipOnWindows('when subcommand file is symlink then lookup succeeds', async() => { const pmlink = path.join(__dirname, 'fixtures', 'pmlink'); const { stdout } = await execFileAsync('node', [pmlink, 'install']); expect(stdout).toBe('install\n');});
testOrSkipOnWindows('when subcommand file is double symlink then lookup succeeds', async() => { const pmlink = path.join(__dirname, 'fixtures', 'another-dir', 'pm'); const { stdout } = await execFileAsync('node', [pmlink, 'install']); expect(stdout).toBe('install\n');});
test('when subcommand suffix is .ts then lookup succeeds', async() => { // We support looking for ts files for ts-node in particular, but don't need to test ts-node itself. // The program and the subcommand `pm-install.ts` are both plain JavaScript code. const binLinkTs = path.join(__dirname, 'fixtures-ts', 'pm.ts'); // childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], function(_error, stdout, stderr) { const { stdout } = await execFileAsync('node', [binLinkTs, 'install']); expect(stdout).toBe('install\n');});
test('when subsubcommand then lookup sub-sub-command', async() => { const { stdout } = await execFileAsync('node', [pm, 'cache', 'clear']); expect(stdout).toBe('cache-clear\n');});