From ae502294d7dc69d0f7f87c5d379498a76bacf0d4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Feb 2025 13:22:52 +0100 Subject: [PATCH] cli/new: add template loader selection tests Signed-off-by: Patrik Oldsberg --- .../lib/new/loader/NewTemplateLoader.test.ts | 86 +++++++++++++++++++ .../cli/src/lib/new/templateSelector.test.ts | 17 ---- 2 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 packages/cli/src/lib/new/loader/NewTemplateLoader.test.ts diff --git a/packages/cli/src/lib/new/loader/NewTemplateLoader.test.ts b/packages/cli/src/lib/new/loader/NewTemplateLoader.test.ts new file mode 100644 index 0000000000..e63de999a6 --- /dev/null +++ b/packages/cli/src/lib/new/loader/NewTemplateLoader.test.ts @@ -0,0 +1,86 @@ +/* + * Copyright 2025 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { NewTemplateLoader } from './NewTemplateLoader'; +import { NewConfig } from '../config/types'; +import inquirer from 'inquirer'; +import { withLogCollector } from '@backstage/test-utils'; + +describe('NewTemplateLoader.selectTemplateInteractively', () => { + const mockConfig: NewConfig = { + isUsingDefaultTemplates: false, + templatePointers: [ + { id: 'template1', target: 'path/to/template1' }, + { id: 'template2', target: 'path/to/template2' }, + ], + globals: {}, + }; + + it('should select a template interactively', async () => { + jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ id: 'template1' }); + + const result = await NewTemplateLoader.selectTemplateInteractively( + mockConfig, + ); + + expect(result).toEqual({ id: 'template1', target: 'path/to/template1' }); + }); + + it('should error if interactive selections is not found', async () => { + jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ id: 'nonexistent' }); + + await expect( + NewTemplateLoader.selectTemplateInteractively(mockConfig), + ).rejects.toThrow("Template 'nonexistent' not found"); + }); + + it('should use preselected template id', async () => { + const result = await NewTemplateLoader.selectTemplateInteractively( + mockConfig, + 'template2', + ); + + expect(result).toEqual({ id: 'template2', target: 'path/to/template2' }); + }); + + it('should throw an error if template is not found', async () => { + await expect( + NewTemplateLoader.selectTemplateInteractively(mockConfig, 'nonexistent'), + ).rejects.toThrow("Template 'nonexistent' not found"); + }); + + it('should rewrite plugin to frontend-plugin if default templates are used', async () => { + await expect( + NewTemplateLoader.selectTemplateInteractively(mockConfig, 'plugin'), + ).rejects.toThrow("Template 'plugin' not found"); + + const logs = await withLogCollector(async () => { + await expect( + NewTemplateLoader.selectTemplateInteractively( + { ...mockConfig, isUsingDefaultTemplates: true }, + 'plugin', + ), + ).rejects.toThrow("Template 'frontend-plugin' not found"); + }); + expect(logs).toEqual({ + log: [], + warn: [ + "DEPRECATION WARNING: The 'plugin' template is deprecated, use 'frontend-plugin' instead", + ], + error: [], + }); + }); +}); diff --git a/packages/cli/src/lib/new/templateSelector.test.ts b/packages/cli/src/lib/new/templateSelector.test.ts index 0bc4a00a9d..d8adc074ec 100644 --- a/packages/cli/src/lib/new/templateSelector.test.ts +++ b/packages/cli/src/lib/new/templateSelector.test.ts @@ -16,23 +16,6 @@ import { readCliConfig, verifyTemplate } from './templateSelector'; import { createMockDirectory } from '@backstage/backend-test-utils'; -describe('readCliConfig', () => { - it('omits default templates if cli defaults are set to false', () => { - const { templates } = readCliConfig({ defaults: false }); - expect(templates).toEqual([]); - }); - - it('returns default templates if cli config does not exist in pkg json', () => { - const { templates } = readCliConfig(undefined); - expect(templates.length).toBeGreaterThan(0); - }); - - it('returns default templates if cli defaults are set to true', () => { - const { templates } = readCliConfig({ defaults: true }); - expect(templates.length).toBeGreaterThan(0); - }); -}); - describe('verifyTemplate', () => { it('throws an error if template target is a remote URL', () => { expect(() => verifyTemplate({ id: '', target: 'http' })).toThrow(