• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const path = require('path')
3const test = require('tap').test
4const mr = require('npm-registry-mock')
5const Tacks = require('tacks')
6const File = Tacks.File
7const Dir = Tacks.Dir
8const common = require('../common-tap.js')
9
10const basedir = common.pkg
11const testdir = path.join(basedir, 'testdir')
12const cachedir = common.cache
13const globaldir = path.join(basedir, 'global')
14const tmpdir = path.join(basedir, 'tmp')
15
16const conf = {
17  cwd: testdir,
18  env: Object.assign({}, process.env, {
19    npm_config_cache: cachedir,
20    npm_config_tmp: tmpdir,
21    npm_config_prefix: globaldir,
22    npm_config_registry: common.registry,
23    npm_config_loglevel: 'warn'
24  })
25}
26
27function exampleManifest (version) {
28  return {
29    name: 'example',
30    version: version
31  }
32}
33
34const examplePackument = {
35  'name': 'example',
36  'dist-tags': {
37    'latest': '1.2.4',
38    'beta': '1.2.6'
39  },
40  'versions': {
41    '1.2.0': exampleManifest('1.2.0'),
42    '1.2.1': exampleManifest('1.2.1'),
43    '1.2.2': exampleManifest('1.2.2'),
44    '1.2.3': exampleManifest('1.2.3'),
45    '1.2.4': exampleManifest('1.2.4'),
46    '1.2.5': exampleManifest('1.2.5'),
47    '1.2.6': exampleManifest('1.2.6')
48  }
49}
50
51const fixture = new Tacks(Dir({
52  cache: Dir(),
53  global: Dir(),
54  tmp: Dir(),
55  testdir: Dir({
56    node_modules: Dir({
57      example: Dir({
58        'package.json': File({
59          name: 'example',
60          version: '1.2.3'
61        })
62      })
63    }),
64    'package.json': File({
65      name: 'outdated-latest',
66      version: '1.0.0',
67      dependencies: {
68        example: '^1.2.0'
69      }
70    })
71  })
72}))
73
74function setup () {
75  cleanup()
76  fixture.create(basedir)
77}
78
79function cleanup () {
80  fixture.remove(basedir)
81}
82
83let server
84
85test('setup', function (t) {
86  setup()
87  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
88    if (err) throw err
89    server = s
90    server.get('/example').reply(200, examplePackument)
91    t.done()
92  })
93})
94
95test('example', function (t) {
96  return common.npm(['outdated', '--json'], conf).spread((code, stdout, stderr) => {
97    t.is(code, 1, 'files ARE outdated!')
98    const result = JSON.parse(stdout.trim())
99    t.comment(stderr.trim())
100    // your assertions here
101    t.like(result, {example: {current: '1.2.3', wanted: '1.2.4', latest: '1.2.4'}}, 'got latest, not beta')
102  })
103})
104
105test('cleanup', function (t) {
106  server.close()
107  cleanup()
108  t.done()
109})
110