Skip to content

Commit 7e6345a

Browse files
committed
feat: add sigkill and sigterm events
1 parent 853750e commit 7e6345a

File tree

6 files changed

+34
-8
lines changed

6 files changed

+34
-8
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
"lz-string": "^1.4.4",
4444
"pretty-format": "^27.0.2",
4545
"strip-ansi": "^6.0.1",
46-
"strip-final-newline": "^2.0.0"
46+
"strip-final-newline": "^2.0.0",
47+
"tree-kill": "^1.2.2"
4748
},
4849
"devDependencies": {
4950
"@types/lz-string": "^1.3.34",
5051
"@types/strip-final-newline": "^3.0.0",
5152
"inquirer": "^8.2.0",
53+
"is-running": "^2.1.0",
5254
"jest-in-case": "^1.0.2",
5355
"jest-snapshot-serializer-ansi": "^1.0.0",
5456
"jest-watch-select-projects": "^2.0.0",

src/__tests__/render-basics.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const {resolve} = require('path')
22
const {render} = require('../pure')
33
const {fireEvent} = require('../events')
4+
const isRunning = require('is-running')
5+
const {waitFor} = require("../wait-for");
46

57
test('Should handle stderr outputs with rejection', async () => {
68
await expect(() =>
@@ -44,7 +46,6 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
4446
expect(await findByText('First option: Two')).toBeTruthy()
4547
})
4648

47-
4849
test('fireEvent works when bound', async () => {
4950
const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [
5051
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
@@ -67,3 +68,21 @@ test('fireEvent works when bound', async () => {
6768
fireEvent.enter(instance)
6869
cleanup();
6970
})
71+
72+
73+
test('SigTerm works', async () => {
74+
const {findByText, cleanup} = await render('node', [
75+
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
76+
])
77+
78+
const instance = await findByText('First option')
79+
80+
expect(instance).toBeTruthy()
81+
82+
fireEvent.sigterm(instance);
83+
84+
cleanup()
85+
86+
await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy())
87+
})
88+

src/event-map.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
const kill = require('tree-kill');
2+
13
const eventMap = {
2-
down: '\x1B\x5B\x42',
3-
up: '\x1B\x5B\x41',
4-
enter: '\x0D',
4+
down: instance => instance.stdin.write('\x1B\x5B\x42'),
5+
up: instance => instance.stdin.write('\x1B\x5B\x41'),
6+
enter: instance => instance.stdin.write('\x0D'),
7+
sigterm: instance => kill(instance.pid),
8+
sigkill: instance => kill(instance.pid, 'SIGKILL')
59
}
610

711
export {eventMap}

src/events.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ type BoundFireEventRecord = Record<
99
ShiftArgs<FireEventRecord[string]>
1010
>
1111

12-
1312
const fireEvent = Object.entries(eventMap).reduce<FireEventRecord>(
14-
(prev, [eventName, keyCode]) => {
13+
(prev, [eventName, eventFn]) => {
1514
prev[eventName] = (instance) => {
16-
instance.stdin.write(keyCode)
15+
eventFn(instance)
1716
}
1817
return prev
1918
},

src/pure.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ async function render(
8585
stdin: exec.stdin,
8686
stdout: exec.stdout,
8787
stderr: exec.stderr,
88+
pid: exec.pid
8889
},
8990
getQueriesForElement(execOutputAPI),
9091
getFireEventForElement(execOutputAPI as unknown as TestInstance)

types/pure.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export interface TestInstance {
1616
stdout: Readable
1717
stderr: Readable
1818
stdoutStr: string
19+
pid: number | undefined
1920
}

0 commit comments

Comments
 (0)