2023-02-06 16:19:37 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
|
|
|
|
import { projectName } from '../dist/index.js';
|
|
|
|
import { setup } from './utils.js';
|
|
|
|
|
|
|
|
describe('project name', () => {
|
|
|
|
const fixture = setup();
|
|
|
|
|
|
|
|
it('pass in name', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: './foo/bar/baz', prompt: () => {} };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.cwd).to.eq('./foo/bar/baz');
|
|
|
|
expect(context.projectName).to.eq('baz');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('dot', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: '.', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(fixture.hasMessage('"." is not empty!')).to.be.true;
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('dot slash', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: './', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(fixture.hasMessage('"./" is not empty!')).to.be.true;
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('empty', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/empty',
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(fixture.hasMessage('"./test/fixtures/empty" is not empty!')).to.be.false;
|
|
|
|
expect(context.projectName).to.eq('empty');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('not empty', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/not-empty',
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(fixture.hasMessage('"./test/fixtures/not-empty" is not empty!')).to.be.true;
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('basic', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.cwd).to.eq('foobar');
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
2023-09-06 13:22:18 +00:00
|
|
|
it('blank space', async () => {
|
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'foobar ' }) };
|
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.cwd).to.eq('foobar');
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
|
|
|
});
|
|
|
|
|
2023-02-06 16:19:37 +00:00
|
|
|
it('normalize', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: 'Invalid Name' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.cwd).to.eq('Invalid Name');
|
|
|
|
expect(context.projectName).to.eq('invalid-name');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('remove leading/trailing dashes', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: '(invalid)' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.projectName).to.eq('invalid');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-02-06 16:19:37 +00:00
|
|
|
|
|
|
|
it('handles scoped packages', async () => {
|
2023-02-06 16:21:48 +00:00
|
|
|
const context = { projectName: '', cwd: '', prompt: () => ({ name: '@astro/site' }) };
|
2023-02-06 16:19:37 +00:00
|
|
|
await projectName(context);
|
|
|
|
|
|
|
|
expect(context.cwd).to.eq('@astro/site');
|
|
|
|
expect(context.projectName).to.eq('@astro/site');
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|
2023-09-19 09:09:27 +00:00
|
|
|
|
|
|
|
it('--yes', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './foo/bar/baz',
|
|
|
|
yes: true,
|
|
|
|
prompt: () => {},
|
|
|
|
};
|
|
|
|
await projectName(context);
|
|
|
|
expect(context.projectName).to.eq('baz');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with name', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './foo/bar/baz',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => {},
|
|
|
|
};
|
|
|
|
await projectName(context);
|
|
|
|
expect(context.projectName).to.eq('baz');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with dot', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: '.',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
|
|
|
await projectName(context);
|
|
|
|
expect(context.projectName).to.eq('foobar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dry run with empty', async () => {
|
|
|
|
const context = {
|
|
|
|
projectName: '',
|
|
|
|
cwd: './test/fixtures/empty',
|
|
|
|
dryRun: true,
|
|
|
|
prompt: () => ({ name: 'foobar' }),
|
|
|
|
};
|
|
|
|
await projectName(context);
|
|
|
|
expect(context.projectName).to.eq('empty');
|
|
|
|
});
|
2023-02-06 16:21:48 +00:00
|
|
|
});
|