Welcome to mirror list, hosted at ThFree Co, Russian Federation.

npm-usage.js « utils « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fbc453811ec2f821f3061ae56b7fb5ab4e29f38f (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
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
const t = require('tap')

const OUTPUT = []
const output = (...msg) => OUTPUT.push(msg)
const requireInject = require('require-inject')
const usage = require('../../../lib/utils/npm-usage.js')

const npm = requireInject('../../../lib/npm.js')
npm.output = output

t.test('usage', t => {
  t.afterEach((cb) => {
    npm.config.set('viewer', null)
    npm.config.set('long', false)
    npm.config.set('userconfig', '/some/config/file/.npmrc')
    cb()
  })
  const { dirname } = require('path')
  const basedir = dirname(dirname(dirname(__dirname)))
  t.cleanSnapshot = str => str.split(basedir).join('{BASEDIR}')
    .split(require('../../../package.json').version).join('{VERSION}')

  npm.load(err => {
    if (err)
      throw err

    npm.config.set('viewer', null)
    npm.config.set('long', false)
    npm.config.set('userconfig', '/some/config/file/.npmrc')

    t.test('basic usage', t => {
      usage(npm)
      t.equal(OUTPUT.length, 1)
      t.equal(OUTPUT[0].length, 1)
      t.matchSnapshot(OUTPUT[0][0])
      OUTPUT.length = 0
      t.end()
    })

    t.test('with browser', t => {
      npm.config.set('viewer', 'browser')
      usage(npm)
      t.equal(OUTPUT.length, 1)
      t.equal(OUTPUT[0].length, 1)
      t.matchSnapshot(OUTPUT[0][0])
      OUTPUT.length = 0
      npm.config.set('viewer', null)
      t.end()
    })

    t.test('with long', t => {
      npm.config.set('long', true)
      usage(npm)
      t.equal(OUTPUT.length, 1)
      t.equal(OUTPUT[0].length, 1)
      t.matchSnapshot(OUTPUT[0][0])
      OUTPUT.length = 0
      npm.config.set('long', false)
      t.end()
    })

    t.test('did you mean?', t => {
      npm.argv.push('unistnall')
      usage(npm)
      t.equal(OUTPUT.length, 2)
      t.equal(OUTPUT[0].length, 1)
      t.equal(OUTPUT[1].length, 1)
      t.matchSnapshot(OUTPUT[0][0])
      t.matchSnapshot(OUTPUT[1][0])
      OUTPUT.length = 0
      npm.argv.length = 0
      t.end()
    })

    t.test('did you mean?', t => {
      npm.argv.push('unistnall')
      const { exitCode } = process
      t.teardown(() => {
        if (t.passing())
          process.exitCode = exitCode
      })
      // make sure it fails when invalid
      usage(npm, false)
      t.equal(process.exitCode, 1)
      OUTPUT.length = 0
      npm.argv.length = 0
      t.end()
    })

    t.test('set process.stdout.columns', t => {
      const { columns } = process.stdout
      t.teardown(() => {
        Object.defineProperty(process.stdout, 'columns', {
          value: columns,
          enumerable: true,
          configurable: true,
          writable: true,
        })
      })
      const cases = [0, 90]
      for (const cols of cases) {
        t.test(`columns=${cols}`, t => {
          Object.defineProperty(process.stdout, 'columns', {
            value: cols,
            enumerable: true,
            configurable: true,
            writable: true,
          })
          usage(npm)
          t.equal(OUTPUT.length, 1)
          t.equal(OUTPUT[0].length, 1)
          t.matchSnapshot(OUTPUT[0][0])
          OUTPUT.length = 0
          t.end()
        })
      }
      t.end()
    })
    t.end()
  })
})