1var fs = require('graceful-fs') 2var path = require('path') 3 4var mkdirp = require('mkdirp') 5var rimraf = require('rimraf') 6var test = require('tap').test 7 8var common = require('../common-tap') 9 10// ignore-scripts/package.json has scripts that always exit with non-zero error 11// codes. 12var pkg = common.pkg 13 14var gypfile = 'bad_binding_file\n' 15var json = { 16 author: 'Milton the Aussie', 17 name: 'ignore-scripts', 18 version: '0.0.0', 19 scripts: { 20 prepublish: 'exit 123', 21 publish: 'exit 123', 22 postpublish: 'exit 123', 23 preinstall: 'exit 123', 24 install: 'exit 123', 25 postinstall: 'exit 123', 26 preuninstall: 'exit 123', 27 uninstall: 'exit 123', 28 postuninstall: 'exit 123', 29 pretest: 'exit 123', 30 test: 'exit 123', 31 posttest: 'exit 123', 32 prestop: 'exit 123', 33 stop: 'exit 123', 34 poststop: 'exit 123', 35 prestart: 'exit 123', 36 start: 'exit 123', 37 poststart: 'exit 123', 38 prerestart: 'exit 123', 39 restart: 'exit 123', 40 postrestart: 'exit 123', 41 preversion: 'exit 123', 42 version: 'exit 123', 43 postversion: 'exit 123', 44 preshrinkwrap: 'exit 123', 45 shrinkwrap: 'exit 123', 46 postshrinkwrap: 'exit 123' 47 } 48} 49 50test('setup', function (t) { 51 setup() 52 t.end() 53}) 54 55test('ignore-scripts: install using the option', function (t) { 56 createChild(['install', '--ignore-scripts'], function (err, code) { 57 t.ifError(err, 'install with scripts ignored finished successfully') 58 t.equal(code, 0, 'npm install exited with code') 59 t.end() 60 }) 61}) 62 63test('ignore-scripts: install NOT using the option', function (t) { 64 createChild(['install'], function (err, code) { 65 t.ifError(err, 'install with scripts successful') 66 t.notEqual(code, 0, 'npm install exited with code') 67 t.end() 68 }) 69}) 70 71var scripts = [ 72 'prepublish', 'publish', 'postpublish', 73 'preinstall', 'install', 'postinstall', 74 'preuninstall', 'uninstall', 'postuninstall', 75 'pretest', 'test', 'posttest', 76 'prestop', 'stop', 'poststop', 77 'prestart', 'start', 'poststart', 78 'prerestart', 'restart', 'postrestart', 79 'preversion', 'version', 'postversion', 80 'preshrinkwrap', 'shrinkwrap', 'postshrinkwrap' 81] 82 83scripts.forEach(function (script) { 84 test('ignore-scripts: run-script ' + script + ' using the option', function (t) { 85 createChild(['--ignore-scripts', 'run-script', script], function (err, code, stdout, stderr) { 86 t.ifError(err, 'run-script ' + script + ' with ignore-scripts successful') 87 t.equal(code, 0, 'npm run-script exited with code') 88 t.end() 89 }) 90 }) 91}) 92 93scripts.forEach(function (script) { 94 test('ignore-scripts: run-script ' + script + ' NOT using the option', function (t) { 95 createChild(['run-script', script], function (err, code) { 96 t.ifError(err, 'run-script ' + script + ' finished successfully') 97 t.notEqual(code, 0, 'npm run-script exited with code') 98 t.end() 99 }) 100 }) 101}) 102 103test('cleanup', function (t) { 104 cleanup() 105 t.end() 106}) 107 108function cleanup () { 109 rimraf.sync(pkg) 110} 111 112function setup () { 113 cleanup() 114 mkdirp.sync(pkg) 115 fs.writeFileSync(path.join(pkg, 'binding.gyp'), gypfile) 116 fs.writeFileSync( 117 path.join(pkg, 'package.json'), 118 JSON.stringify(json, null, 2) 119 ) 120} 121 122function createChild (args, cb) { 123 return common.npm( 124 args.concat(['--loglevel', 'silent']), 125 { cwd: pkg }, 126 cb 127 ) 128} 129