diff options
Diffstat (limited to 'packages/create-astro/test')
-rw-r--r-- | packages/create-astro/test/directory-step.test.js | 50 | ||||
-rw-r--r-- | packages/create-astro/test/install-step.test.js | 71 | ||||
-rw-r--r-- | packages/create-astro/test/utils.js | 40 |
3 files changed, 119 insertions, 42 deletions
diff --git a/packages/create-astro/test/directory-step.test.js b/packages/create-astro/test/directory-step.test.js index 122bd3f8f..02ddc672a 100644 --- a/packages/create-astro/test/directory-step.test.js +++ b/packages/create-astro/test/directory-step.test.js @@ -1,54 +1,20 @@ -import { execa } from 'execa'; -import { fileURLToPath } from 'url'; -import { dirname, resolve } from 'path'; +import { resolve } from 'path'; import { promises, existsSync } from 'fs'; +import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - -const createAstroError = new Error( - 'Timed out waiting for create-astro to respond with expected output.' -); -const timeout = 5000; - -const instructions = { - directory: 'Where would you like to create your app?', - template: 'Which app template would you like to use?', -}; const inputs = { nonEmptyDir: './fixtures/select-directory/nonempty-dir', emptyDir: './fixtures/select-directory/empty-dir', nonexistentDir: './fixtures/select-directory/banana-dir', }; -function promiseWithTimeout(testFn) { - return new Promise((resolve, reject) => { - const timeoutEvent = setTimeout(() => { - reject(createAstroError); - }, timeout); - function resolver() { - clearTimeout(timeoutEvent); - resolve(); - } - testFn(resolver); - }); -} - -function setup(args = []) { - const { stdout, stdin } = execa('../create-astro.mjs', args, { cwd: __dirname }); - return { - stdin, - stdout, - }; -} - describe('[create-astro] select directory', function () { this.timeout(timeout); it('should prompt for directory when none is provided', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup(); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { resolve(); } }); @@ -58,21 +24,21 @@ describe('[create-astro] select directory', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.nonEmptyDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { resolve(); } }); }); }); it('should proceed on an empty directory', async function () { - const resolvedEmptyDirPath = resolve(__dirname, inputs.emptyDir); + const resolvedEmptyDirPath = resolve(testDir, inputs.emptyDir); if (!existsSync(resolvedEmptyDirPath)) { await promises.mkdir(resolvedEmptyDirPath); } return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.emptyDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.template)) { + if (chunk.includes(PROMPT_MESSAGES.template)) { resolve(); } }); @@ -82,7 +48,7 @@ describe('[create-astro] select directory', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.nonexistentDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.template)) { + if (chunk.includes(PROMPT_MESSAGES.template)) { resolve(); } }); @@ -95,7 +61,7 @@ describe('[create-astro] select directory', function () { if (chunk.includes('Please clear contents or choose a different path.')) { resolve(); } - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { stdin.write(`${inputs.nonEmptyDir}\x0D`); } }); diff --git a/packages/create-astro/test/install-step.test.js b/packages/create-astro/test/install-step.test.js new file mode 100644 index 000000000..91e3bc66c --- /dev/null +++ b/packages/create-astro/test/install-step.test.js @@ -0,0 +1,71 @@ +import { setup, promiseWithTimeout, timeout, PROMPT_MESSAGES } from './utils.js'; +import { sep } from 'path'; +import fs from 'fs'; +import os from 'os'; + +const FAKE_PACKAGE_MANAGER = 'banana'; +let initialEnvValue = null; + +describe('[create-astro] install', function () { + this.timeout(timeout); + let tempDir = ''; + beforeEach(async () => { + tempDir = await fs.promises.mkdtemp(`${os.tmpdir()}${sep}`); + }); + this.beforeAll(() => { + initialEnvValue = process.env.npm_config_user_agent; + process.env.npm_config_user_agent = FAKE_PACKAGE_MANAGER; + }) + this.afterAll(() => { + process.env.npm_config_user_agent = initialEnvValue; + }) + + it('should respect package manager in prompt', function() { + const { stdout, stdin } = setup([tempDir, '--dryrun']); + return promiseWithTimeout((resolve) => { + const seen = new Set(); + const installPrompt = PROMPT_MESSAGES.install(FAKE_PACKAGE_MANAGER); + stdout.on('data', (chunk) => { + if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) { + seen.add(PROMPT_MESSAGES.template); + stdin.write('\x0D'); + } + if (!seen.has(PROMPT_MESSAGES.frameworks) && chunk.includes(PROMPT_MESSAGES.frameworks)) { + seen.add(PROMPT_MESSAGES.frameworks); + stdin.write('\x0D'); + } + + if (!seen.has(installPrompt) && chunk.includes(installPrompt)) { + seen.add(installPrompt); + resolve(); + } + }); + }); + }); + + it('should respect package manager in next steps', function() { + const { stdout, stdin } = setup([tempDir, '--dryrun']); + return promiseWithTimeout((resolve) => { + const seen = new Set(); + const installPrompt = PROMPT_MESSAGES.install(FAKE_PACKAGE_MANAGER); + stdout.on('data', (chunk) => { + if (!seen.has(PROMPT_MESSAGES.template) && chunk.includes(PROMPT_MESSAGES.template)) { + seen.add(PROMPT_MESSAGES.template); + stdin.write('\x0D'); + } + if (!seen.has(PROMPT_MESSAGES.frameworks) && chunk.includes(PROMPT_MESSAGES.frameworks)) { + seen.add(PROMPT_MESSAGES.frameworks); + stdin.write('\x0D'); + } + + if (!seen.has(installPrompt) && chunk.includes(installPrompt)) { + seen.add(installPrompt) + stdin.write('n\x0D'); + } + if (chunk.includes('banana dev')) { + resolve(); + } + }); + }); + }); +}) diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js new file mode 100644 index 000000000..c5519c7bf --- /dev/null +++ b/packages/create-astro/test/utils.js @@ -0,0 +1,40 @@ +import { execa } from 'execa' +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +export const testDir = dirname(__filename); +export const timeout = 5000; + +const createAstroError = new Error( + 'Timed out waiting for create-astro to respond with expected output.' +); + +export function promiseWithTimeout(testFn) { + return new Promise((resolve, reject) => { + const timeoutEvent = setTimeout(() => { + reject(createAstroError); + }, timeout); + function resolver() { + clearTimeout(timeoutEvent); + resolve(); + } + testFn(resolver); + }); +} + +export const PROMPT_MESSAGES = { + directory: 'Where would you like to create your app?', + template: 'Which app template would you like to use?', + // TODO: remove when framework selector is removed + frameworks: 'Which frameworks would you like to use?', + install: (pkgManager) => `Would you like us to run "${pkgManager} install?"`, +}; + +export function setup(args = []) { + const { stdout, stdin } = execa('../create-astro.mjs', args, { cwd: testDir }); + return { + stdin, + stdout, + }; +} |