1'use strict' 2var test = require('tap').test 3var common = require('../common-tap.js') 4var path = require('path') 5var rimraf = require('rimraf') 6var mkdirp = require('mkdirp') 7var fs = require('graceful-fs') 8var tar = require('tar') 9var basepath = common.pkg 10var fixturepath = path.resolve(basepath, 'npm-test-bundled-deps') 11var targetpath = path.resolve(basepath, 'target') 12var Tacks = require('tacks') 13var File = Tacks.File 14var Dir = Tacks.Dir 15 16test('basic bundling', function (t) { 17 var fixture = new Tacks( 18 Dir({ 19 'package.json': File({ 20 name: 'npm-test-files', 21 version: '1.2.5', 22 bundledDependencies: [ 23 'addme' 24 ] 25 }), 26 node_modules: Dir({ 27 addme: Dir({ 28 'index.js': File('') 29 }), 30 iggyme: Dir({ 31 'index.js': File('') 32 }) 33 }) 34 }) 35 ) 36 withFixture(t, fixture, function (done) { 37 t.ok(fileExists('node_modules/addme'), 'bundled dep included') 38 t.notOk(fileExists('node_modules/iggyme'), 'unspecified dep not included') 39 done() 40 }) 41}) 42 43test('scoped dep bundling', function (t) { 44 var fixture = new Tacks( 45 Dir({ 46 'package.json': File({ 47 name: 'npm-test-files', 48 version: '1.2.5', 49 bundledDependencies: [ 50 '@foo/addme' 51 ] 52 }), 53 node_modules: Dir({ 54 '@foo': Dir({ 55 addme: Dir({ 56 'index.js': File('') 57 }), 58 iggyme: Dir({ 59 'index.js': File('') 60 }) 61 }) 62 }) 63 }) 64 ) 65 withFixture(t, fixture, function (done) { 66 t.ok(fileExists('node_modules/@foo/addme'), 'bundled dep included') 67 t.notOk( 68 fileExists('node_modules/@foo/iggyme'), 69 'unspecified dep not included') 70 done() 71 }) 72}) 73 74function fileExists (file) { 75 try { 76 return !!fs.statSync(path.resolve(targetpath, 'package', file)) 77 } catch (_) { 78 return false 79 } 80} 81 82function withFixture (t, fixture, tester) { 83 fixture.create(fixturepath) 84 mkdirp.sync(targetpath) 85 common.npm(['pack', fixturepath], {cwd: basepath}, extractAndCheck) 86 function extractAndCheck (err, code) { 87 if (err) throw err 88 t.is(code, 0, 'pack went ok') 89 extractTarball(checkTests) 90 } 91 function checkTests (err) { 92 if (err) throw err 93 tester(removeAndDone) 94 } 95 function removeAndDone (err) { 96 if (err) throw err 97 fixture.remove(fixturepath) 98 rimraf.sync(basepath) 99 t.done() 100 } 101} 102 103function extractTarball (cb) { 104 // Unpack to disk so case-insensitive filesystems are consistent 105 tar.extract({ 106 file: path.join(basepath, 'npm-test-files-1.2.5.tgz'), 107 cwd: targetpath 108 }).then(cb, cb) 109} 110