]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/optional-metadep-rollback-collision.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / optional-metadep-rollback-collision.js
1 var fs = require('graceful-fs')
2 var path = require('path')
3
4 var mkdirp = require('mkdirp')
5 var osenv = require('osenv')
6 var rimraf = require('rimraf')
7 var test = require('tap').test
8
9 var common = require('../common-tap.js')
10
11 var pkg = path.resolve(__dirname, 'optional-metadep-rollback-collision')
12 var deps = path.resolve(pkg, 'deps')
13 var nm = path.resolve(pkg, 'node_modules')
14 var cache = path.resolve(pkg, 'cache')
15 var pidfile = path.resolve(pkg, 'child.pid')
16
17 var json = {
18   name: 'optional-metadep-rollback-collision',
19   version: '1.0.0',
20   description: 'let\'s just see about that race condition',
21   optionalDependencies: {
22     opdep: 'file:./deps/opdep'
23   }
24 }
25
26 var d1 = {
27   name: 'd1',
28   version: '1.0.0',
29   description: 'I FAIL CONSTANTLY',
30   scripts: {
31     preinstall: 'sleep 1'
32   },
33   dependencies: {
34     foo: 'http://localhost:8080/'
35   }
36 }
37
38 var d2 = {
39   name: 'd2',
40   version: '1.0.0',
41   description: 'how do you *really* know you exist?',
42   scripts: {
43     postinstall: 'node blart.js'
44   },
45   dependencies: {
46     'graceful-fs': '^3.0.2',
47     mkdirp: '^0.5.0',
48     rimraf: '^2.2.8'
49   }
50 }
51
52 var opdep = {
53   name: 'opdep',
54   version: '1.0.0',
55   description: 'To explode, of course!',
56   main: 'index.js',
57   scripts: {
58     preinstall: 'node bad-server.js'
59   },
60   dependencies: {
61     d1: 'file:../d1',
62     d2: 'file:../d2'
63   }
64 }
65
66
67 var badServer = function () {/*
68 var createServer = require('http').createServer
69 var spawn = require('child_process').spawn
70 var fs = require('fs')
71 var path = require('path')
72 var pidfile = path.resolve(__dirname, '..', '..', 'child.pid')
73
74 if (process.argv[2]) {
75   console.log('ok')
76   createServer(function (req, res) {
77     setTimeout(function () {
78       res.writeHead(404)
79       res.end()
80     }, 1000)
81     this.close()
82   }).listen(8080)
83 } else {
84   var child = spawn(
85     process.execPath,
86     [__filename, 'whatever'],
87     {
88       stdio: [0, 1, 2],
89       detached: true
90     }
91   )
92   child.unref()
93
94   // kill any prior children, if existing.
95   try {
96     var pid = +fs.readFileSync(pidfile)
97     process.kill(pid, 'SIGKILL')
98   } catch (er) {}
99
100   fs.writeFileSync(pidfile, child.pid + '\n')
101 }
102 */}.toString().split('\n').slice(1, -1).join('\n')
103
104 var blart = function () {/*
105 var rando = require('crypto').randomBytes
106 var resolve = require('path').resolve
107
108 var mkdirp = require('mkdirp')
109 var rimraf = require('rimraf')
110 var writeFile = require('graceful-fs').writeFile
111
112 var BASEDIR = resolve(__dirname, 'arena')
113
114 var keepItGoingLouder = {}
115 var writers = 0
116 var errors = 0
117
118 function gensym () { return rando(16).toString('hex') }
119
120 function writeAlmostForever (filename) {
121   if (!keepItGoingLouder[filename]) {
122     writers--
123     if (writers < 1) return done()
124   } else {
125     writeFile(filename, keepItGoingLouder[filename], function (err) {
126       if (err) errors++
127
128       writeAlmostForever(filename)
129     })
130   }
131 }
132
133 function done () {
134   rimraf(BASEDIR, function () {
135     if (errors > 0) console.log('not ok - %d errors', errors)
136     else console.log('ok')
137   })
138 }
139
140 mkdirp(BASEDIR, function go () {
141   for (var i = 0; i < 16; i++) {
142     var filename = resolve(BASEDIR, gensym() + '.txt')
143
144     keepItGoingLouder[filename] = ''
145     for (var j = 0; j < 512; j++) keepItGoingLouder[filename] += filename
146
147     writers++
148     writeAlmostForever(filename)
149   }
150
151   setTimeout(function viktor () {
152     // kill all the writers
153     keepItGoingLouder = {}
154   }, 3 * 1000)
155 })
156 */}.toString().split('\n').slice(1, -1).join('\n')
157 test('setup', function (t) {
158   cleanup()
159
160   mkdirp.sync(pkg)
161   fs.writeFileSync(
162     path.join(pkg, 'package.json'),
163     JSON.stringify(json, null, 2)
164   )
165
166   mkdirp.sync(path.join(deps, 'd1'))
167   fs.writeFileSync(
168     path.join(deps, 'd1', 'package.json'),
169     JSON.stringify(d1, null, 2)
170   )
171
172   mkdirp.sync(path.join(deps, 'd2'))
173   fs.writeFileSync(
174     path.join(deps, 'd2', 'package.json'),
175     JSON.stringify(d2, null, 2)
176   )
177   fs.writeFileSync(path.join(deps, 'd2', 'blart.js'), blart)
178
179   mkdirp.sync(path.join(deps, 'opdep'))
180   fs.writeFileSync(
181     path.join(deps, 'opdep', 'package.json'),
182     JSON.stringify(opdep, null, 2)
183   )
184   fs.writeFileSync(path.join(deps, 'opdep', 'bad-server.js'), badServer)
185
186   t.end()
187 })
188
189 test('go go test racer', function (t) {
190   common.npm(
191     [
192       '--prefix', pkg,
193       '--fetch-retries', '0',
194       '--loglevel', 'silent',
195       '--cache', cache,
196       'install'
197     ],
198     {
199       cwd: pkg,
200       env: {
201         PATH: process.env.PATH,
202         Path: process.env.Path
203       },
204       stdio: [0, 'pipe', 2]
205     },
206     function (er, code, stdout, stderr) {
207       t.ifError(er, 'install ran to completion without error')
208       t.notOk(code, 'npm install exited with code 0')
209
210       t.equal(stdout, 'ok\nok\n')
211       t.notOk(/not ok/.test(stdout), 'should not contain the string \'not ok\'')
212       t.end()
213     }
214   )
215 })
216
217 test('verify results', function (t) {
218   t.throws(function () {
219     fs.statSync(nm)
220   })
221   t.end()
222 })
223
224 test('cleanup', function (t) {
225   cleanup()
226   t.end()
227 })
228
229 function cleanup () {
230   process.chdir(osenv.tmpdir())
231   try {
232     var pid = +fs.readFileSync(pidfile)
233     process.kill(pid, 'SIGKILL')
234   } catch (er) {}
235
236   rimraf.sync(pkg)
237 }