summaryrefslogtreecommitdiff
path: root/packages/create-astro/test/directory-step.test.js
blob: 0031f97fd41e78c477eb974afeaeccc64f09dc27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import path 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',
	nonEmptySafeDir: './fixtures/select-directory/nonempty-safe-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 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();
				}
			});
		});
	});
	it('should proceed on an empty directory', async function () {
		const resolvedEmptyDirPath = path.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('is not empty!')) {
					resolve();
				}
				if (chunk.includes(PROMPT_MESSAGES.directory)) {
					stdin.write(`${inputs.nonEmptyDir}\x0D`);
				}
			});
		});
	});
});