| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- "use strict"
- var test = require('tape')
- var path = require('path')
- var join = path.join
- var npmWhich = require('../')
- var level0 = join(__dirname, 'fixtures', 'level0')
- var level1 = join(level0, 'node_modules', 'level1')
- var level2 = join(level1, 'node_modules', 'level2')
- var LEVEL = [level0, level1, level2]
- var BINPATH_FOR_LEVEL = LEVEL.map(function(levelPath) {
- return join(levelPath, "node_modules", ".bin")
- })
- var before = process.env.PATH
- test('includes all .bin dirs in all parent node_modules folders', function(t) {
- t.test('no nesting', function(t) {
- var level1Bin = npmWhich(LEVEL[0]).sync('level1')
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- t.test('nesting', function(t) {
- var which = npmWhich(LEVEL[1])
- var level1Bin = which.sync('level1')
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- var level2Bin = which.sync('level2')
- t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
- t.end()
- })
- t.test('more nesting', function(t) {
- var which = npmWhich(LEVEL[1])
- var level1Bin = which.sync('level1')
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- var level2Bin = which.sync('level2')
- t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
- t.end()
- })
- t.end()
- })
- test('which.sync requires a cwd', function(t) {
- t.throws(function() {
- npmWhich().sync('level1')
- })
- t.end()
- })
- test('which.sync requires a cwd, can be supplied in options', function(t) {
- var level1Bin = npmWhich().sync('level1', {cwd: LEVEL[0]})
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- test('which requires a cwd', function(t) {
- npmWhich()('level1', function(err) {
- t.ok(err)
- t.end()
- })
- })
- test('which can be curried', function(t) {
- var which = npmWhich(LEVEL[0])('level1')
- which(function(err, level1Bin) {
- t.ifError(err)
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- })
- test('which can be curried, options overridden', function(t) {
- var which = npmWhich(LEVEL[1])('level1')
- which({cwd: LEVEL[0]}, function(err, level1Bin) {
- t.ifError(err)
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- })
- t.end()
- })
- test('which can be curried with sync', function(t) {
- var which = npmWhich(LEVEL[0])('level1')
- var level1Bin = which.sync()
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- test('which can be curried with sync, options overridden', function(t) {
- var which = npmWhich(LEVEL[1])('level1')
- var level1Bin = which.sync({cwd: LEVEL[0]})
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- test('which requires a cwd, can be supplied in options', function(t) {
- npmWhich()('level1', {cwd: LEVEL[0]}, function(err, level1Bin) {
- t.ifError(err)
- t.equal(level1Bin, join(BINPATH_FOR_LEVEL[0], 'level1'), 'got level1 path')
- t.end()
- })
- })
- test('cwd can be overridden in options', function(t) {
- npmWhich(LEVEL[0])('level2', {cwd: LEVEL[1]}, function(err, level2Bin) {
- t.ifError(err)
- t.equal(level2Bin, join(BINPATH_FOR_LEVEL[1], 'level2'), 'got level2 path')
- t.end()
- })
- })
- test('which.sync does not mutate PATH', function(t) {
- npmWhich(__dirname).sync('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- test('which.sync does not mutate PATH after failed find', function(t) {
- t.throws(function() {
- npmWhich(__dirname).sync('asdasd', {env: {PATH: BINPATH_FOR_LEVEL[0]}})
- })
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- test('which does not mutate PATH', function(t) {
- var level1Bin = npmWhich(__dirname)('level1', {env: {PATH: BINPATH_FOR_LEVEL[0]}}, function(err) {
- t.ifError(err)
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- })
- test('which does not mutate PATH after failed find', function(t) {
- npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
- t.ok(err)
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- })
- test('can find path with bad cwd', function(t) {
- npmWhich('/asdasdb/jhbhj')('node', function(err, path) {
- t.ifError(err)
- t.ok(path)
- t.equal(path.split('/').pop(), 'node')
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- })
- test('which does not mutate PATH with bad cmd & cwd', function(t) {
- npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
- t.ok(err)
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- })
- test('which.sync on default export will error without cwd', function(t) {
- t.throws(function() {
- npmWhich.sync('level1')
- })
- t.end()
- })
- if (process.version.indexOf('v0.10') !== -1) {
- // can't test this on 0.11 as process.platform is (rightfully) read-only
- test('which does not mutate PATH with bad cwd/cmd on "windows"', function(t) {
- var actualPlatform = process.platform
- process.platform = "win32"
- npmWhich('asdasdb/jhbhj')('asdasd', function(err) {
- process.platform = actualPlatform
- t.ok(err)
- var after = process.env.PATH
- t.deepEqual(after, before, 'PATH unmodified')
- t.end()
- })
- })
- }
- // Ensure old 1.0.0 tests function, except for the breakages.
- require('./1.0.0-interface')
|