2022-06-22 15:59:49 +00:00
|
|
|
import path from 'path';
|
2022-04-21 20:38:07 +00:00
|
|
|
import { promises, existsSync } from 'fs';
|
2022-04-26 15:24:24 +00:00
|
|
|
import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js';
|
2022-04-21 20:36:48 +00:00
|
|
|
|
|
|
|
const inputs = {
|
|
|
|
nonEmptyDir: './fixtures/select-directory/nonempty-dir',
|
2022-09-20 12:30:13 +00:00
|
|
|
nonEmptySafeDir: './fixtures/select-directory/nonempty-safe-dir',
|
2022-04-21 20:36:48 +00:00
|
|
|
emptyDir: './fixtures/select-directory/empty-dir',
|
|
|
|
nonexistentDir: './fixtures/select-directory/banana-dir',
|
|
|
|
};
|
|
|
|
|
2022-04-21 20:38:07 +00:00
|
|
|
describe('[create-astro] select directory', function () {
|
2022-04-21 20:36:48 +00:00
|
|
|
this.timeout(timeout);
|
2022-04-21 20:38:07 +00:00
|
|
|
it('should prompt for directory when none is provided', function () {
|
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout } = setup();
|
|
|
|
stdout.on('data', (chunk) => {
|
2022-04-26 15:24:24 +00:00
|
|
|
if (chunk.includes(PROMPT_MESSAGES.directory)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should NOT proceed on a non-empty directory', function () {
|
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout } = setup([inputs.nonEmptyDir]);
|
|
|
|
stdout.on('data', (chunk) => {
|
2022-04-26 15:24:24 +00:00
|
|
|
if (chunk.includes(PROMPT_MESSAGES.directory)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-09-20 12:30:13 +00:00
|
|
|
it('should proceed on a non-empty safe directory', function () {
|
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout } = setup([inputs.nonEmptySafeDir]);
|
|
|
|
stdout.on('data', (chunk) => {
|
|
|
|
if (chunk.includes(PROMPT_MESSAGES.template)) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-04-21 20:38:07 +00:00
|
|
|
it('should proceed on an empty directory', async function () {
|
2022-06-22 15:59:49 +00:00
|
|
|
const resolvedEmptyDirPath = path.resolve(testDir, inputs.emptyDir);
|
2022-04-21 20:36:48 +00:00
|
|
|
if (!existsSync(resolvedEmptyDirPath)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
await promises.mkdir(resolvedEmptyDirPath);
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout } = setup([inputs.emptyDir]);
|
|
|
|
stdout.on('data', (chunk) => {
|
2022-04-26 15:24:24 +00:00
|
|
|
if (chunk.includes(PROMPT_MESSAGES.template)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should proceed when directory does not exist', function () {
|
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout } = setup([inputs.nonexistentDir]);
|
|
|
|
stdout.on('data', (chunk) => {
|
2022-04-26 15:24:24 +00:00
|
|
|
if (chunk.includes(PROMPT_MESSAGES.template)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should error on bad directory selection in prompt', function () {
|
|
|
|
return promiseWithTimeout((resolve) => {
|
|
|
|
const { stdout, stdin } = setup();
|
|
|
|
stdout.on('data', (chunk) => {
|
2022-04-27 00:38:31 +00:00
|
|
|
if (chunk.includes('is not empty!')) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-26 15:24:24 +00:00
|
|
|
if (chunk.includes(PROMPT_MESSAGES.directory)) {
|
2022-04-21 20:38:07 +00:00
|
|
|
stdin.write(`${inputs.nonEmptyDir}\x0D`);
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|