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

cli.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 59769e13fd0e7e26f4d1576c476d15d68f70ca20 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const t = require('tap')

let LOAD_ERROR = null
const npmOutputs = []
const npmock = {
  log: { level: 'silent' },
  output: (...msg) => npmOutputs.push(msg),
  usage: 'npm usage test example',
  version: '99.99.99',
  load: cb => cb(LOAD_ERROR),
  argv: [],
  config: {
    settings: {},
    get: (k) => npmock.config.settings[k],
    set: (k, v) => {
      npmock.config.settings[k] = v
    },
  },
  commands: {},
}

const unsupportedMock = {
  checkForBrokenNode: () => {},
  checkForUnsupportedNode: () => {},
}

let errorHandlerCalled = null
let errorHandlerCb
const errorHandlerMock = (...args) => {
  errorHandlerCalled = args
  if (errorHandlerCb)
    errorHandlerCb()
}
let errorHandlerExitCalled = null
errorHandlerMock.exit = code => {
  errorHandlerExitCalled = code
}

const logs = []
const npmlogMock = {
  pause: () => logs.push('pause'),
  verbose: (...msg) => logs.push(['verbose', ...msg]),
  info: (...msg) => logs.push(['info', ...msg]),
}

const requireInject = require('require-inject')
const cli = requireInject.installGlobally('../../lib/cli.js', {
  '../../lib/npm.js': npmock,
  '../../lib/utils/did-you-mean.js': () => 'test did you mean',
  '../../lib/utils/unsupported.js': unsupportedMock,
  '../../lib/utils/error-handler.js': errorHandlerMock,
  npmlog: npmlogMock,
})

t.test('print the version, and treat npm_g to npm -g', t => {
  t.teardown(() => {
    delete npmock.config.settings.version
    process.argv = argv
    npmock.argv.length = 0
    proc.argv.length = 0
    logs.length = 0
    npmOutputs.length = 0
    errorHandlerExitCalled = null
  })

  const { argv } = process
  const proc = {
    argv: ['node', 'npm_g', '-v'],
    version: '420.69.lol',
    on: () => {},
  }
  process.argv = proc.argv
  npmock.config.settings.version = true

  cli(proc)

  t.strictSame(npmock.argv, [])
  t.strictSame(proc.argv, ['node', 'npm', '-g', '-v'])
  t.strictSame(logs, [
    'pause',
    ['verbose', 'cli', ['node', 'npm', '-g', '-v']],
    ['info', 'using', 'npm@%s', '99.99.99'],
    ['info', 'using', 'node@%s', '420.69.lol'],
  ])
  t.strictSame(npmOutputs, [['99.99.99']])
  t.strictSame(errorHandlerExitCalled, 0)

  t.end()
})

t.test('calling with --versions calls npm version with no args', t => {
  const processArgv = process.argv
  const proc = {
    argv: ['node', 'npm', 'install', 'or', 'whatever', '--versions'],
    on: () => {},
  }
  process.argv = proc.argv
  npmock.config.set('versions', true)

  t.teardown(() => {
    delete npmock.config.settings.versions
    process.argv = processArgv
    npmock.argv.length = 0
    proc.argv.length = 0
    logs.length = 0
    npmOutputs.length = 0
    errorHandlerExitCalled = null
    delete npmock.commands.version
  })

  npmock.commands.version = (args, cb) => {
    t.equal(proc.title, 'npm')
    t.strictSame(npmock.argv, [])
    t.strictSame(proc.argv, ['node', 'npm', 'install', 'or', 'whatever', '--versions'])
    t.strictSame(logs, [
      'pause',
      ['verbose', 'cli', ['node', 'npm', 'install', 'or', 'whatever', '--versions']],
      ['info', 'using', 'npm@%s', '99.99.99'],
      ['info', 'using', 'node@%s', undefined],
    ])

    t.strictSame(npmOutputs, [])
    t.strictSame(errorHandlerExitCalled, null)

    t.strictSame(args, [])
    t.end()
  }

  cli(proc)
})

t.test('print usage if no params provided', t => {
  const { output } = npmock
  t.teardown(() => {
    npmock.output = output
  })
  const proc = {
    argv: ['node', 'npm'],
    on: () => {},
  }
  npmock.argv = []
  npmock.output = (msg) => {
    if (msg) {
      t.match(msg, 'npm usage test example', 'outputs npm usage')
      t.end()
    }
  }
  cli(proc)
})

t.test('print usage if non-command param provided', t => {
  const { output } = npmock
  t.teardown(() => {
    npmock.output = output
  })
  const proc = {
    argv: ['node', 'npm', 'asdf'],
    on: () => {},
  }
  npmock.argv = ['asdf']
  npmock.output = (msg) => {
    if (msg) {
      t.match(msg, 'test did you mean', 'outputs did you mean')
      t.end()
    }
  }
  cli(proc)
})

t.test('gracefully handles error printing usage', t => {
  const { output } = npmock
  t.teardown(() => {
    npmock.output = output
    errorHandlerCb = null
  })
  const proc = {
    argv: ['node', 'npm', 'asdf'],
    on: () => {},
  }
  npmock.argv = []
  npmock.output = (msg) => {
    throw new Error('test exception')
  }
  errorHandlerCb = () => {
    t.match(errorHandlerCalled, /test exception/)
    t.end()
  }
  cli(proc)
})

t.test('load error calls error handler', t => {
  t.teardown(() => {
    errorHandlerCb = null
    LOAD_ERROR = null
  })

  const er = new Error('test load error')
  LOAD_ERROR = er
  const proc = {
    argv: ['node', 'npm', 'asdf'],
    on: () => {},
  }
  errorHandlerCb = () => {
    t.strictSame(errorHandlerCalled, [er])
    t.end()
  }
  cli(proc)
})