• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var common = require('../common-tap.js')
2var test = require('tap').test
3var npm = require('../../')
4var rimraf = require('rimraf')
5var path = require('path')
6var mr = require('npm-registry-mock')
7var osenv = require('osenv')
8var mkdirp = require('mkdirp')
9var fs = require('graceful-fs')
10
11var pkg = path.resolve(__dirname, 'outdated-local')
12var pkgLocal = path.resolve(pkg, 'local-module')
13var pkgScopedLocal = path.resolve(pkg, 'another-local-module')
14var pkgLocalUnderscore = path.resolve(pkg, 'underscore')
15var pkgLocalOptimist = path.resolve(pkg, 'optimist')
16
17var pjParent = JSON.stringify({
18  name: 'outdated-local',
19  version: '1.0.0',
20  dependencies: {
21    'local-module': 'file:local-module', // updated locally, not on repo
22    '@scoped/another-local-module': 'file:another-local-module', // updated locally, scoped, not on repo
23    'underscore': 'file:underscore', // updated locally, updated but lesser version on repo
24    'optimist': 'file:optimist' // updated locally, updated and greater version on repo
25  }
26}, null, 2) + '\n'
27
28var pjLocal = JSON.stringify({
29  name: 'local-module',
30  version: '1.0.0'
31}, null, 2) + '\n'
32
33var pjLocalBumped = JSON.stringify({
34  name: 'local-module',
35  version: '1.1.0'
36}, null, 2) + '\n'
37
38var pjScopedLocal = JSON.stringify({
39  name: '@scoped/another-local-module',
40  version: '1.0.0'
41}, null, 2) + '\n'
42
43var pjScopedLocalBumped = JSON.stringify({
44  name: '@scoped/another-local-module',
45  version: '1.2.0'
46}, null, 2) + '\n'
47
48var pjLocalUnderscore = JSON.stringify({
49  name: 'underscore',
50  version: '1.3.1'
51}, null, 2) + '\n'
52
53var pjLocalUnderscoreBumped = JSON.stringify({
54  name: 'underscore',
55  version: '1.6.1'
56}, null, 2) + '\n'
57
58var pjLocalOptimist = JSON.stringify({
59  name: 'optimist',
60  version: '0.4.0'
61}, null, 2) + '\n'
62
63var pjLocalOptimistBumped = JSON.stringify({
64  name: 'optimist',
65  version: '0.5.0'
66}, null, 2) + '\n'
67
68function mocks (server) {
69  server.get('/local-module')
70    .reply(404)
71  server.get('/@scoped%2fanother-local-module')
72    .reply(404)
73}
74
75test('setup', function (t) {
76  bootstrap()
77  t.end()
78})
79
80test('outdated support local modules', function (t) {
81  t.plan(5)
82  process.chdir(pkg)
83  mr({ port: common.port, plugin: mocks }, function (err, s) {
84    t.ifError(err, 'mock registry started without problems')
85
86    function verify (actual, expected) {
87      for (var i = 0; i < expected.length; i++) {
88        var current = expected[i]
89
90        var found = false
91        for (var j = 0; j < actual.length; j++) {
92          var target = actual[j]
93
94          var k
95          for (k = 0; k < current.length; k++) {
96            if (current[k] !== target[k]) break
97          }
98          if (k === current.length) found = true
99        }
100
101        if (!found) return false
102      }
103
104      return true
105    }
106
107    npm.load(
108      {
109        loglevel: 'silent',
110        parseable: true,
111        registry: common.registry
112      },
113      function () {
114        npm.install('.', function (err) {
115          t.ifError(err, 'install success')
116          bumpLocalModules()
117          npm.outdated(function (er, d) {
118            t.ifError(err, 'npm outdated ran without error')
119            t.is(process.exitCode, 1, 'errorCode set to 1')
120            process.exitCode = 0
121            t.ok(verify(d, [
122              [
123                path.resolve(__dirname, 'outdated-local'),
124                'local-module',
125                '1.0.0',
126                '1.1.0',
127                '1.1.0',
128                'file:local-module'
129              ],
130              [
131                path.resolve(__dirname, 'outdated-local'),
132                '@scoped/another-local-module',
133                '1.0.0',
134                '1.2.0',
135                '1.2.0',
136                'file:another-local-module'
137              ],
138              [
139                path.resolve(__dirname, 'outdated-local'),
140                'underscore',
141                '1.3.1',
142                '1.6.1',
143                '1.5.1',
144                'file:underscore'
145              ],
146              [
147                path.resolve(__dirname, 'outdated-local'),
148                'optimist',
149                '0.4.0',
150                '0.6.0',
151                '0.6.0',
152                'optimist@0.6.0'
153              ]
154            ]), 'got expected outdated output')
155            s.close()
156          })
157        })
158      }
159    )
160  })
161})
162
163test('cleanup', function (t) {
164  cleanup()
165  t.end()
166})
167
168function bootstrap () {
169  mkdirp.sync(pkg)
170  fs.writeFileSync(path.resolve(pkg, 'package.json'), pjParent)
171
172  mkdirp.sync(pkgLocal)
173  fs.writeFileSync(path.resolve(pkgLocal, 'package.json'), pjLocal)
174
175  mkdirp.sync(pkgScopedLocal)
176  fs.writeFileSync(path.resolve(pkgScopedLocal, 'package.json'), pjScopedLocal)
177
178  mkdirp.sync(pkgLocalUnderscore)
179  fs.writeFileSync(path.resolve(pkgLocalUnderscore, 'package.json'), pjLocalUnderscore)
180
181  mkdirp.sync(pkgLocalOptimist)
182  fs.writeFileSync(path.resolve(pkgLocalOptimist, 'package.json'), pjLocalOptimist)
183}
184
185function bumpLocalModules () {
186  fs.writeFileSync(path.resolve(pkgLocal, 'package.json'), pjLocalBumped)
187  fs.writeFileSync(path.resolve(pkgScopedLocal, 'package.json'), pjScopedLocalBumped)
188  fs.writeFileSync(path.resolve(pkgLocalUnderscore, 'package.json'), pjLocalUnderscoreBumped)
189  fs.writeFileSync(path.resolve(pkgLocalOptimist, 'package.json'), pjLocalOptimistBumped)
190}
191
192function cleanup () {
193  process.chdir(osenv.tmpdir())
194  rimraf.sync(pkg)
195}
196