summaryrefslogtreecommitdiff
path: root/packages/create-astro/test/project-name.test.js
blob: 0aebd1c79268a58481e4d06dc1b60fccf8dd5a05 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { projectName } from '../dist/index.js';
import { setup } from './utils.js';

describe('project name', async () => {
	const fixture = setup();

	it('pass in name', async () => {
		const context = { projectName: '', cwd: './foo/bar/baz', prompt: () => {} };
		await projectName(context);
		assert.equal(context.cwd, './foo/bar/baz');
		assert.equal(context.projectName, 'baz');
	});

	it('dot', async () => {
		const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) };
		await projectName(context);
		assert.ok(fixture.hasMessage('"." is not empty!'));
		assert.equal(context.projectName, 'foobar');
	});

	it('dot slash', async () => {
		const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) };
		await projectName(context);
		assert.ok(fixture.hasMessage('"./" is not empty!'));
		assert.equal(context.projectName, 'foobar');
	});

	it('empty', async () => {
		const context = {
			projectName: '',
			cwd: './test/fixtures/empty',
			prompt: () => ({ name: 'foobar' }),
		};
		await projectName(context);
		assert.ok(!fixture.hasMessage('"./test/fixtures/empty" is not empty!'));
		assert.equal(context.projectName, 'empty');
	});

	it('not empty', async () => {
		const context = {
			projectName: '',
			cwd: './test/fixtures/not-empty',
			prompt: () => ({ name: 'foobar' }),
		};
		await projectName(context);
		assert.ok(fixture.hasMessage('"./test/fixtures/not-empty" is not empty!'));
		assert.equal(context.projectName, 'foobar');
	});

	it('basic', async () => {
		const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) };
		await projectName(context);
		assert.equal(context.cwd, 'foobar');
		assert.equal(context.projectName, 'foobar');
	});

	it('head and tail blank spaces should be trimmed', async () => {
		const context = { projectName: '', cwd: '', prompt: () => ({ name: '  foobar  ' }) };
		await projectName(context);
		assert.equal(context.cwd, 'foobar');
		assert.equal(context.projectName, 'foobar');
	});

	it('normalize', async () => {
		const context = { projectName: '', cwd: '', prompt: () => ({ name: 'Invalid Name' }) };
		await projectName(context);
		assert.equal(context.cwd, 'Invalid Name');
		assert.equal(context.projectName, 'invalid-name');
	});

	it('remove leading/trailing dashes', async () => {
		const context = { projectName: '', cwd: '', prompt: () => ({ name: '(invalid)' }) };
		await projectName(context);
		assert.equal(context.projectName, 'invalid');
	});

	it('handles scoped packages', async () => {
		const context = { projectName: '', cwd: '', prompt: () => ({ name: '@astro/site' }) };
		await projectName(context);
		assert.equal(context.cwd, '@astro/site');
		assert.equal(context.projectName, '@astro/site');
	});

	it('--yes', async () => {
		const context = { projectName: '', cwd: './foo/bar/baz', yes: true, prompt: () => {} };
		await projectName(context);
		assert.equal(context.projectName, 'baz');
	});

	it('dry run with name', async () => {
		const context = {
			projectName: '',
			cwd: './foo/bar/baz',
			dryRun: true,
			prompt: () => {},
		};
		await projectName(context);
		assert.equal(context.projectName, 'baz');
	});

	it('dry run with dot', async () => {
		const context = {
			projectName: '',
			cwd: '.',
			dryRun: true,
			prompt: () => ({ name: 'foobar' }),
		};
		await projectName(context);
		assert.equal(context.projectName, 'foobar');
	});

	it('dry run with empty', async () => {
		const context = {
			projectName: '',
			cwd: './test/fixtures/empty',
			dryRun: true,
			prompt: () => ({ name: 'foobar' }),
		};
		await projectName(context);
		assert.equal(context.projectName, 'empty');
	});
});