summaryrefslogtreecommitdiff
path: root/deps/npm/test/lib/bin.js
blob: 05fc1e21e05d4cb5b40b4881246960e5a71b8610 (plain)
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
const { test } = require('tap')
const requireInject = require('require-inject')

test('bin', (t) => {
  t.plan(3)
  const dir = '/bin/dir'

  const bin = requireInject('../../lib/bin.js', {
    '../../lib/npm.js': { bin: dir, flatOptions: { global: false } },
    '../../lib/utils/output.js': (output) => {
      t.equal(output, dir, 'prints the correct directory')
    }
  })

  bin([], (err) => {
    t.ifError(err, 'npm bin')
    t.ok('should have printed directory')
  })
})

test('bin -g', (t) => {
  t.plan(3)
  const consoleError = console.error
  t.tearDown(() => {
    console.error = consoleError
  })

  console.error = (output) => {
    t.fail('should not have printed to console.error')
  }
  const dir = '/bin/dir'

  const bin = requireInject('../../lib/bin.js', {
    '../../lib/npm.js': { bin: dir, flatOptions: { global: true } },
    '../../lib/utils/path.js': [dir],
    '../../lib/utils/output.js': (output) => {
      t.equal(output, dir, 'prints the correct directory')
    }
  })

  bin([], (err) => {
    t.ifError(err, 'npm bin')
    t.ok('should have printed directory')
  })
})

test('bin -g (not in path)', (t) => {
  t.plan(4)
  const consoleError = console.error
  t.tearDown(() => {
    console.error = consoleError
  })

  console.error = (output) => {
    t.equal(output, '(not in PATH env variable)', 'prints env warning')
  }
  const dir = '/bin/dir'

  const bin = requireInject('../../lib/bin.js', {
    '../../lib/npm.js': { bin: dir, flatOptions: { global: true } },
    '../../lib/utils/path.js': ['/not/my/dir'],
    '../../lib/utils/output.js': (output) => {
      t.equal(output, dir, 'prints the correct directory')
    }
  })

  bin([], (err) => {
    t.ifError(err, 'npm bin')
    t.ok('should have printed directory')
  })
})