summaryrefslogtreecommitdiff
path: root/deps/npm/test/need-npm5-update/outdated-local.js
blob: c253331d07e575dd1b6817ee890bfe4fcaed0d39 (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
var common = require('../common-tap.js')
var test = require('tap').test
var npm = require('../../')
var rimraf = require('rimraf')
var path = require('path')
var mr = require('npm-registry-mock')
var osenv = require('osenv')
var mkdirp = require('mkdirp')
var fs = require('graceful-fs')

var pkg = path.resolve(__dirname, 'outdated-local')
var pkgLocal = path.resolve(pkg, 'local-module')
var pkgScopedLocal = path.resolve(pkg, 'another-local-module')
var pkgLocalUnderscore = path.resolve(pkg, 'underscore')
var pkgLocalOptimist = path.resolve(pkg, 'optimist')

var pjParent = JSON.stringify({
  name: 'outdated-local',
  version: '1.0.0',
  dependencies: {
    'local-module': 'file:local-module', // updated locally, not on repo
    '@scoped/another-local-module': 'file:another-local-module', // updated locally, scoped, not on repo
    'underscore': 'file:underscore', // updated locally, updated but lesser version on repo
    'optimist': 'file:optimist' // updated locally, updated and greater version on repo
  }
}, null, 2) + '\n'

var pjLocal = JSON.stringify({
  name: 'local-module',
  version: '1.0.0'
}, null, 2) + '\n'

var pjLocalBumped = JSON.stringify({
  name: 'local-module',
  version: '1.1.0'
}, null, 2) + '\n'

var pjScopedLocal = JSON.stringify({
  name: '@scoped/another-local-module',
  version: '1.0.0'
}, null, 2) + '\n'

var pjScopedLocalBumped = JSON.stringify({
  name: '@scoped/another-local-module',
  version: '1.2.0'
}, null, 2) + '\n'

var pjLocalUnderscore = JSON.stringify({
  name: 'underscore',
  version: '1.3.1'
}, null, 2) + '\n'

var pjLocalUnderscoreBumped = JSON.stringify({
  name: 'underscore',
  version: '1.6.1'
}, null, 2) + '\n'

var pjLocalOptimist = JSON.stringify({
  name: 'optimist',
  version: '0.4.0'
}, null, 2) + '\n'

var pjLocalOptimistBumped = JSON.stringify({
  name: 'optimist',
  version: '0.5.0'
}, null, 2) + '\n'

function mocks (server) {
  server.get('/local-module')
    .reply(404)
  server.get('/@scoped%2fanother-local-module')
    .reply(404)
}

test('setup', function (t) {
  bootstrap()
  t.end()
})

test('outdated support local modules', function (t) {
  t.plan(5)
  process.chdir(pkg)
  mr({ port: common.port, plugin: mocks }, function (err, s) {
    t.ifError(err, 'mock registry started without problems')

    function verify (actual, expected) {
      for (var i = 0; i < expected.length; i++) {
        var current = expected[i]

        var found = false
        for (var j = 0; j < actual.length; j++) {
          var target = actual[j]

          var k
          for (k = 0; k < current.length; k++) {
            if (current[k] !== target[k]) break
          }
          if (k === current.length) found = true
        }

        if (!found) return false
      }

      return true
    }

    npm.load(
      {
        loglevel: 'silent',
        parseable: true,
        registry: common.registry
      },
      function () {
        npm.install('.', function (err) {
          t.ifError(err, 'install success')
          bumpLocalModules()
          npm.outdated(function (er, d) {
            t.ifError(err, 'npm outdated ran without error')
            t.is(process.exitCode, 1, 'errorCode set to 1')
            process.exitCode = 0
            t.ok(verify(d, [
              [
                path.resolve(__dirname, 'outdated-local'),
                'local-module',
                '1.0.0',
                '1.1.0',
                '1.1.0',
                'file:local-module'
              ],
              [
                path.resolve(__dirname, 'outdated-local'),
                '@scoped/another-local-module',
                '1.0.0',
                '1.2.0',
                '1.2.0',
                'file:another-local-module'
              ],
              [
                path.resolve(__dirname, 'outdated-local'),
                'underscore',
                '1.3.1',
                '1.6.1',
                '1.5.1',
                'file:underscore'
              ],
              [
                path.resolve(__dirname, 'outdated-local'),
                'optimist',
                '0.4.0',
                '0.6.0',
                '0.6.0',
                'optimist@0.6.0'
              ]
            ]), 'got expected outdated output')
            s.close()
          })
        })
      }
    )
  })
})

test('cleanup', function (t) {
  cleanup()
  t.end()
})

function bootstrap () {
  mkdirp.sync(pkg)
  fs.writeFileSync(path.resolve(pkg, 'package.json'), pjParent)

  mkdirp.sync(pkgLocal)
  fs.writeFileSync(path.resolve(pkgLocal, 'package.json'), pjLocal)

  mkdirp.sync(pkgScopedLocal)
  fs.writeFileSync(path.resolve(pkgScopedLocal, 'package.json'), pjScopedLocal)

  mkdirp.sync(pkgLocalUnderscore)
  fs.writeFileSync(path.resolve(pkgLocalUnderscore, 'package.json'), pjLocalUnderscore)

  mkdirp.sync(pkgLocalOptimist)
  fs.writeFileSync(path.resolve(pkgLocalOptimist, 'package.json'), pjLocalOptimist)
}

function bumpLocalModules () {
  fs.writeFileSync(path.resolve(pkgLocal, 'package.json'), pjLocalBumped)
  fs.writeFileSync(path.resolve(pkgScopedLocal, 'package.json'), pjScopedLocalBumped)
  fs.writeFileSync(path.resolve(pkgLocalUnderscore, 'package.json'), pjLocalUnderscoreBumped)
  fs.writeFileSync(path.resolve(pkgLocalOptimist, 'package.json'), pjLocalOptimistBumped)
}

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
}