1/* eslint-disable camelcase */ 2var child_process = require('child_process') 3var readdir = require('graceful-fs').readdirSync 4var resolve = require('path').resolve 5 6var rimraf = require('rimraf') 7var test = require('tap').test 8var which = require('which') 9 10var common = require('../common-tap.js') 11var escapeArg = require('../../lib/utils/escape-arg.js') 12var Tacks = require('tacks') 13var Dir = Tacks.Dir 14var File = Tacks.File 15 16var fixture = new Tacks(Dir({ 17 deps: Dir({ 18 gitch: Dir({ 19 '.npmignore': File( 20 't.js\n' 21 ), 22 '.gitignore': File( 23 'node_modules/\n' 24 ), 25 'a.js': File( 26 "console.log('hi');" 27 ), 28 't.js': File( 29 "require('tap').test(function (t) { t.pass('I am a test!'); t.end(); });" 30 ), 31 'package.json': File({ 32 name: 'gitch', 33 version: '1.0.0', 34 private: true, 35 main: 'a.js' 36 }) 37 }) 38 }), 39 'node_modules': Dir({ 40 }) 41})) 42 43var testdir = common.pkg 44var cachedir = common.cache 45var dep = resolve(testdir, 'deps', 'gitch') 46var packname = 'gitch-1.0.0.tgz' 47var packed = resolve(testdir, packname) 48var modules = resolve(testdir, 'node_modules') 49var installed = resolve(modules, 'gitch') 50var expected = [ 51 'a.js', 52 'package.json' 53].sort() 54 55var NPM_OPTS = { 56 cwd: testdir, 57 env: common.newEnv().extend({ 58 npm_config_cache: cachedir 59 }) 60} 61 62function exec (todo, opts, cb) { 63 console.log(' # EXEC:', todo) 64 child_process.exec(todo, opts, cb) 65} 66 67test('setup', function (t) { 68 setup(function (er) { 69 t.ifError(er, 'setup ran OK') 70 71 t.end() 72 }) 73}) 74 75test('npm pack directly from directory', function (t) { 76 packInstallTest(dep, t) 77}) 78 79test('npm pack via git', function (t) { 80 var urlPath = dep 81 .replace(/\\/g, '/') // fixup slashes for Windows 82 .replace(/^\/+/, '') // remove any leading slashes 83 packInstallTest('git+file:///' + urlPath, t) 84}) 85 86test('cleanup', function (t) { 87 cleanup() 88 89 t.end() 90}) 91 92function packInstallTest (spec, t) { 93 console.log(' # pack', spec) 94 common.npm( 95 [ 96 '--loglevel', 'error', 97 'pack', spec 98 ], 99 NPM_OPTS, 100 function (err, code, stdout, stderr) { 101 if (err) throw err 102 t.is(code, 0, 'npm pack exited cleanly') 103 t.is(stderr, '', 'npm pack ran silently') 104 t.is(stdout.trim(), packname, 'got expected package name') 105 106 common.npm( 107 [ 108 '--loglevel', 'error', 109 'install', packed 110 ], 111 NPM_OPTS, 112 function (err, code, stdout, stderr) { 113 if (err) throw err 114 t.is(code, 0, 'npm install exited cleanly') 115 t.is(stderr, '', 'npm install ran silently') 116 117 var actual = readdir(installed).sort() 118 t.isDeeply(actual, expected, 'no unexpected files in packed directory') 119 120 rimraf(packed, function () { 121 t.end() 122 }) 123 } 124 ) 125 } 126 ) 127} 128 129function cleanup () { 130 fixture.remove(testdir) 131 rimraf.sync(testdir) 132} 133 134function setup (cb) { 135 cleanup() 136 137 fixture.create(testdir) 138 139 common.npm( 140 [ 141 '--loglevel', 'error', 142 'cache', 'clean', '--force' 143 ], 144 NPM_OPTS, 145 function (er, code, _, stderr) { 146 if (er) return cb(er) 147 if (stderr) return cb(new Error('npm cache clean error: ' + stderr)) 148 if (code) return cb(new Error('npm cache nonzero exit: ' + code)) 149 150 which('git', function found (er, gitPath) { 151 if (er) return cb(er) 152 153 var git = escapeArg(gitPath) 154 155 exec(git + ' init', {cwd: dep}, init) 156 157 function init (er, _, stderr) { 158 if (er) return cb(er) 159 if (stderr) return cb(new Error('git init error: ' + stderr)) 160 161 exec(git + " config user.name 'Phantom Faker'", {cwd: dep}, user) 162 } 163 164 function user (er, _, stderr) { 165 if (er) return cb(er) 166 if (stderr) return cb(new Error('git config error: ' + stderr)) 167 168 exec(git + ' config user.email nope@not.real', {cwd: dep}, email) 169 } 170 171 function email (er, _, stderr) { 172 if (er) return cb(er) 173 if (stderr) return cb(new Error('git config error: ' + stderr)) 174 175 exec(git + ' config core.autocrlf input', {cwd: dep}, autocrlf) 176 } 177 178 function autocrlf (er, _, stderr) { 179 if (er) return cb(er) 180 if (stderr) return cb(new Error('git config error: ' + stderr)) 181 182 exec(git + ' add .', {cwd: dep}, addAll) 183 } 184 185 function addAll (er, _, stderr) { 186 if (er) return cb(er) 187 if (stderr) return cb(new Error('git add . error: ' + stderr)) 188 189 exec(git + ' commit -m boot', {cwd: dep}, commit) 190 } 191 192 function commit (er, _, stderr) { 193 if (er) return cb(er) 194 if (stderr) return cb(new Error('git commit error: ' + stderr)) 195 cb() 196 } 197 }) 198 } 199 ) 200} 201