]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/update-examples.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / update-examples.js
1 var common = require('../common-tap.js')
2 var test = require('tap').test
3 var npm = require('../../lib/npm.js')
4 var mkdirp = require('mkdirp')
5 var rimraf = require('rimraf')
6 var path = require('path')
7 var mr = require('npm-registry-mock')
8
9 var osenv = require('osenv')
10
11 var requireInject = require('require-inject')
12
13 var PKG_DIR = path.resolve(__dirname, 'update-examples')
14 var CACHE_DIR = path.resolve(PKG_DIR, 'cache')
15
16 // ** constant templates for mocks **
17 var DEFAULT_PKG = {
18   'name': 'update-examples',
19   'version': '1.2.3',
20   'dependencies': {
21     'dep1': '*'
22   }
23 }
24
25 var DEP_PKG = {
26   name: 'dep1',
27   version: '1.1.1',
28   _from: '^1.1.1'
29 }
30
31 var INSTALLED = {
32   dependencies: {
33     'dep1': {
34       version: '1.1.1',
35       link: false
36     }
37   }
38 }
39
40 var DEP1_REGISTRY = { name: 'dep1',
41   'dist-tags': { latest: '1.2.2' },
42   versions: {
43     '1.2.2': { version: '1.2.2' },
44     '1.2.1': { version: '1.2.1' },
45     '1.2.0': { version: '1.2.0' },
46     '1.1.2': { version: '1.1.2' },
47     '1.1.1': { version: '1.1.1' },
48     '1.0.0': { version: '1.0.0' },
49     '0.4.1': { version: '0.4.1' },
50     '0.4.0': { version: '0.4.0' },
51     '0.2.0': { version: '0.2.0' }
52   }
53 }
54
55 var registryMocks = {
56   'get': {
57     '/dep1': [200, DEP1_REGISTRY]
58   }
59 }
60
61 // ** dynamic mocks, cloned from templates and modified **
62 var mockServer
63 var mockDepJson = clone(DEP_PKG)
64 var mockInstalled = clone(INSTALLED)
65 var mockParentJson = clone(DEFAULT_PKG)
66
67 // target
68 var installAskedFor
69
70 function clone (a) {
71   return extend({}, a)
72 }
73
74 function extend (a, b) {
75   for (var key in b) {
76     a[key] = b[key]
77   }
78   return a
79 }
80
81 function resetPackage (options) {
82   rimraf.sync(CACHE_DIR)
83   mkdirp.sync(CACHE_DIR)
84
85   installAskedFor = undefined
86
87   mockParentJson = clone(DEFAULT_PKG)
88   mockInstalled = clone(INSTALLED)
89   mockDepJson = clone(DEP_PKG)
90
91   if (options.wanted) {
92     mockParentJson.dependencies.dep1 = options.wanted
93     mockDepJson._from = options.wanted
94   }
95
96   if (options.installed) {
97     mockInstalled.dependencies.dep1.version = options.installed
98     mockDepJson.version = options.installed
99   }
100 }
101
102 function mockReadInstalled (dir, opts, cb) {
103   cb(null, mockInstalled)
104 }
105
106 function mockReadJson (file, cb) {
107   cb(null, file.match(/dep1/) ? mockDepJson : mockParentJson)
108 }
109
110 function mockCommand (npm, name, fn) {
111   delete npm.commands[name]
112   npm.commands[name] = fn
113 }
114
115 test('setup', function (t) {
116   process.chdir(osenv.tmpdir())
117   mkdirp.sync(PKG_DIR)
118   process.chdir(PKG_DIR)
119
120   resetPackage({})
121
122   mr({ port: common.port, mocks: registryMocks }, function (er, server) {
123     npm.load({ cache: CACHE_DIR,
124       registry: common.registry,
125     cwd: PKG_DIR }, function (err) {
126         t.ifError(err, 'started server')
127         mockServer = server
128
129         mockCommand(npm, 'install', function mockInstall (where, what, cb) {
130           installAskedFor = what
131           cb(null)
132         })
133
134         mockCommand(npm, 'outdated', requireInject('../../lib/outdated', {
135           'read-installed': mockReadInstalled,
136           'read-package-json': mockReadJson
137         }))
138
139         t.end()
140       })
141   })
142 })
143
144 test('update caret dependency to latest', function (t) {
145   resetPackage({ wanted: '^1.1.1' })
146
147   npm.commands.update([], function (err) {
148     t.ifError(err)
149     t.equal('dep1@1.2.2', installAskedFor, 'should want to install dep@1.2.2')
150     t.end()
151   })
152 })
153
154 test('update tilde dependency to latest', function (t) {
155   resetPackage({ wanted: '~1.1.1' })
156
157   npm.commands.update([], function (err) {
158     t.ifError(err)
159     t.equal('dep1@1.1.2', installAskedFor, 'should want to install dep@1.1.2')
160     t.end()
161   })
162 })
163
164 test('hold tilde dependency at wanted (#6441)', function (t) {
165   resetPackage({ wanted: '~1.1.2', installed: '1.1.2' })
166
167   npm.commands.update([], function (err) {
168     t.ifError(err)
169     t.notOk(installAskedFor, 'should not want to install anything')
170     t.end()
171   })
172 })
173
174 test('update old caret dependency with no newer', function (t) {
175   resetPackage({ wanted: '^0.2.0', installed: '^0.2.0' })
176
177   npm.commands.update([], function (err) {
178     t.ifError(err)
179     t.equal('dep1@0.2.0', installAskedFor, 'should want to install dep@0.2.0')
180     t.end()
181   })
182 })
183
184 test('update old caret dependency with newer', function (t) {
185   resetPackage({ wanted: '^0.4.0', installed: '^0.4.0' })
186
187   npm.commands.update([], function (err) {
188     t.ifError(err)
189     t.equal('dep1@0.4.1', installAskedFor, 'should want to install dep@0.4.1')
190     t.end()
191   })
192 })
193
194 test('cleanup', function (t) {
195   mockServer.close()
196
197   process.chdir(osenv.tmpdir())
198   rimraf.sync(PKG_DIR)
199
200   t.end()
201 })