File: basic.js

package info (click to toggle)
node-which 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 192 kB
  • sloc: sh: 8; makefile: 2
file content (174 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var t = require('tap')
var fs = require('fs')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var fixture = __dirname + '/fixture'
var which = require('../which.js')
var path = require('path')

var isWindows = process.platform === 'win32' ||
    process.env.OSTYPE === 'cygwin' ||
    process.env.OSTYPE === 'msys'

var skip = { skip: isWindows ? 'not relevant on windows' : false }

t.test('setup', function (t) {
  rimraf.sync(fixture)
  mkdirp.sync(fixture)
  fs.writeFileSync(fixture + '/foo.sh', 'echo foo\n')
  t.end()
})

t.test('does not find missed', function(t) {
  t.plan(4)

  which(fixture + '/foobar.sh', function (er) {
    t.isa(er, Error)
    t.equal(er.code, 'ENOENT')
  })

  t.throws(function () {
    which.sync(fixture + '/foobar.sh')
  }, {code: 'ENOENT'})

  t.equal(which.sync(fixture + '/foobar.sh', {nothrow:true}), null)
})

t.test('does not find non-executable', skip, function (t) {
  t.plan(2)

  t.test('absolute', function (t) {
    t.plan(3)
    which(fixture + '/foo.sh', function (er) {
      t.isa(er, Error)
      t.equal(er.code, 'ENOENT')
    })

    t.throws(function () {
      which.sync(fixture + '/foo.sh')
    }, {code: 'ENOENT'})
  })

  t.test('with path', function (t) {
    t.plan(3)
    which('foo.sh', { path: fixture }, function (er) {
      t.isa(er, Error)
      t.equal(er.code, 'ENOENT')
    })

    t.throws(function () {
      which.sync('foo.sh', { path: fixture })
    }, {code: 'ENOENT'})
  })
})

t.test('make executable', function (t) {
  fs.chmodSync(fixture + '/foo.sh', '0755')
  t.end()
})

t.test('find when executable', function (t) {
  var opt = { pathExt: '.sh' }
  var expect = path.resolve(fixture, 'foo.sh').toLowerCase()
  var PATH = process.env.PATH

  t.test('absolute', function (t) {
    runTest(fixture + '/foo.sh', t)
  })

  t.test('with process.env.PATH', function (t) {
    process.env.PATH = fixture
    runTest('foo.sh', t)
  })

  t.test('with pathExt', {
    skip: isWindows ? false : 'Only for Windows'
  }, function (t) {
    var pe = process.env.PATHEXT
    process.env.PATHEXT = '.SH'
    process.env.PATH = fixture

    t.test('foo.sh', function (t) {
      process.env.PATH = fixture
      runTest('foo.sh', t)
    })
    t.test('foo', function (t) {
      process.env.PATH = fixture
      runTest('foo', t)
    })
    t.test('replace', function (t) {
      process.env.PATHEXT = pe
      t.end()
    })
    t.end()
  })

  t.test('with path opt', function (t) {
    opt.path = fixture
    runTest('foo.sh', t)
  })

  t.test('relative path', function (t) {
    var opt = { pathExt: '.sh' }
    var expect = path.join('test/fixture/foo.sh')
    t.plan(3)

    t.test('no ./', function (t) {
      t.plan(2)
      var actual = which.sync('test/fixture/foo.sh', opt)
      t.equal(actual, expect)
      which('test/fixture/foo.sh', opt, function (er, actual) {
        if (er)
          throw er
        t.equal(actual, expect)
      })
    })

    t.test('with ./', function (t) {
      t.plan(2)
      expect = './' + expect
      var actual = which.sync('./test/fixture/foo.sh', opt)
      t.equal(actual, expect)
      which('./test/fixture/foo.sh', opt, function (er, actual) {
        if (er)
          throw er
        t.equal(actual, expect)
      })
    })

    t.test('with ../', function (t) {
      t.plan(2)
      var dir = path.basename(process.cwd())
      expect = path.join('..', dir, 'test/fixture/foo.sh')
      var actual = which.sync(expect, opt)
      t.equal(actual, expect)
      which(expect, opt, function (er, actual) {
        if (er)
          throw er
        t.equal(actual, expect)
      })
    })
  })

  function runTest(exec, t) {
    t.plan(2)

    var found = which.sync(exec, opt).toLowerCase()
    t.equal(found, expect)

    which(exec, opt, function (er, found) {
      if (er)
        throw er
      t.equal(found.toLowerCase(), expect)
      t.end()
      process.env.PATH = PATH
    })
  }

  t.end()
})

t.test('clean', function (t) {
  rimraf.sync(fixture)
  t.end()
})