summaryrefslogtreecommitdiff
path: root/deps/npm/test/need-npm5-update/lifecycle-signal.js
blob: a003fb54ab49fa2fd12ffcdac5311501b1e1eb2c (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
var fs = require('graceful-fs')
var path = require('path')
var spawn = require('child_process').spawn

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

var node = process.execPath
var npm = require.resolve('../../bin/npm-cli.js')

var pkg = path.resolve(__dirname, 'lifecycle-signal')

var asyncScript = 'console.error(process.pid);process.on(\'SIGINT\',function (){'
asyncScript += 'setTimeout(function(){console.error(process.pid);process.exit()},10)'
asyncScript += '});setInterval(function(){},10);'

var zombieScript = 'console.error(process.pid);process.on(\'SIGINT\',function (){'
zombieScript += '});setInterval(function(){console.error(process.pid)},10);'

var SIGSEGV = require('constants').SIGSEGV // eslint-disable-line node/no-deprecated-api

var json = {
  name: 'lifecycle-signal',
  version: '1.2.5',
  scripts: {
    preinstall: 'node -e "process.kill(process.pid,\'SIGSEGV\')"',
    forever: 'node -e "console.error(process.pid);setInterval(function(){},1000)"',
    async: 'node -e "' + asyncScript + '"',
    zombie: 'node -e "' + zombieScript + '"'
  }
}

test('setup', function (t) {
  cleanup()
  mkdirp.sync(pkg)
  fs.writeFileSync(
    path.join(pkg, 'package.json'),
    JSON.stringify(json, null, 2)
  )

  process.chdir(pkg)
  t.end()
})

test('lifecycle signal abort', {
  skip: process.platform === 'win32' && 'windows does not use lifecycle signals'
}, function (t) {
  var child = spawn(node, [npm, 'install'], {
    cwd: pkg
  })
  child.on('close', function (code, signal) {
    // The error may be forwarded by the shell as an exit code rather than
    // the signal itself.
    t.ok((code === 128 + SIGSEGV) || signal === 'SIGSEGV')
    t.end()
  })
})

test('lifecycle propagate signal term to child', {
  /* This feature is broken. npm runs its lifecycle processes in a shell, and at
   * least `bash` doesn’t forward SIGTERM to its children. */
  skip: process.platform !== 'darwin' && 'broken'
}, function (t) {
  var innerChildPid
  var child = spawn(npm, ['run', 'forever'], {
    cwd: pkg
  })
  child.stderr.on('data', function (data) {
    innerChildPid = parseInt(data.toString(), 10)
    t.doesNotThrow(function () {
      process.kill(innerChildPid, 0) // inner child should be running
    })
    child.kill() // send SIGTERM to npm
  })
  child.on('exit', function (code, signal) {
    t.equal(code, null)
    t.equal(signal, 'SIGTERM')
    t.throws(function () {
      process.kill(innerChildPid, 0) // SIGTERM should have reached inner child
    })
    t.end()
  })
})

test('lifecycle wait for async child process exit', {
  skip: process.platform !== 'darwin' && 'broken'
}, function (t) {
  var innerChildPid
  var interupted
  var child = spawn(npm, ['run', 'async'], {
    cwd: pkg
  })
  child.stderr.on('data', function (data) {
    if (!interupted) {
      interupted = true
      child.kill('SIGINT')
    } else {
      innerChildPid = parseInt(data.toString(), 10)
    }
  })
  child.on('exit', function (code, signal) {
    t.ok(innerChildPid)
    t.end()
  })
})

test('lifecycle force kill using multiple SIGINT signals', {
  skip: process.platform !== 'darwin' && 'broken'
}, function (t) {
  var innerChildPid
  var interupted
  var child = spawn(npm, ['run', 'zombie'], {
    cwd: pkg
  })
  child.stderr.on('data', function (data) {
    if (!interupted) {
      interupted = true
      child.kill('SIGINT')
    } else {
      innerChildPid = parseInt(data.toString(), 10)
      child.kill('SIGINT')
    }
  })
  child.on('exit', function (code, signal) {
    t.ok(innerChildPid)
    t.end()
  })
})

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

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