deps: upgrade to npm 2.14.4
[platform/upstream/nodejs.git] / deps / npm / node_modules / which / test / basic.js
1 var t = require('tap')
2 var fs = require('fs')
3 var rimraf = require('rimraf')
4 var mkdirp = require('mkdirp')
5 var fixture = __dirname + '/fixture'
6 var which = require('../which.js')
7 var path = require('path')
8
9 var isWindows = process.platform === 'win32' ||
10     process.env.OSTYPE === 'cygwin' ||
11     process.env.OSTYPE === 'msys'
12
13 var skip = { skip: isWindows ? 'not relevant on windows' : false }
14
15 t.test('setup', function (t) {
16   rimraf.sync(fixture)
17   mkdirp.sync(fixture)
18   fs.writeFileSync(fixture + '/foo.sh', 'echo foo\n')
19   t.end()
20 })
21
22 t.test('does not find non-executable', skip, function (t) {
23   t.plan(2)
24
25   t.test('absolute', function (t) {
26     t.plan(2)
27     which(fixture + '/foo.sh', function (er) {
28       t.isa(er, Error)
29     })
30
31     t.throws(function () {
32       which.sync(fixture + '/foo.sh')
33     })
34   })
35
36   t.test('with path', function (t) {
37     t.plan(2)
38     which('foo.sh', { path: fixture }, function (er) {
39       t.isa(er, Error)
40     })
41
42     t.throws(function () {
43       which.sync('foo.sh', { path: fixture })
44     })
45   })
46 })
47
48 t.test('make executable', function (t) {
49   fs.chmodSync(fixture + '/foo.sh', '0755')
50   t.end()
51 })
52
53 t.test('find when executable', function (t) {
54   t.plan(4)
55   var opt = { pathExt: '.sh' }
56   var expect = path.resolve(fixture, 'foo.sh').toLowerCase()
57   var PATH = process.env.PATH
58
59   t.test('absolute', function (t) {
60     runTest(fixture + '/foo.sh', t)
61   })
62
63   t.test('with process.env.PATH', function (t) {
64     process.env.PATH = fixture
65     runTest('foo.sh', t)
66   })
67
68   t.test('with process.env.Path', {
69     skip: isWindows ? false : 'Only for Windows'
70   }, function (t) {
71     process.env.PATH = ""
72     process.env.Path = fixture
73     runTest('foo.sh', t)
74   })
75
76   t.test('with path opt', function (t) {
77     opt.path = fixture
78     runTest('foo.sh', t)
79   })
80
81   function runTest(exec, t) {
82     t.plan(2)
83     which(exec, opt, function (er, found) {
84       if (er)
85         throw er
86       t.equal(found.toLowerCase(), expect)
87       process.env.PATH = PATH
88     })
89
90     var found = which.sync(exec, opt).toLowerCase()
91     t.equal(found, expect)
92   }
93
94 })
95
96 t.test('clean', function (t) {
97   rimraf.sync(fixture)
98   t.end()
99 })