1'use strict' 2var path = require('path') 3var fs = require('fs') 4var test = require('tap').test 5var common = require('../common-tap.js') 6var rimraf = require('rimraf') 7var mkdirp = require('mkdirp') 8var mr = require('npm-registry-mock') 9var basepath = path.resolve(__dirname, path.basename(__filename, '.js')) 10var fixturepath = path.resolve(basepath, 'npm-test-optional-deps') 11var modulepath = path.resolve(basepath, 'node_modules') 12var Tacks = require('tacks') 13var File = Tacks.File 14var Dir = Tacks.Dir 15 16var fixture = new Tacks( 17 Dir({ 18 README: File( 19 'just an npm test\n' 20 ), 21 'package.json': File({ 22 name: 'npm-test-optional-deps', 23 version: '1.2.5', 24 optionalDependencies: { 25 'npm-test-foobarzaaakakaka': common.registry + '/not-a-package', 26 async: '10.999.14234', 27 mkdirp: '0.3.5', 28 optimist: 'some invalid version 99 #! $$ x y z', 29 'npm-test-failer': '*' 30 } 31 }) 32 }) 33) 34 35var server 36 37test('setup', function (t) { 38 setup() 39 mr({port: common.port}, function (err, s) { 40 if (err) throw err 41 server = s 42 t.done() 43 }) 44}) 45 46test('optional-deps', function (t) { 47 server.get('/npm-test-failer').reply(404, {error: 'nope'}) 48 server.get('/not-a-package').reply(200, 'HI THERE') 49 50 var opts = ['--registry=' + common.registry, '--timeout=100'] 51 common.npm(opts.concat(['install', fixturepath]), {cwd: basepath}, installCheckAndTest) 52 53 function installCheckAndTest (err, code, stdout, stderr) { 54 if (err) throw err 55 if (stderr) console.error(stderr) 56 server.done() 57 t.is(code, 0, 'install went ok') 58 var dir = fs.readdirSync(modulepath).sort() 59 t.isDeeply(dir, ['mkdirp', 'npm-test-optional-deps'], 'only one optional dep should be there') 60 t.is(require(path.resolve(modulepath, 'mkdirp', 'package.json')).version, '0.3.5', 'mkdirp version right') 61 t.done() 62 } 63}) 64 65test('cleanup', function (t) { 66 cleanup() 67 server.close() 68 t.done() 69}) 70 71function setup () { 72 cleanup() 73 fixture.create(fixturepath) 74 mkdirp.sync(modulepath) 75} 76 77function cleanup () { 78 fixture.remove(fixturepath) 79 rimraf.sync(basepath) 80} 81