Skip to content

Commit

Permalink
feat: add sigkill and sigterm events
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 23, 2021
1 parent 853750e commit 7e6345a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
"lz-string": "^1.4.4",
"pretty-format": "^27.0.2",
"strip-ansi": "^6.0.1",
"strip-final-newline": "^2.0.0"
"strip-final-newline": "^2.0.0",
"tree-kill": "^1.2.2"
},
"devDependencies": {
"@types/lz-string": "^1.3.34",
"@types/strip-final-newline": "^3.0.0",
"inquirer": "^8.2.0",
"is-running": "^2.1.0",
"jest-in-case": "^1.0.2",
"jest-snapshot-serializer-ansi": "^1.0.0",
"jest-watch-select-projects": "^2.0.0",
Expand Down
21 changes: 20 additions & 1 deletion src/__tests__/render-basics.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const {resolve} = require('path')
const {render} = require('../pure')
const {fireEvent} = require('../events')
const isRunning = require('is-running')
const {waitFor} = require("../wait-for");

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


test('fireEvent works when bound', async () => {
const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
Expand All @@ -67,3 +68,21 @@ test('fireEvent works when bound', async () => {
fireEvent.enter(instance)
cleanup();
})


test('SigTerm works', async () => {
const {findByText, cleanup} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

const instance = await findByText('First option')

expect(instance).toBeTruthy()

fireEvent.sigterm(instance);

cleanup()

await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy())
})

10 changes: 7 additions & 3 deletions src/event-map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const kill = require('tree-kill');

const eventMap = {
down: '\x1B\x5B\x42',
up: '\x1B\x5B\x41',
enter: '\x0D',
down: instance => instance.stdin.write('\x1B\x5B\x42'),
up: instance => instance.stdin.write('\x1B\x5B\x41'),
enter: instance => instance.stdin.write('\x0D'),
sigterm: instance => kill(instance.pid),
sigkill: instance => kill(instance.pid, 'SIGKILL')
}

export {eventMap}
5 changes: 2 additions & 3 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ type BoundFireEventRecord = Record<
ShiftArgs<FireEventRecord[string]>
>


const fireEvent = Object.entries(eventMap).reduce<FireEventRecord>(
(prev, [eventName, keyCode]) => {
(prev, [eventName, eventFn]) => {
prev[eventName] = (instance) => {
instance.stdin.write(keyCode)
eventFn(instance)
}
return prev
},
Expand Down
1 change: 1 addition & 0 deletions src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function render(
stdin: exec.stdin,
stdout: exec.stdout,
stderr: exec.stderr,
pid: exec.pid
},
getQueriesForElement(execOutputAPI),
getFireEventForElement(execOutputAPI as unknown as TestInstance)
Expand Down
1 change: 1 addition & 0 deletions types/pure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface TestInstance {
stdout: Readable
stderr: Readable
stdoutStr: string
pid: number | undefined
}

0 comments on commit 7e6345a

Please sign in to comment.