Write tests for populateOptions and createDirName

Signed-off-by: Min Kim <minkimcello@gmail.com>
This commit is contained in:
Min Kim
2024-12-21 14:01:12 -05:00
parent 1fb45bdb55
commit 7d6d32eee4
4 changed files with 102 additions and 9 deletions
+2 -2
View File
@@ -47,8 +47,8 @@ export default async () => {
const pkgJson = await fs.readJson(paths.resolveTargetRoot('package.json'));
const cliConfig = pkgJson.backstage?.cli;
const { templates, globals } = await readCliConfig(cliConfig);
const template = await verifyTemplate(await templateSelector(templates));
const { templates, globals } = readCliConfig(cliConfig);
const template = verifyTemplate(await templateSelector(templates));
const codeOwnersFilePath = await getCodeownersFilePath(paths.targetRoot);
+2 -5
View File
@@ -23,7 +23,7 @@ import { Template, TemplateLocation } from './types';
import defaultTemplates from '../../../templates/all-default-templates';
export async function readCliConfig(
export function readCliConfig(
cliConfig:
| {
defaults?: boolean;
@@ -72,10 +72,7 @@ export async function templateSelector(
return answer.name;
}
export async function verifyTemplate({
id,
target,
}: TemplateLocation): Promise<Template> {
export function verifyTemplate({ id, target }: TemplateLocation): Template {
if (target.startsWith('http')) {
throw new Error('Remote templates are not supported yet');
}
+97 -1
View File
@@ -13,7 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { resolvePackageName } from './utils';
import {
resolvePackageName,
populateOptions,
createDirName,
Options,
} from './utils';
import { Template } from './types';
describe('resolvePackageName', () => {
it('should generate correct name without scope', () => {
@@ -76,3 +82,93 @@ describe('resolvePackageName', () => {
).toEqual('@custom/myapp.test');
});
});
describe('populateOptions', () => {
it('should return default values if not provided', async () => {
expect(
await populateOptions({}, { targetPath: '/example' } as Template),
).toEqual({
id: '',
private: false,
baseVersion: '0.0.0',
owner: '',
license: 'Apache-2.0',
targetPath: '/example',
scope: '',
moduleId: '',
});
});
it('should include all non-standard global and prompt values', async () => {
expect(
await populateOptions({ foo: 'bar' }, {
targetPath: '/example',
} as Template),
).toEqual({
id: '',
private: false,
baseVersion: '0.0.0',
owner: '',
license: 'Apache-2.0',
targetPath: '/example',
scope: '',
moduleId: '',
foo: 'bar',
});
});
it('should priority global targetPath over the targetPath specified in template', async () => {
expect(
await populateOptions({ targetPath: '/global' }, {
targetPath: '/example',
} as Template),
).toEqual({
id: '',
private: false,
baseVersion: '0.0.0',
owner: '',
license: 'Apache-2.0',
targetPath: '/global',
scope: '',
moduleId: '',
});
});
});
describe('createDirName', () => {
it('should return name in the backend-module format if backendModulePrefix is set to true', () => {
expect(
createDirName(
{ backendModulePrefix: true } as Template,
{
id: 'foo',
moduleId: 'bar',
} as Options,
),
).toEqual('foo-backend-module-bar');
});
it('should throw an error if backendModulePrefix is configured as true but is missing moduleId', () => {
expect(() =>
createDirName(
{ backendModulePrefix: true } as Template,
{
id: 'foo',
moduleId: '',
} as Options,
),
).toThrow('backendModulePrefix requires moduleId prompt');
});
it('should append the suffix value if one is provided', () => {
expect(
createDirName({ suffix: 'foo' } as Template, { id: 'bar' } as Options),
).toEqual('bar-foo');
});
it('should return id if neither backendModulePrefix nor suffix is specified', () => {
expect(createDirName({} as Template, { id: 'foo' } as Options)).toEqual(
'foo',
);
});
});
+1 -1
View File
@@ -17,7 +17,7 @@ import fs from 'fs-extra';
import { paths } from '../paths';
import { Template } from './types';
interface Options extends Record<string, string | boolean> {
export interface Options extends Record<string, string | boolean> {
id: string;
private: boolean;
baseVersion: string;