diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/spawn-wrap/test | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/spawn-wrap/test')
-rw-r--r-- | node_modules/spawn-wrap/test/abs-shebang.js | 68 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/basic.js | 419 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/fixtures/node_modules/npm/bin/npm-cli.js | 4 | ||||
-rwxr-xr-x | node_modules/spawn-wrap/test/fixtures/npm | 5 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/fixtures/npm.cmd | 2 | ||||
-rwxr-xr-x | node_modules/spawn-wrap/test/fixtures/script.js | 6 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/fixtures/test-shim.js | 3 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/fixtures/wrap.js | 27 | ||||
-rw-r--r-- | node_modules/spawn-wrap/test/win-rebase.js | 40 |
9 files changed, 574 insertions, 0 deletions
diff --git a/node_modules/spawn-wrap/test/abs-shebang.js b/node_modules/spawn-wrap/test/abs-shebang.js new file mode 100644 index 000000000..f254683df --- /dev/null +++ b/node_modules/spawn-wrap/test/abs-shebang.js @@ -0,0 +1,68 @@ +var path = require('path') +var fs = require('fs') +var spawn = require('child_process').spawn +var t = require('tap') +var node = process.execPath +var wrap = require.resolve('./fixtures/wrap.js') +var rimraf = require('rimraf') +var mkdirp = require('mkdirp') +var fs = require('fs') + +if (process.platform === 'win32') { + t.plan(0, 'No proper shebang support on windows, so skip this') + process.exit(0) +} + +var expect = + 'before in shim\n' + + 'shebang main foo,bar\n' + + 'after in shim\n' + + 'before in shim\n' + + 'shebang main foo,bar\n' + + 'after in shim\n' + +var fixdir = path.resolve(__dirname, 'fixtures', 'shebangs') + +t.test('setup', function (t) { + rimraf.sync(fixdir) + mkdirp.sync(fixdir) + t.end() +}) + +t.test('absolute', function (t) { + var file = path.resolve(fixdir, 'absolute.js') + runTest(file, process.execPath, t) +}) + +t.test('env', function (t) { + var file = path.resolve(fixdir, 'env.js') + runTest(file, '/usr/bin/env node', t) +}) + +function runTest (file, shebang, t) { + var content = '#!' + shebang + '\n' + + 'console.log("shebang main " + process.argv.slice(2))\n' + fs.writeFileSync(file, content, 'utf8') + fs.chmodSync(file, '0755') + var child = spawn(node, [wrap, file, 'foo', 'bar']) + var out = '' + var err = '' + child.stdout.on('data', function (c) { + out += c + }) + child.stderr.on('data', function (c) { + err += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + // console.error(err) + t.end() + }) +} + +t.test('cleanup', function (t) { + rimraf.sync(fixdir) + t.end() +}) diff --git a/node_modules/spawn-wrap/test/basic.js b/node_modules/spawn-wrap/test/basic.js new file mode 100644 index 000000000..969310088 --- /dev/null +++ b/node_modules/spawn-wrap/test/basic.js @@ -0,0 +1,419 @@ +var sw = require('../') +var isWindows = require('../lib/is-windows.js')() +var winNoShebang = isWindows && 'no shebang execution on windows' +var winNoSig = isWindows && 'no signals get through cmd' + +var onExit = require('signal-exit') +var cp = require('child_process') +var fixture = require.resolve('./fixtures/script.js') +var npmFixture = require.resolve('./fixtures/npm') +var fs = require('fs') +var path = require('path') + +if (process.argv[2] === 'parent') { + // hang up once + process.once('SIGHUP', function onHup () { + console.log('SIGHUP') + }) + // handle sigints forever + process.on('SIGINT', function onInt () { + console.log('SIGINT') + }) + onExit(function (code, signal) { + console.log('EXIT %j', [code, signal]) + }) + var argv = process.argv.slice(3).map(function (arg) { + if (arg === fixture) { + return '{{FIXTURE}}' + } + return arg + }) + console.log('WRAP %j', process.execArgv.concat(argv)) + sw.runMain() + return +} + +var t = require('tap') +var unwrap = sw([__filename, 'parent']) + +var expect = 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'EXIT [0,null]\n' + +// dummy for node v0.10 +if (!cp.spawnSync) { + cp.spawnSync = function () { + return { + status: 0, + signal: null, + stdout: expect + } + } +} + +t.test('spawn execPath', function (t) { + t.plan(4) + + t.test('basic', function (t) { + var child = cp.spawn(process.execPath, [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + t.end() + }) + }) + + t.test('basic sync', function (t) { + var child = cp.spawnSync(process.execPath, [fixture, 'xyz']) + + t.equal(child.status, 0) + t.equal(child.signal, null) + t.equal(child.stdout.toString(), expect) + t.end() + }) + + t.test('SIGINT', { skip: winNoSig }, function (t) { + var child = cp.spawn(process.execPath, [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.stdout.once('data', function () { + child.kill('SIGINT') + }) + child.stderr.on('data', function (t) { + console.error(t) + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGINT\n' + + 'EXIT [0,null]\n') + t.end() + }) + }) + + t.test('SIGHUP', { skip: winNoSig }, function (t) { + var child = cp.spawn(process.execPath, [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + child.kill('SIGHUP') + }) + child.on('close', function (code, signal) { + t.equal(signal, 'SIGHUP') + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGHUP\n' + + 'EXIT [null,"SIGHUP"]\n') + t.end() + }) + }) +}) + +t.test('spawn node', function (t) { + t.plan(4) + + t.test('basic', function (t) { + var child = cp.spawn('node', [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + t.end() + }) + }) + + t.test('basic sync', function (t) { + var child = cp.spawnSync('node', [fixture, 'xyz']) + + t.equal(child.status, 0) + t.equal(child.signal, null) + t.equal(child.stdout.toString(), expect) + t.end() + }) + + t.test('SIGINT', { skip: winNoSig }, function (t) { + var child = cp.spawn('node', [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.stdout.once('data', function () { + child.kill('SIGINT') + }) + child.stderr.on('data', function (t) { + console.error(t) + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGINT\n' + + 'EXIT [0,null]\n') + t.end() + }) + }) + + t.test('SIGHUP', { skip: winNoSig }, function (t) { + var child = cp.spawn('node', [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + child.kill('SIGHUP') + }) + child.on('close', function (code, signal) { + t.equal(signal, 'SIGHUP') + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGHUP\n' + + 'EXIT [null,"SIGHUP"]\n') + t.end() + }) + }) +}) + +t.test('exec execPath', function (t) { + t.plan(4) + + t.test('basic', function (t) { + var opt = isWindows ? null : { shell: '/bin/bash' } + var child = cp.exec(process.execPath + ' ' + fixture + ' xyz', opt) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + t.end() + }) + }) + + t.test('execPath wrapped with quotes', function (t) { + var opt = isWindows ? null : { shell: '/bin/bash' } + var child = cp.exec(JSON.stringify(process.execPath) + ' ' + fixture + + ' xyz', opt) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + t.end() + }) + }) + + t.test('SIGINT', { skip: winNoSig }, function (t) { + var child = cp.exec(process.execPath + ' ' + fixture + ' xyz', { shell: '/bin/bash' }) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.stdout.once('data', function () { + child.kill('SIGINT') + }) + child.stderr.on('data', function (t) { + console.error(t) + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGINT\n' + + 'EXIT [0,null]\n') + t.end() + }) + }) + + t.test('SIGHUP', { skip: winNoSig }, function (t) { + var child = cp.exec(process.execPath + ' ' + fixture + ' xyz', { shell: '/bin/bash' }) + + var out = '' + child.stdout.on('data', function (c) { + out += c + child.kill('SIGHUP') + }) + child.on('close', function (code, signal) { + t.equal(signal, 'SIGHUP') + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGHUP\n' + + 'EXIT [null,"SIGHUP"]\n') + t.end() + }) + }) +}) + +t.test('exec shebang', { skip: winNoShebang }, function (t) { + t.plan(3) + + t.test('basic', function (t) { + var child = cp.exec(fixture + ' xyz', { shell: '/bin/bash' }) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + t.end() + }) + }) + + t.test('SIGHUP', function (t) { + var child = cp.exec(fixture + ' xyz', { shell: '/bin/bash' }) + + var out = '' + child.stdout.on('data', function (c) { + out += c + child.kill('SIGHUP') + }) + child.on('close', function (code, signal) { + t.equal(signal, 'SIGHUP') + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGHUP\n' + + 'EXIT [null,"SIGHUP"]\n') + t.end() + }) + }) + + t.test('SIGINT', function (t) { + var child = cp.exec(fixture + ' xyz', { shell: '/bin/bash' }) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.stdout.once('data', function () { + child.kill('SIGINT') + }) + child.stderr.on('data', function (t) { + console.error(t) + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, 'WRAP ["{{FIXTURE}}","xyz"]\n' + + '[]\n' + + '["xyz"]\n' + + 'SIGINT\n' + + 'EXIT [0,null]\n') + t.end() + }) + }) +}) + +// see: https://github.com/bcoe/nyc/issues/190 +t.test('Node 5.8.x + npm 3.7.x - spawn', { skip: winNoShebang }, function (t) { + var npmdir = path.dirname(npmFixture) + process.env.PATH = npmdir + ':' + (process.env.PATH || '') + var child = cp.spawn('npm', ['xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.true(~out.indexOf('xyz')) + t.end() + }) +}) + +t.test('Node 5.8.x + npm 3.7.x - shell', { skip: winNoShebang }, function (t) { + var npmdir = path.dirname(npmFixture) + process.env.PATH = npmdir + ':' + (process.env.PATH || '') + var child = cp.exec('npm xyz') + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.true(~out.indexOf('xyz')) + t.end() + }) +}) + +t.test('--harmony', function (t) { + var node = process.execPath + var child = cp.spawn(node, ['--harmony', fixture, 'xyz']) + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, 'WRAP ["--harmony","{{FIXTURE}}","xyz"]\n' + + '["--harmony"]\n' + + '["xyz"]\n' + + 'EXIT [0,null]\n') + t.end() + }) +}) + +t.test('node exe with different name', function(t) { + var fp = path.join(__dirname, 'fixtures', 'exething.exe') + var data = fs.readFileSync(process.execPath) + fs.writeFileSync(fp, data) + fs.chmodSync(fp, '0775') + var child = cp.spawn(process.execPath, [fixture, 'xyz']) + + var out = '' + child.stdout.on('data', function (c) { + out += c + }) + child.on('close', function (code, signal) { + t.equal(code, 0) + t.equal(signal, null) + t.equal(out, expect) + fs.unlinkSync(fp) + t.end() + }) +}) + +t.test('unwrap', function (t) { + unwrap() + t.end() +}) diff --git a/node_modules/spawn-wrap/test/fixtures/node_modules/npm/bin/npm-cli.js b/node_modules/spawn-wrap/test/fixtures/node_modules/npm/bin/npm-cli.js new file mode 100644 index 000000000..b3c400768 --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/node_modules/npm/bin/npm-cli.js @@ -0,0 +1,4 @@ +'use strict'; +console.log('%j', process.execArgv) +console.log('%j', process.argv.slice(2)) +setTimeout(function () {}, 100) diff --git a/node_modules/spawn-wrap/test/fixtures/npm b/node_modules/spawn-wrap/test/fixtures/npm new file mode 100755 index 000000000..4e026deb9 --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/npm @@ -0,0 +1,5 @@ +#!/bin/sh +// 2>/dev/null; exec "`dirname "$0"`/node" "$0" "$@" +console.log('%j', process.execArgv) +console.log('%j', process.argv.slice(2)) +setTimeout(function () {}, 100) diff --git a/node_modules/spawn-wrap/test/fixtures/npm.cmd b/node_modules/spawn-wrap/test/fixtures/npm.cmd new file mode 100644 index 000000000..521ec9e4d --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/npm.cmd @@ -0,0 +1,2 @@ +@echo This code should never be executed. +exit /B 1 diff --git a/node_modules/spawn-wrap/test/fixtures/script.js b/node_modules/spawn-wrap/test/fixtures/script.js new file mode 100755 index 000000000..3ccd0c9ea --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/script.js @@ -0,0 +1,6 @@ +#!/usr/bin/env node +console.log('%j', process.execArgv) +console.log('%j', process.argv.slice(2)) + +// Keep the event loop alive long enough to receive signals. +setTimeout(function() {}, 100) diff --git a/node_modules/spawn-wrap/test/fixtures/test-shim.js b/node_modules/spawn-wrap/test/fixtures/test-shim.js new file mode 100644 index 000000000..b6baf69ee --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/test-shim.js @@ -0,0 +1,3 @@ +console.log('before in shim') +require('../..').runMain() +console.log('after in shim') diff --git a/node_modules/spawn-wrap/test/fixtures/wrap.js b/node_modules/spawn-wrap/test/fixtures/wrap.js new file mode 100644 index 000000000..a8af6e471 --- /dev/null +++ b/node_modules/spawn-wrap/test/fixtures/wrap.js @@ -0,0 +1,27 @@ +#!/usr/bin/env node +var sw = require('../..') + +sw([require.resolve('./test-shim.js')]) + +var path = require('path') +var spawn = require('child_process').spawn + +spawn(path.resolve(process.argv[2]), process.argv.slice(3), { + stdio: 'inherit' +}).on('close', function (code, signal) { + if (code || signal) { + throw new Error('failed with ' + (code || signal)) + } + + // now run using PATH + process.env.PATH = path.resolve(path.dirname(process.argv[2])) + + ':' + process.env.PATH + + spawn(path.basename(process.argv[2]), process.argv.slice(3), { + stdio: 'inherit', + }, function (code, signal) { + if (code || signal) { + throw new Error('failed with ' + (code || signal)) + } + }) +}) diff --git a/node_modules/spawn-wrap/test/win-rebase.js b/node_modules/spawn-wrap/test/win-rebase.js new file mode 100644 index 000000000..b9d09fe0b --- /dev/null +++ b/node_modules/spawn-wrap/test/win-rebase.js @@ -0,0 +1,40 @@ +var t = require('tap') +var winRebase = require('../lib/win-rebase') + +t.test('it replaces path to node bin', function (t) { + var result = winRebase('C:\\Program Files\\nodejs\\node.exe', 'C:\\foo') + t.equal(result, 'C:\\foo') + t.done() +}) + +t.test('it does not replace path if it references an unknown bin', function (t) { + var result = winRebase('C:\\Program Files\\nodejs\\banana', 'C:\\foo') + t.equal(result, 'C:\\Program Files\\nodejs\\banana') + t.done() +}) + +t.test('replaces node bin and leaves the script being executed', function (t) { + var result = winRebase('C:\\Program Files\\nodejs\\node.exe foo.js', 'C:\\foo') + t.equal(result, 'C:\\foo foo.js') + t.done() +}) + +t.test('handles a quote', function (t) { + var result = winRebase('"C:\\Program Files\\nodejs\\node.exe" "foo.js"', 'C:\\foo') + t.equal(result, '"C:\\foo" "foo.js"') + t.end() +}) + +t.test('handles many quotes', function (t) { + var result = winRebase('""C:\\Program Files\\nodejs\\node.exe" "foo.js""', 'C:\\foo') + t.equal(result, '""C:\\foo" "foo.js""') + t.end() +}) + +t.test('handles npm invocations', function (t) { + var result = winRebase('""npm" "install""', + 'C:\\foo', + function() { return 'C:\\path-to-npm\\npm' }) + t.equal(result, '""C:\\foo "C:\\path-to-npm\\node_modules\\npm\\bin\\npm-cli.js"" "install""') + t.end() +}) |