Skip to content

Commit

Permalink
chore: fix handle-questions helper to support autocomplete (#5629)
Browse files Browse the repository at this point in the history
The problem was that the inquirer autocomplete was not catching up with the date
we wrote to stdin. By giving it 50ms breathing room before we press ENTER the autocomplete
does work.

This also chooses the set-cookie template for the functions-create test
as it only has the `cookie` dependency to install instead of the whole apollo server previously.

Hopefully this will not make these tests timeout so much.

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
danez and kodiakhq[bot] authored Apr 17, 2023
1 parent 7e305a9 commit f30d409
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
4 changes: 2 additions & 2 deletions tests/integration/600.framework-detection.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const execa = require('execa')
const cliPath = require('./utils/cli-path.cjs')
const { getExecaOptions, withDevServer } = require('./utils/dev-server.cjs')
const got = require('./utils/got.cjs')
const { CONFIRM, DOWN, handleQuestions } = require('./utils/handle-questions.cjs')
const { DOWN, answerWithValue, handleQuestions } = require('./utils/handle-questions.cjs')
const { withSiteBuilder } = require('./utils/site-builder.cjs')
const { normalize } = require('./utils/snapshots.cjs')

Expand Down Expand Up @@ -241,7 +241,7 @@ test('should prompt when multiple frameworks are detected', async (t) => {
handleQuestions(childProcess, [
{
question: 'Multiple possible start commands found',
answer: `${DOWN}${CONFIRM}`,
answer: answerWithValue(DOWN),
},
])

Expand Down
8 changes: 4 additions & 4 deletions tests/integration/640.command.recipes.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('Generates a new VS Code settings file if one does not exist', async (t) =>
handleQuestions(childProcess, [
{
question: `A new VS Code settings file will be created at ${settingsPath}`,
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
])

Expand Down Expand Up @@ -60,7 +60,7 @@ test('Updates an existing VS Code settings file', async (t) => {
handleQuestions(childProcess, [
{
question: `There is a VS Code settings file at ${settingsPath}. Can we update it?`,
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
])

Expand All @@ -87,7 +87,7 @@ test('Does not generate a new VS Code settings file if the user does not confirm
handleQuestions(childProcess, [
{
question: `A new VS Code settings file will be created at ${settingsPath}`,
answer: answerWithValue('n'),
answer: answerWithValue(NO),
},
])

Expand Down Expand Up @@ -119,7 +119,7 @@ test('Handles JSON with comments', async (t) => {
handleQuestions(childProcess, [
{
question: `There is a VS Code settings file at ${settingsPath}. Can we update it?`,
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'The Deno VS Code extension is recommended. Would you like to install it now?',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand Down Expand Up @@ -100,19 +100,19 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'What route do you want your edge function to be invoked on?',
Expand Down Expand Up @@ -163,19 +163,19 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'What route do you want your edge function to be invoked on?',
Expand Down Expand Up @@ -232,23 +232,23 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(`${DOWN}${DOWN}${CONFIRM}`),
answer: answerWithValue(`set-cookie`),
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand All @@ -261,14 +261,10 @@ describe.concurrent('functions:create command', () => {

const { dependencies } = JSON.parse(await readFile(`${builder.directory}/package.json`, 'utf-8'))

// NOTE: Ideally we should be running this test with a specific template,
// but `inquirer-autocomplete-prompt` doesn't seem to work with the way
// we're mocking prompt responses with `handleQuestions`. Instead, we're
// choosing the second template in the list, assuming it's the first one
// that contains a `package.json` (currently that's `apollo-graphql`).
expect(await fileExistsAsync(`${builder.directory}/test/functions/apollo-graphql/apollo-graphql.js`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/apollo-graphql/package.json`)).toBe(false)
expect(typeof dependencies['apollo-server-lambda']).toBe('string')
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/set-cookie.js`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/package.json`)).toBe(false)
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/package-lock.json`)).toBe(false)
expect(typeof dependencies.cookie).toBe('string')

expect(dependencies['@netlify/functions']).toBe('^0.1.0')
})
Expand Down Expand Up @@ -304,23 +300,23 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(`${DOWN}${DOWN}${CONFIRM}`),
answer: answerWithValue(`set-cookie`),
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand All @@ -331,13 +327,9 @@ describe.concurrent('functions:create command', () => {

await childProcess

// NOTE: Ideally we should be running this test with a specific template,
// but `inquirer-autocomplete-prompt` doesn't seem to work with the way
// we're mocking prompt responses with `handleQuestions`. Instead, we're
// choosing the second template in the list, assuming it's the first one
// that contains a `package.json` (currently that's `apollo-graphql`).
expect(await fileExistsAsync(`${builder.directory}/test/functions/apollo-graphql/apollo-graphql.js`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/apollo-graphql/package.json`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/set-cookie.js`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/package.json`)).toBe(true)
expect(await fileExistsAsync(`${builder.directory}/test/functions/set-cookie/package-lock.json`)).toBe(true)
})
})
})
Expand Down Expand Up @@ -373,19 +365,19 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Select the language of your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand Down Expand Up @@ -430,19 +422,19 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand Down Expand Up @@ -491,19 +483,19 @@ describe.concurrent('functions:create command', () => {
const createFunctionQuestions = [
{
question: "Select the type of function you'd like to create",
answer: answerWithValue(`${DOWN}${CONFIRM}`),
answer: answerWithValue(DOWN),
},
{
question: 'Enter the path, relative to your site',
answer: answerWithValue('test/functions'),
},
{
question: 'Pick a template',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
{
question: 'Name your function',
answer: answerWithValue(CONFIRM),
answer: CONFIRM,
},
]

Expand Down
14 changes: 11 additions & 3 deletions tests/integration/utils/handle-questions.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Buffer } = require('buffer')
* Utility to mock the stdin of the cli. You must provide the correct number of
* questions correctly typed or the process will keep waiting for input.
* @param {ExecaChildProcess<string>} process
* @param {Array<{question: string, answer: string}>} questions
* @param {Array<{question: string, answer: string|string[]}>} questions
* @param {Array<number>} prompts
* - questions that you know the CLI will ask and respective answers to mock
*/
Expand All @@ -18,12 +18,20 @@ const handleQuestions = (process, questions, prompts = []) => {
if (index >= 0) {
prompts.push(index)
buffer = ''
process.stdin.write(Buffer.from(questions[index].answer))
const { answer } = questions[index]

writeResponse(process, Array.isArray(answer) ? answer : [answer])
}
})
}

const answerWithValue = (value) => `${value}${CONFIRM}`
const writeResponse = (process, responses) => {
const response = responses.shift()
if (response) process.stdin.write(Buffer.from(response))
if (responses.length !== 0) setTimeout(() => writeResponse(process, responses), 50)
}

const answerWithValue = (value = '') => [value, CONFIRM].flat()

const CONFIRM = '\n'
const DOWN = '\u001B[B'
Expand Down

1 comment on commit f30d409

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

  • Package size: 302 MB

Please sign in to comment.