2024-02-13 09:41:59 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { describe, it } from 'node:test';
|
2023-02-06 11:19:37 -05:00
|
|
|
import { projectName } from '../dist/index.js';
|
|
|
|
import { setup } from './utils.js';
|
|
|
|
|
2023-10-23 17:14:33 -05:00
|
|
|
describe('project name', async () => {
|
2023-02-06 11:19:37 -05:00
|
|
|
const fixture = setup();
|
|
|
|
|
|
|
|
it('pass in name', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: './foo/bar/baz', prompt: () => {} };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.cwd, './foo/bar/baz');
|
|
|
|
assert.equal(context.projectName, 'baz');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('dot', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.ok(fixture.hasMessage('"." is not empty!'));
|
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('dot slash', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.ok(fixture.hasMessage('"./" is not empty!'));
|
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('empty', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/empty',
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.ok(!fixture.hasMessage('"./test/fixtures/empty" is not empty!'));
|
|
|
|
assert.equal(context.projectName, 'empty');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('not empty', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/not-empty',
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.ok(fixture.hasMessage('"./test/fixtures/not-empty" is not empty!'));
|
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('basic', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.cwd, 'foobar');
|
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
2023-09-06 08:22:18 -05:00
|
|
|
it('blank space', async () => {
|
2024-02-13 09:41:59 -05:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) };
|
2023-09-06 08:22:18 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.cwd, 'foobar');
|
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-09-06 08:22:18 -05:00
|
|
|
});
|
|
|
|
|
2023-02-06 11:19:37 -05:00
|
|
|
it('normalize', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'Invalid Name' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.cwd, 'Invalid Name');
|
|
|
|
assert.equal(context.projectName, 'invalid-name');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('remove leading/trailing dashes', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: '(invalid)' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.projectName, 'invalid');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-02-06 11:19:37 -05:00
|
|
|
|
|
|
|
it('handles scoped packages', async () => {
|
2023-02-06 11:21:48 -05:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: '@astro/site' }) };
|
2023-02-06 11:19:37 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.cwd, '@astro/site');
|
|
|
|
assert.equal(context.projectName, '@astro/site');
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|
2023-09-19 04:09:27 -05:00
|
|
|
|
|
|
|
it('--yes', async () => {
|
2024-02-13 09:41:59 -05:00
|
|
|
const context = { projectName: '', cwd: './foo/bar/baz', yes: true, prompt: () => {} };
|
2023-09-19 04:09:27 -05:00
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.projectName, 'baz');
|
2023-09-19 04:09:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with name', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './foo/bar/baz',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => {},
|
|
|
|
};
|
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.projectName, 'baz');
|
2023-09-19 04:09:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with dot', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: '.',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.projectName, 'foobar');
|
2023-09-19 04:09:27 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with empty', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/empty',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
|
|
|
await projectName(context);
|
2024-02-13 09:41:59 -05:00
|
|
|
assert.equal(context.projectName, 'empty');
|
2023-09-19 04:09:27 -05:00
|
|
|
});
|
2023-02-06 11:21:48 -05:00
|
|
|
});
|