summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/run-script.js
blob: a937abadf2c1aaf877f168c4030eadc2264e3117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
var fs = require('graceful-fs')
var path = require('path')

var mkdirp = require('mkdirp')
var test = require('tap').test
var rimraf = require('rimraf')

var common = require('../common-tap')

var pkg = common.pkg
var cache = path.resolve(pkg, 'cache')
var tmp = path.resolve(pkg, 'tmp')

var opts = { cwd: pkg }

var fullyPopulated = {
  'name': 'runscript',
  'version': '1.2.3',
  'scripts': {
    'start': 'node -e "console.log(process.argv[1] || \'start\')"',
    'prewith-pre': 'node -e "console.log(process.argv[1] || \'pre\')"',
    'with-pre': 'node -e "console.log(process.argv[1] || \'main\')"',
    'with-post': 'node -e "console.log(process.argv[1] || \'main\')"',
    'postwith-post': 'node -e "console.log(process.argv[1] || \'post\')"',
    'prewith-both': 'node -e "console.log(process.argv[1] || \'pre\')"',
    'with-both': 'node -e "console.log(process.argv[1] || \'main\')"',
    'postwith-both': 'node -e "console.log(process.argv[1] || \'post\')"',
    'stop': 'node -e "console.log(process.argv[1] || \'stop\')"',
    'env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
    'npm-env-vars': 'node -e "console.log(process.env.npm_run_script_foo_var)"',
    'package-env-vars': 'node -e "console.log(process.env.run_script_foo_var)"',
    'prefixed-package-env-vars': 'node -e "console.log(process.env.npm_package_run_script_foo_var)"'
  },
  'run_script_foo_var': 'run_script_test_foo_val'
}

var lifecycleOnly = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'prestart': 'echo prestart'
  }
}

var directOnly = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'whoa': 'echo whoa'
  }
}

var both = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'prestart': 'echo prestart',
    'whoa': 'echo whoa'
  }
}

var preversionOnly = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'preversion': 'echo preversion'
  }
}

var exitCode = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'start': 'node -e "process.exit(7)"'
  }
}

var shell = {
  name: 'scripted',
  version: '1.2.3',
  scripts: {
    'start': 'echo foo'
  }
}

function testOutput (t, command, er, code, stdout, stderr) {
  var lines

  if (er) throw er

  if (stderr) {
    throw new Error('npm ' + command + ' stderr: ' + stderr.toString())
  }

  lines = stdout.trim().split('\n')
  stdout = lines.filter(function (line) {
    return line.trim() !== '' && line[0] !== '>'
  }).join(';')

  t.equal(stdout, command)
  t.end()
}

function writeMetadata (object) {
  fs.writeFileSync(
    path.resolve(pkg, 'package.json'),
    JSON.stringify(object, null, 2) + '\n'
  )
}

function cleanup () {
  rimraf.sync(pkg)
}

test('setup', function (t) {
  cleanup()
  mkdirp.sync(cache)
  mkdirp.sync(tmp)
  writeMetadata(fullyPopulated)
  t.end()
})

test('npm run-script start', function (t) {
  common.npm(['run-script', 'start'], opts, testOutput.bind(null, t, 'start'))
})

test('npm run-script with args', function (t) {
  common.npm(['run-script', 'start', '--', 'stop'], opts, testOutput.bind(null, t, 'stop'))
})

test('npm run-script with args that contain spaces', function (t) {
  common.npm(['run-script', 'start', '--', 'hello world'], opts, testOutput.bind(null, t, 'hello world'))
})

test('npm run-script with args that contain single quotes', function (t) {
  common.npm(['run-script', 'start', '--', 'they"re awesome'], opts, testOutput.bind(null, t, 'they"re awesome'))
})

test('npm run-script with args that contain double quotes', function (t) {
  common.npm(['run-script', 'start', '--', 'what"s "up"?'], opts, testOutput.bind(null, t, 'what"s "up"?'))
})

test('npm run-script with args that contain ticks', function (t) {
  common.npm(['run-script', 'start', '--', 'what\'s \'up\'?'], opts, testOutput.bind(null, t, 'what\'s \'up\'?'))
})

test('npm run-script with pre script', function (t) {
  common.npm(['run-script', 'with-post'], opts, testOutput.bind(null, t, 'main;post'))
})

test('npm run-script with post script', function (t) {
  common.npm(['run-script', 'with-pre'], opts, testOutput.bind(null, t, 'pre;main'))
})

test('npm run-script with both pre and post script', function (t) {
  common.npm(['run-script', 'with-both'], opts, testOutput.bind(null, t, 'pre;main;post'))
})

test('npm run-script with both pre and post script and with args', function (t) {
  common.npm(['run-script', 'with-both', '--', 'an arg'], opts, testOutput.bind(null, t, 'pre;an arg;post'))
})

test('npm run-script explicitly call pre script with arg', function (t) {
  common.npm(['run-script', 'prewith-pre', '--', 'an arg'], opts, testOutput.bind(null, t, 'an arg'))
})

test('npm run-script test', function (t) {
  common.npm(['run-script', 'test'], opts, function (er, code, stdout, stderr) {
    t.ifError(er, 'npm run-script test ran without issue')
    t.notOk(stderr, 'should not generate errors')
    t.end()
  })
})

test('npm run-script env', function (t) {
  common.npm(['run-script', 'env'], opts, function (er, code, stdout, stderr) {
    t.ifError(er, 'using default env script')
    t.notOk(stderr, 'should not generate errors')
    t.ok(stdout.indexOf('npm_config_init_version') > 0, 'expected values in var list')
    t.end()
  })
})

test('npm run-script nonexistent-script', function (t) {
  common.npm(['run-script', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
    t.ifError(er, 'npm run-script nonexistent-script did not cause npm to explode')
    t.ok(stderr, 'should generate errors')
    t.end()
  })
})

test('npm run-script restart when there isn\'t restart', function (t) {
  common.npm(['run-script', 'restart'], opts, testOutput.bind(null, t, 'stop;start'))
})

test('npm run-script nonexistent-script with --if-present flag', function (t) {
  common.npm(['run-script', '--if-present', 'nonexistent-script'], opts, function (er, code, stdout, stderr) {
    t.ifError(er, 'npm run-script --if-present non-existent-script ran without issue')
    t.notOk(stderr, 'should not generate errors')
    t.end()
  })
})

test('npm run-script env vars accessible', function (t) {
  process.env.run_script_foo_var = 'run_script_test_foo_val'
  common.npm(['run-script', 'env-vars'], {
    cwd: pkg
  }, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without crashing')
    t.equal(code, 0, 'exited normally')
    t.equal(stderr, '', 'no error output')
    t.match(stdout,
      new RegExp(process.env.run_script_foo_var),
      'script had env access')
    t.end()
  })
})

test('npm run-script package.json vars injected', function (t) {
  common.npm(['run-script', 'package-env-vars'], {
    cwd: pkg
  }, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without crashing')
    t.equal(code, 0, 'exited normally')
    t.equal(stderr, '', 'no error output')
    t.match(stdout,
      new RegExp(fullyPopulated.run_script_foo_var),
      'script injected package.json value')
    t.end()
  })
})

test('npm run-script package.json vars injected with prefix', function (t) {
  common.npm(['run-script', 'prefixed-package-env-vars'], {
    cwd: pkg
  }, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without crashing')
    t.equal(code, 0, 'exited normally')
    t.equal(stderr, '', 'no error output')
    t.match(stdout,
      new RegExp(fullyPopulated.run_script_foo_var),
      'script injected npm_package-prefixed package.json value')
    t.end()
  })
})

test('npm run-script env vars stripped npm-prefixed', function (t) {
  process.env.npm_run_script_foo_var = 'run_script_test_foo_val'
  common.npm(['run-script', 'npm-env-vars'], {
    cwd: pkg
  }, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without crashing')
    t.equal(code, 0, 'exited normally')
    t.equal(stderr, '', 'no error output')
    t.notMatch(stdout,
      new RegExp(process.env.npm_run_script_foo_var),
      'script stripped npm-prefixed env var')
    t.end()
  })
})

test('npm run-script no-params (lifecycle only)', function (t) {
  var expected = [
    'Lifecycle scripts included in scripted:',
    '  prestart',
    '    echo prestart',
    ''
  ].join('\n')

  writeMetadata(lifecycleOnly)

  common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without parameters without crashing')
    t.notOk(code, 'npm exited without error code')
    t.notOk(stderr, 'npm printed nothing to stderr')
    t.equal(stdout, expected, 'got expected output')
    t.end()
  })
})

test('npm run-script no-params (preversion only)', function (t) {
  var expected = [
    'Lifecycle scripts included in scripted:',
    '  preversion',
    '    echo preversion',
    ''
  ].join('\n')

  writeMetadata(preversionOnly)

  common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without parameters without crashing')
    t.notOk(code, 'npm exited without error code')
    t.notOk(stderr, 'npm printed nothing to stderr')
    t.equal(stdout, expected, 'got expected output')
    t.end()
  })
})

test('npm run-script no-params (direct only)', function (t) {
  var expected = [
    'Scripts available in scripted via `npm run-script`:',
    '  whoa',
    '    echo whoa',
    ''
  ].join('\n')

  writeMetadata(directOnly)

  common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without parameters without crashing')
    t.notOk(code, 'npm exited without error code')
    t.notOk(stderr, 'npm printed nothing to stderr')
    t.equal(stdout, expected, 'got expected output')
    t.end()
  })
})

test('npm run-script script-shell config', function (t) {
  writeMetadata(shell)

  common.npm(['run-script', 'start', '--script-shell', 'echo'], opts, testOutput.bind(null, t, '-c echo foo'))
})

test('npm run-script no-params (direct only)', function (t) {
  var expected = [
    'Lifecycle scripts included in scripted:',
    '  prestart',
    '    echo prestart',
    '',
    'available via `npm run-script`:',
    '  whoa',
    '    echo whoa',
    ''
  ].join('\n')

  writeMetadata(both)

  common.npm(['run-script'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without parameters without crashing')
    t.notOk(code, 'npm exited without error code')
    t.notOk(stderr, 'npm printed nothing to stderr')
    t.equal(stdout, expected, 'got expected output')
    t.end()
  })
})

test('npm run-script keep non-zero exit code', function (t) {
  writeMetadata(exitCode)

  common.npm(['run-script', 'start'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without parameters without crashing')
    t.equal(code, 7, 'got expected exit code')
    t.ok(stderr, 'should generate errors')
    t.end()
  })
})

test('npm run-script nonexistent script and display suggestions', function (t) {
  writeMetadata(directOnly)

  common.npm(['run-script', 'whoop'], opts, function (err, code, stdout, stderr) {
    t.ifError(err, 'ran run-script without crashing')
    t.equal(code, 1, 'got expected exit code')
    t.has(stderr, 'Did you mean this?')
    t.end()
  })
})

test('cleanup', function (t) {
  cleanup()
  t.end()
})