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

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

let result = []

const noop = () => null
const npm = {
  flatOptions: {
    json: false,
  },
  prefix: '',
  version: '1.0.0',
}
const mocks = {
  libnpmversion: noop,
  '../../lib/npm.js': npm,
  '../../lib/utils/output.js': (...msg) => {
    for (const m of msg)
      result.push(m)
  },
  '../../lib/utils/usage.js': () => 'usage instructions',
}

const version = requireInject('../../lib/version.js', mocks)

const _processVersions = process.versions
t.afterEach(cb => {
  npm.flatOptions.json = false
  npm.prefix = ''
  process.versions = _processVersions
  result = []
  cb()
})

t.test('no args', t => {
  const prefix = t.testdir({
    'package.json': JSON.stringify({
      name: 'test-version-no-args',
      version: '3.2.1',
    }),
  })
  npm.prefix = prefix
  Object.defineProperty(process, 'versions', { value: { node: '1.0.0' } })

  version([], err => {
    if (err)
      throw err

    t.deepEqual(
      result,
      [{
        'test-version-no-args': '3.2.1',
        node: '1.0.0',
        npm: '1.0.0',
      }],
      'should output expected values for various versions in npm'
    )

    t.end()
  })
})

t.test('too many args', t => {
  version(['foo', 'bar'], err => {
    t.match(
      err,
      'usage instructions',
      'should throw usage instructions error'
    )

    t.end()
  })
})

t.test('completion', t => {
  const { completion } = version

  const testComp = (argv, expect) => {
    completion({ conf: { argv: { remain: argv } } }, (err, res) => {
      t.ifError(err)
      t.strictSame(res, expect, argv.join(' '))
    })
  }

  testComp(['npm', 'version'], [
    'major',
    'minor',
    'patch',
    'premajor',
    'preminor',
    'prepatch',
    'prerelease',
    'from-git',
  ])
  testComp(['npm', 'version', 'major'], [])

  t.end()
})

t.test('failure reading package.json', t => {
  const prefix = t.testdir({})
  npm.prefix = prefix

  version([], err => {
    if (err)
      throw err

    t.deepEqual(
      result,
      [{
        npm: '1.0.0',
        node: '1.0.0',
      }],
      'should not have package name on returning object'
    )

    t.end()
  })
})

t.test('--json option', t => {
  const prefix = t.testdir({})
  npm.flatOptions.json = true
  npm.prefix = prefix
  Object.defineProperty(process, 'versions', { value: {} })

  version([], err => {
    if (err)
      throw err
    t.deepEqual(
      result,
      ['{\n  "npm": "1.0.0"\n}'],
      'should return json stringified result'
    )
    t.end()
  })
})

t.test('with one arg', t => {
  const version = requireInject('../../lib/version.js', {
    ...mocks,
    libnpmversion: (arg, opts) => {
      t.equal(arg, 'major', 'should forward expected value')
      t.deepEqual(
        opts,
        {
          json: false,
          path: '',
        },
        'should forward expected options'
      )
      t.end()
    },
  })

  version(['major'], err => {
    if (err)
      throw err
  })
})