2022-04-21 20:38:07 +00:00
|
|
|
import { execa } from 'execa';
|
2022-04-21 20:36:48 +00:00
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname, resolve } from 'path';
|
2022-04-21 20:38:07 +00:00
|
|
|
import { promises, existsSync } from 'fs';
|
2022-04-21 20:36:48 +00:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
|
2022-04-21 20:38:07 +00:00
|
|
|
const createAstroError = new Error(
|
|
|
|
'Timed out waiting for create-astro to respond with expected output.'
|
|
|
|
);
|
2022-04-21 20:36:48 +00:00
|
|
|
const timeout = 5000;
|
|
|
|
|
|
|
|
const instructions = {
|
|
|
|
directory: 'Where would you like to create your app?',
|
2022-04-21 20:38:07 +00:00
|
|
|
template: 'Which app template would you like to use?',
|
2022-04-21 20:36:48 +00:00
|
|
|
};
|
|
|
|
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);
|
2022-04-21 20:38:07 +00:00
|
|
|
});
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup(args = []) {
|
2022-04-21 20:38:07 +00:00
|
|
|
const { stdout, stdin } = execa('../create-astro.mjs', args, { cwd: __dirname });
|
2022-04-21 20:36:48 +00:00
|
|
|
return {
|
|
|
|
stdin,
|
|
|
|
stdout,
|
2022-04-21 20:38:07 +00:00
|
|
|
};
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
|
|
|
|
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-21 20:36:48 +00:00
|
|
|
if (chunk.includes(instructions.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-21 20:36:48 +00:00
|
|
|
if (chunk.includes(instructions.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 proceed on an empty directory', async function () {
|
|
|
|
const resolvedEmptyDirPath = resolve(__dirname, 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-21 20:36:48 +00:00
|
|
|
if (chunk.includes(instructions.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-21 20:36:48 +00:00
|
|
|
if (chunk.includes(instructions.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-21 20:36:48 +00:00
|
|
|
if (chunk.includes('Please clear contents or choose a different path.')) {
|
2022-04-21 20:38:07 +00:00
|
|
|
resolve();
|
2022-04-21 20:36:48 +00:00
|
|
|
}
|
|
|
|
if (chunk.includes(instructions.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
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|