astro/packages/create-astro/test/directory-step.test.js
Ben Holmes 38e5e9e982
Feat: create astro add install step (#3190)
* feat: add instlal step with pkg manager detection

* feat: add package emoji for style points

* feat: update next steps to match pkg manager

* refactor: extract some create-astro test utils

* refactor: extract promp msgs to utils

* chore: add install step tests

* chore: changeset

* fix: remove directory test skip

* fix: unset env variables after install step test

* deps: add execa to create-astro

* refactor: use execa for install step

* chore: remove old comment

* fix: rework install step test for node 14?

* chore: remove "politely stolen" footnote

* temp: show stdout dialog

* feat: remove debugging logs, add dryrun flag for testing

* chore: more stray logs

* fix: remove rmdir
2022-04-26 11:24:24 -04:00

70 lines
2.1 KiB
JavaScript

import { resolve } from 'path';
import { promises, existsSync } from 'fs';
import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js';
const inputs = {
nonEmptyDir: './fixtures/select-directory/nonempty-dir',
emptyDir: './fixtures/select-directory/empty-dir',
nonexistentDir: './fixtures/select-directory/banana-dir',
};
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(PROMPT_MESSAGES.directory)) {
resolve();
}
});
});
});
it('should NOT proceed on a non-empty directory', function () {
return promiseWithTimeout((resolve) => {
const { stdout } = setup([inputs.nonEmptyDir]);
stdout.on('data', (chunk) => {
if (chunk.includes(PROMPT_MESSAGES.directory)) {
resolve();
}
});
});
});
it('should proceed on an empty directory', async function () {
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(PROMPT_MESSAGES.template)) {
resolve();
}
});
});
});
it('should proceed when directory does not exist', function () {
return promiseWithTimeout((resolve) => {
const { stdout } = setup([inputs.nonexistentDir]);
stdout.on('data', (chunk) => {
if (chunk.includes(PROMPT_MESSAGES.template)) {
resolve();
}
});
});
});
it('should error on bad directory selection in prompt', function () {
return promiseWithTimeout((resolve) => {
const { stdout, stdin } = setup();
stdout.on('data', (chunk) => {
if (chunk.includes('Please clear contents or choose a different path.')) {
resolve();
}
if (chunk.includes(PROMPT_MESSAGES.directory)) {
stdin.write(`${inputs.nonEmptyDir}\x0D`);
}
});
});
});
});