1// verify that prepare runs on pack and publish 2var common = require('../common-tap') 3var test = require('tap').test 4var fs = require('graceful-fs') 5var join = require('path').join 6var mkdirp = require('mkdirp') 7var rimraf = require('rimraf') 8 9var pkg = common.pkg 10var tmp = join(pkg, 'tmp') 11var cache = common.cache 12 13test('setup', function (t) { 14 var n = 0 15 cleanup() 16 mkdirp(pkg, then()) 17 mkdirp(cache, then()) 18 mkdirp(tmp, then()) 19 function then () { 20 n++ 21 return function (er) { 22 if (er) throw er 23 if (--n === 0) next() 24 } 25 } 26 27 function next () { 28 fs.writeFile(join(pkg, 'package.json'), JSON.stringify({ 29 name: 'npm-test-prepare', 30 version: '1.2.5', 31 scripts: { prepare: 'echo ok' } 32 }), 'ascii', function (er) { 33 if (er) throw er 34 35 t.pass('setup done') 36 t.end() 37 }) 38 } 39}) 40 41test('test', function (t) { 42 var env = { 43 'npm_config_cache': cache, 44 'npm_config_tmp': tmp, 45 'npm_config_prefix': pkg, 46 'npm_config_global': 'false' 47 } 48 49 for (var i in process.env) { 50 if (!/^npm_config_/.test(i)) { 51 env[i] = process.env[i] 52 } 53 } 54 55 common.npm([ 56 'pack', 57 '--loglevel', 'warn' 58 ], { 59 cwd: pkg, 60 env: env, 61 nodeExecPath: process.execPath 62 }, function (err, code, stdout, stderr) { 63 t.equal(code, 0, 'pack finished successfully') 64 t.ifErr(err, 'pack finished successfully') 65 66 t.notOk(stderr, 'got stderr data:' + JSON.stringify('' + stderr)) 67 var c = stdout.trim() 68 var regex = new RegExp( 69 '> npm-test-prepare@1.2.5 prepare [^\\r\\n]+\\r?\\n' + 70 '> echo ok\\r?\\n' + 71 '\\r?\\n' + 72 'ok\\r?\\n' + 73 'npm-test-prepare-1.2.5.tgz', 'ig' 74 ) 75 76 t.match(c, regex) 77 t.end() 78 }) 79}) 80 81test('cleanup', function (t) { 82 cleanup() 83 t.pass('cleaned up') 84 t.end() 85}) 86 87function cleanup () { 88 rimraf.sync(pkg) 89} 90