1 var fs = require('graceful-fs')
2 var path = require('path')
4 var mkdirp = require('mkdirp')
5 var osenv = require('osenv')
6 var rimraf = require('rimraf')
7 var test = require('tap').test
9 var common = require('../common-tap.js')
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')
18 name: 'optional-metadep-rollback-collision',
20 description: 'let\'s just see about that race condition',
21 optionalDependencies: {
22 opdep: 'file:./deps/opdep'
29 description: 'I FAIL CONSTANTLY',
34 foo: 'http://localhost:8080/'
41 description: 'how do you *really* know you exist?',
43 postinstall: 'node blart.js'
46 'graceful-fs': '^3.0.2',
55 description: 'To explode, of course!',
58 preinstall: 'node bad-server.js'
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')
74 if (process.argv[2]) {
76 createServer(function (req, res) {
77 setTimeout(function () {
86 [__filename, 'whatever'],
94 // kill any prior children, if existing.
96 var pid = +fs.readFileSync(pidfile)
97 process.kill(pid, 'SIGKILL')
100 fs.writeFileSync(pidfile, child.pid + '\n')
102 */}.toString().split('\n').slice(1, -1).join('\n')
104 var blart = function () {/*
105 var rando = require('crypto').randomBytes
106 var resolve = require('path').resolve
108 var mkdirp = require('mkdirp')
109 var rimraf = require('rimraf')
110 var writeFile = require('graceful-fs').writeFile
112 var BASEDIR = resolve(__dirname, 'arena')
114 var keepItGoingLouder = {}
118 function gensym () { return rando(16).toString('hex') }
120 function writeAlmostForever (filename) {
121 if (!keepItGoingLouder[filename]) {
123 if (writers < 1) return done()
125 writeFile(filename, keepItGoingLouder[filename], function (err) {
128 writeAlmostForever(filename)
134 rimraf(BASEDIR, function () {
135 if (errors > 0) console.log('not ok - %d errors', errors)
136 else console.log('ok')
140 mkdirp(BASEDIR, function go () {
141 for (var i = 0; i < 16; i++) {
142 var filename = resolve(BASEDIR, gensym() + '.txt')
144 keepItGoingLouder[filename] = ''
145 for (var j = 0; j < 512; j++) keepItGoingLouder[filename] += filename
148 writeAlmostForever(filename)
151 setTimeout(function viktor () {
152 // kill all the writers
153 keepItGoingLouder = {}
156 */}.toString().split('\n').slice(1, -1).join('\n')
157 test('setup', function (t) {
162 path.join(pkg, 'package.json'),
163 JSON.stringify(json, null, 2)
166 mkdirp.sync(path.join(deps, 'd1'))
168 path.join(deps, 'd1', 'package.json'),
169 JSON.stringify(d1, null, 2)
172 mkdirp.sync(path.join(deps, 'd2'))
174 path.join(deps, 'd2', 'package.json'),
175 JSON.stringify(d2, null, 2)
177 fs.writeFileSync(path.join(deps, 'd2', 'blart.js'), blart)
179 mkdirp.sync(path.join(deps, 'opdep'))
181 path.join(deps, 'opdep', 'package.json'),
182 JSON.stringify(opdep, null, 2)
184 fs.writeFileSync(path.join(deps, 'opdep', 'bad-server.js'), badServer)
189 test('go go test racer', function (t) {
193 '--fetch-retries', '0',
194 '--loglevel', 'silent',
201 PATH: process.env.PATH,
202 Path: process.env.Path
204 stdio: [0, 'pipe', 2]
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')
210 t.equal(stdout, 'ok\nok\n')
211 t.notOk(/not ok/.test(stdout), 'should not contain the string \'not ok\'')
217 test('verify results', function (t) {
218 t.throws(function () {
224 test('cleanup', function (t) {
229 function cleanup () {
230 process.chdir(osenv.tmpdir())
232 var pid = +fs.readFileSync(pidfile)
233 process.kill(pid, 'SIGKILL')