0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/create-astro/test/directory-step.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.1 KiB
JavaScript
Raw Normal View History

import path from 'path';
2022-04-21 15:38:07 -05:00
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',
};
2022-04-21 15:38:07 -05:00
describe('[create-astro] select directory', function () {
this.timeout(timeout);
2022-04-21 15:38:07 -05:00
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)) {
2022-04-21 15:38:07 -05:00
resolve();
}
2022-04-21 15:38:07 -05:00
});
});
});
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)) {
2022-04-21 15:38:07 -05:00
resolve();
}
2022-04-21 15:38:07 -05:00
});
});
});
it('should proceed on an empty directory', async function () {
const resolvedEmptyDirPath = path.resolve(testDir, inputs.emptyDir);
if (!existsSync(resolvedEmptyDirPath)) {
2022-04-21 15:38:07 -05:00
await promises.mkdir(resolvedEmptyDirPath);
}
2022-04-21 15:38:07 -05:00
return promiseWithTimeout((resolve) => {
const { stdout } = setup([inputs.emptyDir]);
stdout.on('data', (chunk) => {
if (chunk.includes(PROMPT_MESSAGES.template)) {
2022-04-21 15:38:07 -05:00
resolve();
}
2022-04-21 15:38:07 -05:00
});
});
});
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)) {
2022-04-21 15:38:07 -05:00
resolve();
}
2022-04-21 15:38:07 -05:00
});
});
});
it('should error on bad directory selection in prompt', function () {
return promiseWithTimeout((resolve) => {
const { stdout, stdin } = setup();
stdout.on('data', (chunk) => {
if (chunk.includes('is not empty!')) {
2022-04-21 15:38:07 -05:00
resolve();
}
if (chunk.includes(PROMPT_MESSAGES.directory)) {
2022-04-21 15:38:07 -05:00
stdin.write(`${inputs.nonEmptyDir}\x0D`);
}
2022-04-21 15:38:07 -05:00
});
});
});
});