Commit 4944122
Changed files (3)
src
test
src/question.ts
@@ -14,9 +14,12 @@
import { createInterface } from 'node:readline'
-export async function question(query: string, options: { choices: string[] }) {
+export async function question(
+ query?: string,
+ options?: { choices: string[] }
+): Promise<string> {
let completer = undefined
- if (Array.isArray(options?.choices)) {
+ if (options && Array.isArray(options.choices)) {
completer = function completer(line: string) {
const completions = options.choices
const hits = completions.filter((c) => c.startsWith(line))
test/cli.test.js
@@ -119,8 +119,8 @@ test('exceptions are caught', async () => {
})
test('eval works', async () => {
- let { stdout } = await $`node build/cli.js --eval '69'`
- assert.is(stdout, '69\n')
+ assert.is((await $`node build/cli.js --eval '42'`).stdout, '42\n')
+ assert.is((await $`node build/cli.js -e='69'`).stdout, '69\n')
})
test('eval works with stdin', async () => {
test/index.test.js
@@ -111,16 +111,30 @@ test('pipes are working', async () => {
}
})
-test('question', async () => {
+test('question() works', async () => {
+ let log = console.log
+ console.log = () => {}
let p = question('foo or bar? ', { choices: ['foo', 'bar'] })
-
setImmediate(() => {
process.stdin.emit('data', 'fo')
process.stdin.emit('data', '\t')
process.stdin.emit('data', '\n')
})
-
assert.is(await p, 'foo')
+ console.log = log
+})
+
+test('empty question() works', async () => {
+ let log = console.log
+ console.log = () => {}
+ let p = question(undefined, { choices: [] })
+ setImmediate(() => {
+ process.stdin.emit('data', 'xxx')
+ process.stdin.emit('data', '\t')
+ process.stdin.emit('data', '\n')
+ })
+ assert.match(await p, 'xxx')
+ console.log = log
})
test('ProcessPromise', async () => {