summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/view.js
blob: 71d21487ae99c61f7d7ffe6210ce26986d2be241 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var common = require('../common-tap.js')
const t = require('tap')
var test = t.test
var osenv = require('osenv')
var path = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')

// this test has to use a tmpdir so that it's outside of
// the current package context of npm.
var tmp = osenv.tmpdir()
var t1dir = path.resolve(tmp, 'view-local-no-pkg')
var t2dir = path.resolve(tmp, 'view-local-notmine')
var t3dir = path.resolve(tmp, 'view-local-mine')
var mr = require('npm-registry-mock')

var server

t.teardown(() => {
  rimraf.sync(t1dir)
  rimraf.sync(t2dir)
  rimraf.sync(t3dir)
  if (server) {
    server.close()
  }
})

test('setup', function (t) {
  mkdirp.sync(t1dir)
  mkdirp.sync(t2dir)
  mkdirp.sync(t3dir)

  fs.writeFileSync(t2dir + '/package.json', JSON.stringify({
    author: 'Evan Lucas',
    name: 'test-repo-url-https',
    version: '0.0.1'
  }), 'utf8')

  fs.writeFileSync(t3dir + '/package.json', JSON.stringify({
    author: 'Evan Lucas',
    name: 'biscuits',
    version: '0.0.1'
  }), 'utf8')

  t.pass('created fixtures')

  mr({ port: common.port, plugin: plugin }, function (er, s) {
    server = s
    t.end()
  })
})

function plugin (server) {
  server
    .get('/biscuits')
    .many()
    .reply(404, {'error': 'version not found'})
}

test('npm view . in global mode', function (t) {
  common.npm([
    'view',
    '.',
    '--registry=' + common.registry,
    '--global'
  ], { cwd: t1dir }, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')
    t.similar(stderr, /Cannot use view command in global mode./m)
    t.end()
  })
})

test('npm view --global', function (t) {
  common.npm([
    'view',
    '--registry=' + common.registry,
    '--global'
  ], { cwd: t1dir }, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')
    t.similar(stderr, /Cannot use view command in global mode./m)
    t.end()
  })
})

test('npm view . with no package.json', function (t) {
  common.npm([
    'view',
    '.',
    '--registry=' + common.registry
  ], { cwd: t1dir }, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')
    t.similar(stderr, /Invalid package.json/m)
    t.end()
  })
})

test('npm view . with no published package', function (t) {
  common.npm([
    'view',
    '.',
    '--registry=' + common.registry
  ], { cwd: t3dir }, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')
    t.similar(stderr, /not in the npm registry/m)
    t.end()
  })
})

test('npm view .', function (t) {
  common.npm([
    'view',
    '.',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.matches(stdout, /test-repo-url-https/, 'has the right package')
    t.end()
  })
})

test('npm view . select fields', function (t) {
  common.npm([
    'view',
    '.',
    'main',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), 'index.js', 'should print `index.js`')
    t.end()
  })
})

test('npm view .@<version>', function (t) {
  common.npm([
    'view',
    '.@0.0.0',
    'version',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), '0.0.0', 'should print `0.0.0`')
    t.end()
  })
})

test('npm view .@<version> version --json', function (t) {
  common.npm([
    'view',
    '.@0.0.0',
    'version',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), '"0.0.0"', 'should print `"0.0.0"`')
    t.end()
  })
})

test('npm view . --json author name version', function (t) {
  common.npm([
    'view',
    '.',
    'author',
    'name',
    'version',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    var expected = JSON.stringify({
      author: 'Evan Lucas <evanlucas@me.com>',
      name: 'test-repo-url-https',
      version: '0.0.1'
    }, null, 2)
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), expected, 'should print ' + expected)
    t.end()
  })
})

test('npm view .@<version> --json author name version', function (t) {
  common.npm([
    'view',
    '.@0.0.0',
    'author',
    'name',
    'version',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    var expected = JSON.stringify({
      author: 'Evan Lucas <evanlucas@me.com>',
      name: 'test-repo-url-https',
      version: '0.0.0'
    }, null, 2)
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), expected, 'should print ' + expected)
    t.end()
  })
})

test('npm view <package name>', function (t) {
  common.npm([
    'view',
    'underscore',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.matches(stdout, /underscore/, 'should have name `underscore`')
    t.end()
  })
})

test('npm view <package name> --global', function (t) {
  common.npm([
    'view',
    'underscore',
    '--global',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.matches(stdout, /underscore/, 'should have name `underscore`')
    t.end()
  })
})

test('npm view <package name>@<semver range> versions', function (t) {
  common.npm([
    'view',
    'underscore@^1.5.0',
    'versions',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    var re = new RegExp('1.5.0')
    t.similar(stdout, re, 'should have version `1.5.0`')
    t.end()
  })
})

test('npm view <package name>@<semver range> version --json', function (t) {
  common.npm([
    'view',
    'underscore@~1.5.0',
    'version',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), JSON.stringify([
      '1.5.0',
      '1.5.1'
    ], null, 2), 'should have three versions')
    t.end()
  })
})

test('npm view <package name> --json', function (t) {
  t.plan(3)
  common.npm([
    'view',
    'underscore',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    try {
      var out = JSON.parse(stdout.trim())
      t.similar(out, {
        maintainers: ['jashkenas <jashkenas@gmail.com>']
      }, 'should have the same maintainer')
    } catch (er) {
      t.fail('Unable to parse JSON')
    }
  })
})

test('npm view <package name>@<invalid version>', function (t) {
  common.npm([
    'view',
    'underscore@12345',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), '', 'should return empty')
    t.end()
  })
})

test('npm view <package name>@<invalid version> --json', function (t) {
  common.npm([
    'view',
    'underscore@12345',
    '--json',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), '', 'should return empty')
    t.end()
  })
})

test('npm view <package name> <field>', function (t) {
  common.npm([
    'view',
    'underscore',
    'homepage',
    '--registry=' + common.registry
  ], { cwd: t2dir }, function (err, code, stdout) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 0, 'exit ok')
    t.equal(stdout.trim(), 'http://underscorejs.org',
      'homepage should equal `http://underscorejs.org`')
    t.end()
  })
})

test('npm view with invalid package name', function (t) {
  var invalidName = 'InvalidPackage'

  server.get('/' + invalidName).reply('404', {'error': 'not found'})
  common.npm([
    'view',
    invalidName,
    '--registry=' + common.registry
  ], {}, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')

    t.similar(stderr, new RegExp('is not in the npm registry'),
      'Package should NOT be found')

    t.dissimilar(stderr, new RegExp('use the name yourself!'),
      'Suggestion should not be there')

    t.similar(stderr, new RegExp('name can no longer contain capital letters'),
      'Suggestion about Capital letter should be there')

    t.end()
  })
})

test('npm view with valid but non existent package name', function (t) {
  server.get('/valid-but-non-existent-package').reply(404, {'error': 'not found'})
  common.npm([
    'view',
    'valid-but-non-existent-package',
    '--registry=' + common.registry
  ], {}, function (err, code, stdout, stderr) {
    t.ifError(err, 'view command finished successfully')
    t.equal(code, 1, 'exit not ok')

    t.similar(stderr,
      new RegExp("'valid-but-non-existent-package' is not in the npm registry\\."),
      'Package should NOT be found')

    t.similar(stderr, new RegExp('use the name yourself!'),
      'Suggestion should be there')

    t.end()
  })
})