1 var fs = require('graceful-fs')
2 var path = require('path')
4 var mkdirp = require('mkdirp')
5 var test = require('tap').test
6 var rimraf = require('rimraf')
8 var common = require('../common-tap')
10 var pkg = path.resolve(__dirname, 'run-script')
11 var cache = path.resolve(pkg, 'cache')
12 var tmp = path.resolve(pkg, 'tmp')
14 var opts = { cwd: pkg }
16 var fullyPopulated = {
20 'start': 'node -e "console.log(process.argv[1] || \'start\')"',
21 'prewith-pre': 'node -e "console.log(process.argv[1] || \'pre\')"',
22 'with-pre': 'node -e "console.log(process.argv[1] || \'main\')"',
23 'with-post': 'node -e "console.log(process.argv[1] || \'main\')"',
24 'postwith-post': 'node -e "console.log(process.argv[1] || \'post\')"',
25 'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"',
26 'with-both': 'node -e "console.log(process.argv[1] || \'main\')"',
27 'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"',
28 'stop': 'node -e "console.log(process.argv[1] || \'stop\')"',
29 'env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
30 'npm-env-vars': 'node -e "console.log(process.env.npm_run_script_foo_var)"',
31 'package-env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
32 'prefixed-package-env-vars': 'node -e "console.log(process.env.npm_package_run_script_foo_var)"'
34 'run_script_foo_var': 'run_script_test_foo_val'
41 'prestart': 'echo prestart'
57 'prestart': 'echo prestart',
62 var preversionOnly = {
66 'preversion': 'echo preversion'
71 function testOutput (t, command, er, code, stdout, stderr) {
78 throw new Error('npm ' + command + ' stderr: ' + stderr.toString())
80 lines = stdout.trim().split('\n')
81 stdout = lines.filter(function (line) {
82 return line.trim() !== '' && line[0] !== '>'
85 t.equal(stdout, command)
89 function writeMetadata (object) {
91 path.resolve(pkg, 'package.json'),
92 JSON.stringify(object, null, 2) + '\n'
100 test('setup', function (t) {
104 writeMetadata(fullyPopulated)
108 test('npm run-script start', function (t) {
109 common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, 'start'))
112 test('npm run-script with args', function (t) {
113 common.npm(['run-script', 'start', '--', 'stop'], opts, testOutput.bind(null, t, 'stop'))
116 test('npm run-script with args that contain spaces', function (t) {
117 common.npm(['run-script', 'start', '--', 'hello world'], opts, testOutput.bind(null, t, 'hello world'))
120 test('npm run-script with args that contain single quotes', function (t) {
121 common.npm(['run-script', 'start', '--', 'they"re awesome'], opts, testOutput.bind(null, t, 'they"re awesome'))
124 test('npm run-script with args that contain double quotes', function (t) {
125 common.npm(['run-script', 'start', '--', 'what"s "up"?'], opts, testOutput.bind(null, t, 'what"s "up"?'))
128 test('npm run-script with args that contain ticks', function (t) {
129 common.npm(['run-script', 'start', '--', 'what\'s \'up\'?'], opts, testOutput.bind(null, t, 'what\'s \'up\'?'))
132 test('npm run-script with pre script', function (t) {
133 common.npm(['run-script', 'with-post'], opts, testOutput.bind(null, t, 'main;post'))
136 test('npm run-script with post script', function (t) {
137 common.npm(['run-script', 'with-pre'], opts, testOutput.bind(null, t, 'pre;main'))
140 test('npm run-script with both pre and post script', function (t) {
141 common.npm(['run-script', 'with-both'], opts, testOutput.bind(null, t, 'pre;main;post'))
144 test('npm run-script with both pre and post script and with args', function (t) {
145 common.npm(['run-script', 'with-both', '--', 'an arg'], opts, testOutput.bind(null, t, 'pre;an arg;post'))
148 test('npm run-script explicitly call pre script with arg', function (t) {
149 common.npm(['run-script', 'prewith-pre', '--', 'an arg'], opts, testOutput.bind(null, t, 'an arg'))
152 test('npm run-script test', function (t) {
153 common.npm(['run-script', 'test'], opts, function (er, code, stdout, stderr) {
154 t.ifError(er, 'npm run-script test ran without issue')
155 t.notOk(stderr, 'should not generate errors')
160 test('npm run-script env', function (t) {
161 common.npm(['run-script', 'env'], opts, function (er, code, stdout, stderr) {
162 t.ifError(er, 'using default env script')
163 t.notOk(stderr, 'should not generate errors')
164 t.ok(stdout.indexOf('npm_config_init_version') > 0, 'expected values in var list')
169 test('npm run-script nonexistent-script', function (t) {
170 common.npm(['run-script', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
171 t.ifError(er, 'npm run-script nonexistent-script did not cause npm to explode')
172 t.ok(stderr, 'should generate errors')
177 test('npm run-script restart when there isn\'t restart', function (t) {
178 common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start'))
181 test('npm run-script nonexistent-script with --if-present flag', function (t) {
182 common.npm(['run-script', '--if-present', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
183 t.ifError(er, 'npm run-script --if-present non-existent-script ran without issue')
184 t.notOk(stderr, 'should not generate errors')
189 test('npm run-script env vars accessible', function (t) {
190 process.env.run_script_foo_var = 'run_script_test_foo_val'
191 common.npm(['run-script', 'env-vars'], {
193 }, function (err, code, stdout, stderr) {
194 t.ifError(err, 'ran run-script without crashing')
195 t.equal(code, 0, 'exited normally')
196 t.equal(stderr, '', 'no error output')
198 new RegExp(process.env.run_script_foo_var),
199 'script had env access')
204 test('npm run-script package.json vars injected', function (t) {
205 common.npm(['run-script', 'package-env-vars'], {
207 }, function (err, code, stdout, stderr) {
208 t.ifError(err, 'ran run-script without crashing')
209 t.equal(code, 0, 'exited normally')
210 t.equal(stderr, '', 'no error output')
212 new RegExp(fullyPopulated.run_script_foo_var),
213 'script injected package.json value')
218 test('npm run-script package.json vars injected with prefix', function (t) {
219 common.npm(['run-script', 'prefixed-package-env-vars'], {
221 }, function (err, code, stdout, stderr) {
222 t.ifError(err, 'ran run-script without crashing')
223 t.equal(code, 0, 'exited normally')
224 t.equal(stderr, '', 'no error output')
226 new RegExp(fullyPopulated.run_script_foo_var),
227 'script injected npm_package-prefixed package.json value')
232 test('npm run-script env vars stripped npm-prefixed', function (t) {
233 process.env.npm_run_script_foo_var = 'run_script_test_foo_val'
234 common.npm(['run-script', 'npm-env-vars'], {
236 }, function (err, code, stdout, stderr) {
237 t.ifError(err, 'ran run-script without crashing')
238 t.equal(code, 0, 'exited normally')
239 t.equal(stderr, '', 'no error output')
241 new RegExp(process.env.npm_run_script_foo_var),
242 'script stripped npm-prefixed env var')
247 test('npm run-script no-params (lifecycle only)', function (t) {
249 'Lifecycle scripts included in scripted:',
255 writeMetadata(lifecycleOnly)
257 common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
258 t.ifError(err, 'ran run-script without parameters without crashing')
259 t.notOk(code, 'npm exited without error code')
260 t.notOk(stderr, 'npm printed nothing to stderr')
261 t.equal(stdout, expected, 'got expected output')
266 test('npm run-script no-params (preversion only)', function (t) {
268 'Lifecycle scripts included in scripted:',
274 writeMetadata(preversionOnly)
276 common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
277 t.ifError(err, 'ran run-script without parameters without crashing')
278 t.notOk(code, 'npm exited without error code')
279 t.notOk(stderr, 'npm printed nothing to stderr')
280 t.equal(stdout, expected, 'got expected output')
285 test('npm run-script no-params (direct only)', function (t) {
287 'Scripts available in scripted via `npm run-script`:',
293 writeMetadata(directOnly)
295 common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
296 t.ifError(err, 'ran run-script without parameters without crashing')
297 t.notOk(code, 'npm exited without error code')
298 t.notOk(stderr, 'npm printed nothing to stderr')
299 t.equal(stdout, expected, 'got expected output')
304 test('npm run-script no-params (direct only)', function (t) {
306 'Lifecycle scripts included in scripted:',
310 'available via `npm run-script`:',
318 common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
319 t.ifError(err, 'ran run-script without parameters without crashing')
320 t.notOk(code, 'npm exited without error code')
321 t.notOk(stderr, 'npm printed nothing to stderr')
322 t.equal(stdout, expected, 'got expected output')
327 test('cleanup', function (t) {