Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit d5170bf

Browse files
bithavocKent C. Dodds
authored and
Kent C. Dodds
committed
fix: exit code should be set to 1 when os kills child process (#177)
* fixes #150 exit code when os kills child process * Update .all-contributorsrc
1 parent 450dae9 commit d5170bf

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"projectName": "cross-env",
33
"projectOwner": "kentcdodds",
4+
"repoType": "github",
45
"files": [
56
"README.md"
67
],
@@ -163,6 +164,15 @@
163164
"contributions": [
164165
"code"
165166
]
167+
},
168+
{
169+
"login": "bithavoc",
170+
"name": "Johan Hernandez",
171+
"avatar_url": "https://avatars3.githubusercontent.com/u/219289?v=4",
172+
"profile": "http://bithavoc.io",
173+
"contributions": [
174+
"code"
175+
]
166176
}
167177
]
168178
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Run scripts that set and use environment variables across platforms
1616
[![downloads][downloads-badge]][npm-stat]
1717

1818
[![MIT License][license-badge]][LICENSE]
19-
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-16-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Donate][donate-badge]][donate]
2222
[![Code of Conduct][coc-badge]][coc]
@@ -158,7 +158,7 @@ Thanks goes to these people ([emoji key][emojis]):
158158
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/499038?v=3" width="100px;"/><br /><sub><b>Ya Zhuang </b></sub>](https://zhuangya.me)<br />[🔌](#plugin-zhuangya "Plugin/utility libraries") [📖](https://github.com/kentcdodds/cross-env/commits?author=zhuangya "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/3440094?v=3" width="100px;"/><br /><sub><b>James Harris</b></sub>](https://wopian.me)<br />[📖](https://github.com/kentcdodds/cross-env/commits?author=wopian "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/8941730?v=3" width="100px;"/><br /><sub><b>compumike08</b></sub>](https://github.com/compumike08)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Acompumike08 "Bug reports") [📖](https://github.com/kentcdodds/cross-env/commits?author=compumike08 "Documentation") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=compumike08 "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2270425?v=3" width="100px;"/><br /><sub><b>Daniel Rodríguez Rivero</b></sub>](https://github.com/danielo515)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Adanielo515 "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=danielo515 "Code") [📖](https://github.com/kentcdodds/cross-env/commits?author=danielo515 "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1508477?v=3" width="100px;"/><br /><sub><b>Jonas Keinholz</b></sub>](https://github.com/inyono)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Ainyono "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=inyono "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=inyono "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1656170?v=3" width="100px;"/><br /><sub><b>Hugo Wood</b></sub>](https://github.com/hgwood)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Ahgwood "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=hgwood "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=hgwood "Tests") |
159159
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
160160
| [<img src="https://avatars0.githubusercontent.com/u/3715715?v=3" width="100px;"/><br /><sub><b>Thiebaud Thomas</b></sub>](https://github.com/thomasthiebaud)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Athomasthiebaud "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1715800?v=3" width="100px;"/><br /><sub><b>Daniel Rey López</b></sub>](https://daniel.blog)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=DanReyLop "Tests") | [<img src="https://avatars2.githubusercontent.com/u/6374832?v=3" width="100px;"/><br /><sub><b>Amila Welihinda</b></sub>](http://amilajack.com)<br />[🚇](#infra-amilajack "Infrastructure (Hosting, Build-Tools, etc)") | [<img src="https://avatars1.githubusercontent.com/u/1396?v=3" width="100px;"/><br /><sub><b>Paul Betts</b></sub>](https://twitter.com/paulcbetts)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Apaulcbetts "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=paulcbetts "Code") | [<img src="https://avatars1.githubusercontent.com/u/6371670?v=3" width="100px;"/><br /><sub><b>Turner Hayes</b></sub>](https://github.com/turnerhayes)<br />[🐛](https://github.com/kentcdodds/cross-env/issues?q=author%3Aturnerhayes "Bug reports") [💻](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=turnerhayes "Tests") | [<img src="https://avatars2.githubusercontent.com/u/22251956?v=4" width="100px;"/><br /><sub><b>Suhas Karanth</b></sub>](https://github.com/sudo-suhas)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas "Code") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas "Tests") | [<img src="https://avatars3.githubusercontent.com/u/512692?v=4" width="100px;"/><br /><sub><b>Sven</b></sub>](https://github.com/sventschui)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Code") [📖](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Documentation") [💡](#example-sventschui "Examples") [⚠️](https://github.com/kentcdodds/cross-env/commits?author=sventschui "Tests") |
161-
| [<img src="https://avatars0.githubusercontent.com/u/5522668?v=4" width="100px;"/><br /><sub><b>D. Nicolás Lopez Zelaya</b></sub>](https://github.com/NicoZelaya)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=NicoZelaya "Code") |
161+
| [<img src="https://avatars0.githubusercontent.com/u/5522668?v=4" width="100px;"/><br /><sub><b>D. Nicolás Lopez Zelaya</b></sub>](https://github.com/NicoZelaya)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=NicoZelaya "Code") | [<img src="https://avatars3.githubusercontent.com/u/219289?v=4" width="100px;"/><br /><sub><b>Johan Hernandez</b></sub>](http://bithavoc.io)<br />[💻](https://github.com/kentcdodds/cross-env/commits?author=bithavoc "Code") |
162162
<!-- ALL-CONTRIBUTORS-LIST:END -->
163163

164164
This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!

src/__tests__/index.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,33 +62,40 @@ it(`should handle quoted scripts`, () => {
6262
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo $GREETING && echo $NAME'], {
6363
shell: true,
6464
})
65-
expect(
66-
crossSpawnMock.spawn,
67-
).toHaveBeenCalledWith('echo $GREETING && echo $NAME', [], {
68-
stdio: 'inherit',
69-
shell: true,
70-
env: Object.assign({}, process.env, {
71-
GREETING: 'Hi',
72-
NAME: 'Joe',
73-
}),
74-
})
65+
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
66+
'echo $GREETING && echo $NAME',
67+
[],
68+
{
69+
stdio: 'inherit',
70+
shell: true,
71+
env: Object.assign({}, process.env, {
72+
GREETING: 'Hi',
73+
NAME: 'Joe',
74+
}),
75+
},
76+
)
7577
})
7678

7779
it(`should handle escaped characters`, () => {
7880
// this escapes \,",' and $
79-
crossEnv(['GREETING=Hi', 'NAME=Joe', 'echo \\"\\\'\\$GREETING\\\'\\" && echo $NAME'], {
80-
shell: true,
81-
})
82-
expect(
83-
crossSpawnMock.spawn,
84-
).toHaveBeenCalledWith("echo \"'$GREETING'\" && echo $NAME", [], {
85-
stdio: 'inherit',
86-
shell: true,
87-
env: Object.assign({}, process.env, {
88-
GREETING: 'Hi',
89-
NAME: 'Joe',
90-
}),
91-
})
81+
crossEnv(
82+
['GREETING=Hi', 'NAME=Joe', 'echo \\"\\\'\\$GREETING\\\'\\" && echo $NAME'],
83+
{
84+
shell: true,
85+
},
86+
)
87+
expect(crossSpawnMock.spawn).toHaveBeenCalledWith(
88+
'echo "\'$GREETING\'" && echo $NAME',
89+
[],
90+
{
91+
stdio: 'inherit',
92+
shell: true,
93+
env: Object.assign({}, process.env, {
94+
GREETING: 'Hi',
95+
NAME: 'Joe',
96+
}),
97+
},
98+
)
9299
})
93100

94101
it(`should do nothing given no command`, () => {
@@ -133,6 +140,24 @@ it(`should propagate kill signals`, () => {
133140
expect(crossSpawnMock.__mock.spawned.kill).toHaveBeenCalledWith('SIGBREAK')
134141
})
135142

143+
it(`should propagate unhandled exit signal`, () => {
144+
process.exit = jest.fn()
145+
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
146+
const spawnExitCallback = crossSpawnMock.__mock.spawned.on.mock.calls[0][1]
147+
const spawnExitCode = null
148+
spawnExitCallback(spawnExitCode)
149+
expect(process.exit).toHaveBeenCalledWith(1)
150+
})
151+
152+
it(`should propagate regular exit code`, () => {
153+
process.exit = jest.fn()
154+
testEnvSetting({FOO_ENV: 'foo=bar'}, 'FOO_ENV="foo=bar"')
155+
const spawnExitCallback = crossSpawnMock.__mock.spawned.on.mock.calls[0][1]
156+
const spawnExitCode = 0
157+
spawnExitCallback(spawnExitCode)
158+
expect(process.exit).toHaveBeenCalledWith(0)
159+
})
160+
136161
it(`should keep backslashes`, () => {
137162
isWindowsMock.__mock.returnValue = true
138163
crossEnv(['echo', '\\\\\\\\someshare\\\\somefolder'])
@@ -147,7 +172,6 @@ it(`should keep backslashes`, () => {
147172
isWindowsMock.__mock.reset()
148173
})
149174

150-
151175
function testEnvSetting(expected, ...envSettings) {
152176
if (expected.APPDATA === 2) {
153177
// kill the APPDATA to test both is undefined

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function crossEnv(args, options = {}) {
2525
process.on('SIGINT', () => proc.kill('SIGINT'))
2626
process.on('SIGBREAK', () => proc.kill('SIGBREAK'))
2727
process.on('SIGHUP', () => proc.kill('SIGHUP'))
28-
proc.on('exit', process.exit)
28+
proc.on('exit', code => {
29+
// exit code could be null when OS kills the process(out of memory, etc)
30+
process.exit(code === null ? 1 : code)
31+
})
2932
return proc
3033
}
3134
return null
@@ -61,7 +64,7 @@ function parseCommand(args) {
6164
const re = /\\\\|(\\)?'|([\\])(?=[$"\\])/g
6265
// Eliminate all matches except for "\'" => "'"
6366
return a.replace(re, m => {
64-
if (m === "\\\\") return "\\"
67+
if (m === '\\\\') return '\\'
6568
if (m === "\\'") return "'"
6669
return ''
6770
})

0 commit comments

Comments
 (0)