-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vadim Demedes
committed
Sep 23, 2019
1 parent
d9870e0
commit b41bd6e
Showing
2 changed files
with
119 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,97 @@ | ||
import {serial as test} from 'ava'; | ||
import {spawn} from 'node-pty'; | ||
|
||
test.cb('exit when user types "q" character', t => { | ||
const term = spawn('node', ['./fixtures/run', './use-input'], { | ||
const term = (fixture, args = []) => { | ||
let resolve; | ||
let reject; | ||
|
||
// eslint-disable-next-line promise/param-names | ||
const exitPromise = new Promise((resolve2, reject2) => { | ||
resolve = resolve2; | ||
reject = reject2; | ||
}); | ||
|
||
const ps = spawn('node', ['./fixtures/run', `./${fixture}`, ...args], { | ||
name: 'xterm-color', | ||
cols: 100, | ||
cwd: __dirname, | ||
env: process.env | ||
}); | ||
|
||
let output = ''; | ||
const result = { | ||
write: input => ps.write(input), | ||
output: '', | ||
waitForExit: () => exitPromise | ||
}; | ||
|
||
term.on('data', data => { | ||
output += data; | ||
ps.on('data', data => { | ||
result.output += data; | ||
}); | ||
|
||
let isExited = false; | ||
|
||
term.on('exit', code => { | ||
isExited = true; | ||
|
||
ps.on('exit', code => { | ||
if (code === 0) { | ||
t.true(output.includes('exited')); | ||
t.pass(); | ||
t.end(); | ||
resolve(); | ||
return; | ||
} | ||
|
||
t.fail(); | ||
t.end(); | ||
reject(new Error(`Process exited with non-zero exit code: ${code}`)); | ||
}); | ||
|
||
setTimeout(() => { | ||
t.false(isExited); | ||
term.write('q'); | ||
}, 1000); | ||
return result; | ||
}; | ||
|
||
test('handle lowercase character', async t => { | ||
const ps = term('use-input', ['lowercase']); | ||
ps.write('q'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle uppercase character', async t => { | ||
const ps = term('use-input', ['uppercase']); | ||
ps.write('Q'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle escape', async t => { | ||
const ps = term('use-input', ['escape']); | ||
ps.write('\u001B'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle ctrl', async t => { | ||
const ps = term('use-input', ['ctrl']); | ||
ps.write('\u0006'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle up arrow', async t => { | ||
const ps = term('use-input', ['upArrow']); | ||
ps.write('\u001B[A'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle down arrow', async t => { | ||
const ps = term('use-input', ['downArrow']); | ||
ps.write('\u001B[B'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
test('handle left arrow', async t => { | ||
const ps = term('use-input', ['leftArrow']); | ||
ps.write('\u001B[D'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); | ||
|
||
setTimeout(() => { | ||
term.kill(); | ||
t.fail(); | ||
t.end(); | ||
}, 1500); | ||
test('handle right arrow', async t => { | ||
const ps = term('use-input', ['rightArrow']); | ||
ps.write('\u001B[C'); | ||
await ps.waitForExit(); | ||
t.true(ps.output.includes('exited')); | ||
}); |