• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var fs = require('fs')
2var resolve = require('path').resolve
3var test = require('tap').test
4var common = require('../common-tap.js')
5var pkg = common.pkg
6
7var outGraceless = [
8  'prerestart',
9  'prestop',
10  'stop',
11  'poststop',
12  'prestart',
13  'start',
14  'poststart',
15  'postrestart',
16  ''
17].join('\n')
18
19var outGraceful = [
20  'prerestart',
21  'restart',
22  'postrestart',
23  ''
24].join('\n')
25
26var pjGraceless = JSON.stringify({
27  name: 'graceless',
28  version: '1.2.3',
29  scripts: {
30    'prestop': 'echo prestop',
31    'stop': 'echo stop',
32    'poststop': 'echo poststop',
33    'prerestart': 'echo prerestart',
34    'postrestart': 'echo postrestart',
35    'prestart': 'echo prestart',
36    'start': 'echo start',
37    'poststart': 'echo poststart'
38  }
39}, null, 2) + '\n'
40
41var pjGraceful = JSON.stringify({
42  name: 'graceful',
43  version: '1.2.3',
44  scripts: {
45    'prestop': 'echo prestop',
46    'stop': 'echo stop',
47    'poststop': 'echo poststop',
48    'prerestart': 'echo prerestart',
49    'restart': 'echo restart',
50    'postrestart': 'echo postrestart',
51    'prestart': 'echo prestart',
52    'start': 'echo start',
53    'poststart': 'echo poststart'
54  }
55}, null, 2) + '\n'
56
57test('graceless restart', function (t) {
58  fs.writeFileSync(resolve(pkg, 'package.json'), pjGraceless)
59  createChild(['run-script', 'restart'], function (err, code, out) {
60    t.ifError(err, 'restart finished successfully')
61    t.equal(code, 0, 'npm run-script exited with code')
62    t.equal(out.replace(/\r/g, ''), outGraceless, 'expected all scripts to run')
63    t.end()
64  })
65})
66
67test('graceful restart', function (t) {
68  fs.writeFileSync(resolve(pkg, 'package.json'), pjGraceful)
69  createChild(['run-script', 'restart'], function (err, code, out) {
70    t.ifError(err, 'restart finished successfully')
71    t.equal(code, 0, 'npm run-script exited with code')
72    t.equal(out.replace(/\r/g, ''), outGraceful, 'expected only *restart scripts to run')
73    t.end()
74  })
75})
76
77function createChild (args, cb) {
78  var env = {
79    HOME: process.env.HOME,
80    Path: process.env.PATH,
81    PATH: process.env.PATH,
82    'npm_config_loglevel': 'silent'
83  }
84
85  if (process.platform === 'win32') {
86    env.npm_config_cache = '%APPDATA%\\npm-cache'
87  }
88
89  return common.npm(args, {
90    cwd: pkg,
91    stdio: ['ignore', 'pipe', 'ignore'],
92    env: env
93  }, cb)
94}
95