From f31a530c4246f2357777962315b10980e0f7cf0e Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 9 Feb 2025 12:19:10 +0100 Subject: [PATCH] cli/new: rename executePluginPackageTemplate + add tests Signed-off-by: Patrik Oldsberg --- .../executePluginPackageTemplate.test.ts | 147 ------------------ .../new/execution/executePortableTemplate.ts | 4 +- .../execution/writeTemplateContents.test.ts | 111 +++++++++++++ ...geTemplate.ts => writeTemplateContents.ts} | 2 +- 4 files changed, 114 insertions(+), 150 deletions(-) delete mode 100644 packages/cli/src/lib/new/execution/executePluginPackageTemplate.test.ts create mode 100644 packages/cli/src/lib/new/execution/writeTemplateContents.test.ts rename packages/cli/src/lib/new/execution/{executePluginPackageTemplate.ts => writeTemplateContents.ts} (97%) diff --git a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.test.ts b/packages/cli/src/lib/new/execution/executePluginPackageTemplate.test.ts deleted file mode 100644 index 79c9fa9483..0000000000 --- a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.test.ts +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2021 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 fs from 'fs-extra'; -import { mockPaths } from './testUtils'; -import { - executePluginPackageTemplate, - templatingTask, -} from './executePluginPackageTemplate'; -import { createMockDirectory } from '@backstage/backend-test-utils'; - -describe('executePluginPackageTemplate', () => { - const mockDir = createMockDirectory(); - mockPaths({ - ownDir: mockDir.resolve('own'), - targetRoot: mockDir.resolve('root'), - }); - afterEach(() => { - jest.resetAllMocks(); - }); - - it('should execute template', async () => { - mockDir.setContent({ - root: { - 'yarn.lock': ` -some-package@^1.1.0: - version "1.5.0" -`, - }, - own: { - 'test-template': { - 'package.json.hbs': ` -{ - "name": "my-{{id}}-plugin", - {{#if makePrivate}} - "private": true, - {{/if}} - "description": "testing", - "dependencies": { - "some-package": "{{ versionQuery 'some-package' '1.3.0' }}", - "other-package": "{{ versionQuery 'other-package' '2.3.0' }}" - } -} -`, - subdir: { - 'templated.txt.hbs': 'Hello {{id}}!', - 'not-templated.txt': 'Hello {{id}}!', - }, - }, - }, - }); - - let modified = false; - await executePluginPackageTemplate( - { - isMonoRepo: false, - createTemporaryDirectory: (name: string) => fs.mkdtemp(name), - markAsModified: () => { - modified = true; - }, - }, - { - templateDir: mockDir.resolve('own', 'test-template'), - targetDir: mockDir.resolve('target'), - values: { - id: 'testing', - }, - }, - ); - - expect(modified).toBe(true); - - await expect(fs.readFile(mockDir.resolve('target/package.json'), 'utf8')) - .resolves.toBe(`{ - "name": "my-testing-plugin", - "description": "testing", - "dependencies": { - "some-package": "^1.1.0", - "other-package": "^2.3.0" - } -} -`); - await expect( - fs.readFile(mockDir.resolve('target/subdir/templated.txt'), 'utf8'), - ).resolves.toBe('Hello testing!'); - await expect( - fs.readFile(mockDir.resolve('target/subdir/not-templated.txt'), 'utf8'), - ).resolves.toBe('Hello {{id}}!'); - }); -}); - -describe('templatingTask', () => { - const mockDir = createMockDirectory(); - - it('should template a directory with mix of regular files and templates', async () => { - // Testing template directory - const tmplDir = 'test-tmpl'; - - // Temporary dest dir to write the template to - const destDir = 'test-dest'; - - // Files content - const testFileContent = 'testing'; - const testVersionFileContent = - "version: {{pluginVersion}} {{versionQuery 'mock-pkg'}}"; - - mockDir.setContent({ - [tmplDir]: { - sub: { - 'version.txt.hbs': testVersionFileContent, - }, - 'test.txt': testFileContent, - }, - [destDir]: {}, - }); - - await templatingTask( - mockDir.resolve(tmplDir), - mockDir.resolve(destDir), - { - pluginVersion: '0.0.0', - }, - () => '^0.1.2', - true, - ); - - await expect( - fs.readFile(mockDir.resolve(destDir, 'test.txt'), 'utf8'), - ).resolves.toBe(testFileContent); - await expect( - fs.readFile(mockDir.resolve(destDir, 'sub/version.txt'), 'utf8'), - ).resolves.toBe('version: 0.0.0 ^0.1.2'); - }); -}); diff --git a/packages/cli/src/lib/new/execution/executePortableTemplate.ts b/packages/cli/src/lib/new/execution/executePortableTemplate.ts index 5d93afbb34..746c299586 100644 --- a/packages/cli/src/lib/new/execution/executePortableTemplate.ts +++ b/packages/cli/src/lib/new/execution/executePortableTemplate.ts @@ -23,7 +23,7 @@ import { PortableTemplateInput, } from '../types'; import { runAdditionalActions } from './additionalActions'; -import { executePluginPackageTemplate } from './executePluginPackageTemplate'; +import { writeTemplateContents } from './writeTemplateContents'; type ExecuteNewTemplateOptions = { config: PortableTemplateConfig; @@ -38,7 +38,7 @@ export async function executePortableTemplate( let modified = false; try { - const { targetDir } = await executePluginPackageTemplate(template, input); + const { targetDir } = await writeTemplateContents(template, input); modified = true; diff --git a/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts b/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts new file mode 100644 index 0000000000..f9df3f72cd --- /dev/null +++ b/packages/cli/src/lib/new/execution/writeTemplateContents.test.ts @@ -0,0 +1,111 @@ +/* + * Copyright 2021 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 { relative as relativePath } from 'node:path'; +import { writeTemplateContents } from './writeTemplateContents'; +import { createMockDirectory } from '@backstage/backend-test-utils'; +import { paths } from '../../paths'; + +const mockGlobals = { + baseVersion: '0.1.0', + license: 'Apache-2.0', + private: true, + packagePrefix: '@internal/', + pluginInfix: 'plugin-', +}; + +describe('writeTemplateContents', () => { + const mockDir = createMockDirectory(); + + beforeEach(() => { + mockDir.clear(); + jest.resetAllMocks(); + jest + .spyOn(paths, 'resolveTargetRoot') + .mockImplementation((...args) => mockDir.resolve(...args)); + }); + + it('should write an empty template', async () => { + const { targetDir } = await writeTemplateContents( + { + id: 'test', + files: [], + role: 'frontend-plugin', + targetPath: 'plugins/plugin-test', + templateValues: {}, + }, + { + roleParams: { role: 'frontend-plugin', pluginId: 'test' }, + globals: mockGlobals, + builtInParams: {}, + packageParams: { + packageName: '@internal/plugin-test', + packagePath: 'plugins/plugin-test', + }, + params: {}, + }, + ); + + expect(relativePath(mockDir.path, targetDir)).toBe('plugins/plugin-test'); + expect(mockDir.content()).toEqual({}); + }); + + it('should write template with various files', async () => { + await writeTemplateContents( + { + id: 'test', + files: [ + { + content: 'test', + path: 'test.txt', + }, + { + content: 'id={{ pluginId}}', + path: 'plugin.txt', + syntax: 'handlebars', + }, + { + content: '{"x":1}', + path: 'test.json', + }, + ], + role: 'frontend-plugin', + targetPath: 'plugins/plugin-test', + templateValues: {}, + }, + { + roleParams: { role: 'frontend-plugin', pluginId: 'test' }, + globals: mockGlobals, + builtInParams: {}, + packageParams: { + packageName: '@internal/plugin-test', + packagePath: 'out', + }, + params: { + pluginId: 'test', + }, + }, + ); + + expect(mockDir.content()).toEqual({ + out: { + 'test.txt': 'test', + 'plugin.txt': 'id=test', + 'test.json': '{\n "x": 1\n}', + }, + }); + }); +}); diff --git a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts b/packages/cli/src/lib/new/execution/writeTemplateContents.ts similarity index 97% rename from packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts rename to packages/cli/src/lib/new/execution/writeTemplateContents.ts index d9541280ed..e380183958 100644 --- a/packages/cli/src/lib/new/execution/executePluginPackageTemplate.ts +++ b/packages/cli/src/lib/new/execution/writeTemplateContents.ts @@ -23,7 +23,7 @@ import { ForwardedError, InputError } from '@backstage/errors'; import { isMonoRepo as getIsMonoRepo } from '@backstage/cli-node'; import { PortableTemplater } from './PortableTemplater'; -export async function executePluginPackageTemplate( +export async function writeTemplateContents( template: PortableTemplate, input: PortableTemplateInput, ): Promise<{ targetDir: string }> {