summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/test/tap/graceful-restart.js
blob: 56513fbf7ebbb216ded673e123b56c7fa59d25f2 (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
var fs = require('fs')
var resolve = require('path').resolve

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

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

var pkg = resolve(__dirname, 'graceful-restart')

var outGraceless = [
  'prerestart',
  'prestop',
  'stop',
  'poststop',
  'prestart',
  'start',
  'poststart',
  'postrestart',
  ''
].join('\n')

var outGraceful = [
  'prerestart',
  'restart',
  'postrestart',
  ''
].join('\n')

var pjGraceless = JSON.stringify({
  name: 'graceless',
  version: '1.2.3',
  scripts: {
    'prestop': 'echo prestop',
    'stop': 'echo stop',
    'poststop': 'echo poststop',
    'prerestart': 'echo prerestart',
    'postrestart': 'echo postrestart',
    'prestart': 'echo prestart',
    'start': 'echo start',
    'poststart': 'echo poststart'
  }
}, null, 2) + '\n'

var pjGraceful = JSON.stringify({
  name: 'graceful',
  version: '1.2.3',
  scripts: {
    'prestop': 'echo prestop',
    'stop': 'echo stop',
    'poststop': 'echo poststop',
    'prerestart': 'echo prerestart',
    'restart': 'echo restart',
    'postrestart': 'echo postrestart',
    'prestart': 'echo prestart',
    'start': 'echo start',
    'poststart': 'echo poststart'
  }
}, null, 2) + '\n'

test('setup', function (t) {
  bootstrap()
  t.end()
})

test('graceless restart', function (t) {
  fs.writeFileSync(resolve(pkg, 'package.json'), pjGraceless)
  createChild(['run-script', 'restart'], function (err, code, out) {
    t.ifError(err, 'restart finished successfully')
    t.equal(code, 0, 'npm run-script exited with code')
    t.equal(out.replace(/\r/g, ''), outGraceless, 'expected all scripts to run')
    t.end()
  })
})

test('graceful restart', function (t) {
  fs.writeFileSync(resolve(pkg, 'package.json'), pjGraceful)
  createChild(['run-script', 'restart'], function (err, code, out) {
    t.ifError(err, 'restart finished successfully')
    t.equal(code, 0, 'npm run-script exited with code')
    t.equal(out.replace(/\r/g, ''), outGraceful, 'expected only *restart scripts to run')
    t.end()
  })
})

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

function bootstrap () {
  mkdirp.sync(pkg)
}

function cleanup () {
  process.chdir(osenv.tmpdir())
  rimraf.sync(pkg)
}

function createChild (args, cb) {
  var env = {
    HOME: process.env.HOME,
    Path: process.env.PATH,
    PATH: process.env.PATH,
    'npm_config_loglevel': 'silent'
  }

  if (process.platform === 'win32') {
    env.npm_config_cache = '%APPDATA%\\npm-cache'
  }

  return common.npm(args, {
    cwd: pkg,
    stdio: ['ignore', 'pipe', 'ignore'],
    env: env
  }, cb)
}