Windows tests (and GitHub Actions) (#1070)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Use separatr run commands to work-around Windows issue * Restrict tests while fixing Windows failures * Skip signal tests on Windows * Get platform without needing extra require * Make tests runnable on WIndows * Use node explicitly for compatibility with WIndows * Name conditional more explicitly * Suppress test that fails differently on WIndows * Missing executable subcommand fails without tidy message on Windows * Remove test for non-executable launch failure, different behaviours on node 8 vs 10, and Windows * Minor code tidy * Skip symlink tests on Windows * Fix lint call, and restore full matrix
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−44 removed
- tests:+50 added−39 removed
- other:+7 added−5 removed
Provenance
- Commit
7ab5fbf- Tree
739d9ff- Author
John Gee <john@ruru.gen.nz>- Committer
Tomoaki Abe <abe@enzou.tokyo>- Parent
a92da62
5 files changed+57 added−44 removed
modified:.github/workflows/tests.yml+7 added-5 removed
15 unmodified lines1617181920212223242021222324252615 unmodified lines - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: npm install, build, and test run: | npm install npm run build --if-present npm test # Use separate run commands so command status handled correctly on Windows - name: npm install run: npm install - name: npm test run: npm test - name: npm run lint run: npm run lintmodified:tests/command.executableSubcommand.default.test.js+6 added-4 removed
123445678791011121314151416171819202122212324252627const childProcess = require('child_process');const path = require('path');
const bin = path.join(__dirname, './fixtures/pm');// Calling node explicitly so pm works without file suffix cross-platform.
const pm = path.join(__dirname, './fixtures/pm');
test('when default subcommand and no command then call default', (done) => { childProcess.execFile(bin, [], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm}`, function(_error, stdout, stderr) { expect(stdout).toBe('default\n'); done(); });});
test('when default subcommand and unrecognised argument then call default with argument', (done) => { childProcess.execFile(bin, ['an-argument'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} an-argument`, function(_error, stdout, stderr) { expect(stdout).toBe("default\n[ 'an-argument' ]\n"); done(); });});
test('when default subcommand and unrecognised option then call default with option', (done) => { childProcess.execFile(bin, ['--an-option'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} --an-option`, function(_error, stdout, stderr) { expect(stdout).toBe("default\n[ '--an-option' ]\n"); done(); });});modified:tests/command.executableSubcommand.lookup.test.js+33 added-32 removed
1234456789781011121314151617181920211415222324252627282930313233213435363738394028414243444546473548495051525354425556575859606149626364656667685669707172737462636465666768697071727374757675767778798081828283848384858687888990919192929394959697989999100101102103104const childProcess = require('child_process');const path = require('path');
const bin = path.join(__dirname, './fixtures/pm');// Calling node explicitly so pm works without file suffix cross-platform.
const testOrSkipOnWindows = (process.platform === 'win32') ? test.skip : test;const pm = path.join(__dirname, './fixtures/pm');
test('when subcommand file missing then error', (done) => { childProcess.execFile(bin, ['list'], function(_error, stdout, stderr) { expect(stderr).toBe('error: pm-list(1) does not exist, try --help\n'); childProcess.exec(`node ${pm} list`, function(_error, stdout, stderr) { if (process.platform === 'win32') { // Get uncaught thrown error on Windows expect(stderr.length).toBeGreaterThan(0); } else { expect(stderr).toBe('error: pm-list(1) does not exist, try --help\n'); } done(); });});
test('when alias subcommand file missing then error', (done) => { childProcess.execFile(bin, ['lst'], function(_error, stdout, stderr) { expect(stderr).toBe('error: pm-list(1) does not exist, try --help\n'); childProcess.exec(`node ${pm} lst`, function(_error, stdout, stderr) { if (process.platform === 'win32') { // Get uncaught thrown error on Windows expect(stderr.length).toBeGreaterThan(0); } else { expect(stderr).toBe('error: pm-list(1) does not exist, try --help\n'); } done(); });});
test('when subcommand file has no suffix then lookup succeeds', (done) => { childProcess.execFile(bin, ['install'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} install`, function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when alias subcommand file has no suffix then lookup succeeds', (done) => { childProcess.execFile(bin, ['i'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} i`, function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when subcommand target executablefile has no suffix then lookup succeeds', (done) => { childProcess.execFile(bin, ['specifyInstall'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} specifyInstall`, function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when subcommand file suffix .js then lookup succeeds', (done) => { childProcess.execFile(bin, ['publish'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} publish`, function(_error, stdout, stderr) { expect(stdout).toBe('publish\n'); done(); });});
test('when alias subcommand file suffix .js then lookup succeeds', (done) => { childProcess.execFile(bin, ['p'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} p`, function(_error, stdout, stderr) { expect(stdout).toBe('publish\n'); done(); });});
test('when subcommand target executablefile has suffix .js then lookup succeeds', (done) => { childProcess.execFile(bin, ['specifyPublish'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} specifyPublish`, function(_error, stdout, stderr) { expect(stdout).toBe('publish\n'); done(); });});
// This is not a behaviour we enforce but rather one that is expected.test('when subcommand file not executable then error', (done) => { childProcess.execFile(bin, ['search'], { }, function(error, stdout, stderr) { // In node 8 the EACCES gets thrown instead of emitted through the spawned process. // In node 10 and 12 we get the commander error. // commander: error: %s(1) not executable. try chmod or run with root // node: Error: Command failed: ... Error: spawn EACCES expect(error.toString()).toMatch(new RegExp('not executable|EACCES')); done(); });});
test('when subcommand file is symlink then lookup succeeds', (done) => { const binLink = path.join(__dirname, './fixtures/pmlink'); childProcess.execFile(binLink, ['install'], { }, function(_error, stdout, stderr) {testOrSkipOnWindows('when subcommand file is symlink then lookup succeeds', (done) => { const pmlink = path.join(__dirname, 'fixtures', 'pmlink'); childProcess.exec(`node ${pmlink} install`, function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when subcommand file is double symlink then lookup succeeds', (done) => { const binLink = path.join(__dirname, './fixtures/another-dir/pm'); childProcess.execFile(binLink, ['install'], { }, function(_error, stdout, stderr) {testOrSkipOnWindows('when subcommand file is double symlink then lookup succeeds', (done) => { const pmlink = path.join(__dirname, 'fixtures', 'another-dir', 'pm'); childProcess.exec(`node ${pmlink} install`, function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when subcommand suffix is .ts then lookup succeeds', (done) => { const binLinkTs = path.join(__dirname, './fixtures-ts/pm.ts'); childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], { }, function(_error, stdout, stderr) { const binLinkTs = path.join(__dirname, 'fixtures-ts', 'pm.ts'); childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], function(_error, stdout, stderr) { expect(stdout).toBe('install\n'); done(); });});
test('when subsubcommand then lookup sub-sub-command', (done) => { childProcess.execFile(bin, ['cache', 'clear'], { }, function(_error, stdout, stderr) { childProcess.exec(`node ${pm} cache clear`, function(_error, stdout, stderr) { expect(stdout).toBe('cache-clear\n'); done(); });});modified:tests/command.executableSubcommand.signals.test.js+7 added-2 removed
12344567891011121314151612171819202116 unmodified linesconst childProcess = require('child_process');const path = require('path');
// Test that a signal sent to the parent process is received by the executable subcommand process (which is listening)// Test that a signal sent to the parent process is received by the executable subcommand process (which is listening).
// Disabling tests on Windows as:// "Windows does not support sending signals"// https://nodejs.org/api/process.html#process_signal_eventsconst describeOrSkipOnWindows = (process.platform === 'win32') ? describe.skip : describe;
// Note: the previous (sinon) test had custom code for SIGUSR1, revist if required:// As described at https://nodejs.org/api/process.html#process_signal_events// this signal will start a debugger and thus the process might output an// additional error message:// "Failed to open socket on port 5858, waiting 1000 ms before retrying".
describe.each([['SIGINT'], ['SIGHUP'], ['SIGTERM'], ['SIGUSR1'], ['SIGUSR2']])(describeOrSkipOnWindows.each([['SIGINT'], ['SIGHUP'], ['SIGTERM'], ['SIGUSR1'], ['SIGUSR2']])( 'test signal handling in executableSubcommand', (value) => { test(`when command killed with ${value} then executableSubcommand receieves ${value}`, (done) => { const pmPath = path.join(__dirname, './fixtures/pm');
16 unmodified linesmodified:tests/command.exitOverride.test.js+4 added-1 removed
11 unmodified lines12 expect(err.code).toBe(code);13 expect(err.message).toBe(message);14 }15 16+const testOrSkipOnWindows = (process.platform === 'win32') ? test.skip : test;17+18 describe('.exitOverride and error details', () => {19 // Use internal knowledge to suppress output to keep test output clean.20 let consoleErrorSpy;21 let writeSpy;167 unmodified lines189 190 program.parse(['node', pm, 'silent']);191 });192 193+ // Throws directly on Windows191- test('when executableSubcommand fails then call exitOverride', (done) => {194+ testOrSkipOnWindows('when executableSubcommand fails then call exitOverride', (done) => {195 // Tricky for override, get called for `error` event then `exit` event.196 const exitCallback = jest.fn()197 .mockImplementationOnce((err) => {198 expectCommanderError(err, 1, 'commander.executeSubCommandAsync', '(error)');15 unmodified lines