-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect shebangs absolutely to node, wrap those too
- Loading branch information
Showing
5 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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') | ||
|
||
var expect = | ||
'before in shim\n' + | ||
'shebang main\n' + | ||
'after in shim\n' + | ||
'before in shim\n' + | ||
'shebang main\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 + '\nconsole.log("shebang main")\n' | ||
fs.writeFileSync(file, content, 'utf8') | ||
fs.chmodSync(file, '0755') | ||
var child = spawn(node, [wrap, file]) | ||
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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
console.log('before in shim') | ||
require('../..').runMain() | ||
console.log('after in shim') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
}) | ||
}) |