From 23c58be5254d98edb1a08be3d492ff073bc27a3b Mon Sep 17 00:00:00 2001 From: Ivan Shmidt Date: Wed, 17 Jun 2020 20:49:14 +0200 Subject: [PATCH] test(scaffolder): add nested template test --- .../template-1/expected_file.ts | 0 .../test-nested-template/template.yaml | 0 .../test-simple-template/expected_file.ts | 0 .../test-simple-template/template.yaml | 0 .../src/scaffolder/prepare/file.test.ts | 55 ++++++++++++------- .../src/scaffolder/prepare/file.ts | 7 ++- 6 files changed, 38 insertions(+), 24 deletions(-) rename plugins/scaffolder-backend/{test => fixtures}/test-nested-template/template-1/expected_file.ts (100%) rename plugins/scaffolder-backend/{test => fixtures}/test-nested-template/template.yaml (100%) rename plugins/scaffolder-backend/{test => fixtures}/test-simple-template/expected_file.ts (100%) rename plugins/scaffolder-backend/{test => fixtures}/test-simple-template/template.yaml (100%) diff --git a/plugins/scaffolder-backend/test/test-nested-template/template-1/expected_file.ts b/plugins/scaffolder-backend/fixtures/test-nested-template/template-1/expected_file.ts similarity index 100% rename from plugins/scaffolder-backend/test/test-nested-template/template-1/expected_file.ts rename to plugins/scaffolder-backend/fixtures/test-nested-template/template-1/expected_file.ts diff --git a/plugins/scaffolder-backend/test/test-nested-template/template.yaml b/plugins/scaffolder-backend/fixtures/test-nested-template/template.yaml similarity index 100% rename from plugins/scaffolder-backend/test/test-nested-template/template.yaml rename to plugins/scaffolder-backend/fixtures/test-nested-template/template.yaml diff --git a/plugins/scaffolder-backend/test/test-simple-template/expected_file.ts b/plugins/scaffolder-backend/fixtures/test-simple-template/expected_file.ts similarity index 100% rename from plugins/scaffolder-backend/test/test-simple-template/expected_file.ts rename to plugins/scaffolder-backend/fixtures/test-simple-template/expected_file.ts diff --git a/plugins/scaffolder-backend/test/test-simple-template/template.yaml b/plugins/scaffolder-backend/fixtures/test-simple-template/template.yaml similarity index 100% rename from plugins/scaffolder-backend/test/test-simple-template/template.yaml rename to plugins/scaffolder-backend/fixtures/test-simple-template/template.yaml diff --git a/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts b/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts index 6ab0aa3fb0..b279b56ea3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/prepare/file.test.ts @@ -23,28 +23,41 @@ import { LOCATION_ANNOTATION, } from '@backstage/catalog-model'; +const setupTest = async (fixturePath: string) => { + const locationForTemplateYaml = path.resolve( + __dirname, + '../../../fixtures', + fixturePath, + ); + + const [parsedDocument] = YAML.parseAllDocuments( + await fs.readFile(locationForTemplateYaml, 'utf-8'), + ); + + const template: TemplateEntityV1alpha1 = parsedDocument.toJSON(); + template.metadata.annotations = { + [LOCATION_ANNOTATION]: `file:${locationForTemplateYaml}`, + }; + + const filePreparer = new FilePreparer(); + const resultDir = await filePreparer.prepare(template); + + return { filePreparer, template, resultDir }; +}; + describe('File preparer', () => { - it('resolves relative path from the template', async () => { - const locationForTemplateYaml = path.resolve( - __dirname, - '../../..', - 'test/test-simple-template/template.yaml', - ); - - const [parsedDocument] = YAML.parseAllDocuments( - await fs.readFile(locationForTemplateYaml, 'utf-8'), - ); - - const template: TemplateEntityV1alpha1 = parsedDocument.toJSON(); - - template.metadata.annotations = { - [LOCATION_ANNOTATION]: `file:${locationForTemplateYaml}`, - }; - - const filePreparer = new FilePreparer(); - const resultDir = await filePreparer.prepare(template); - - expect(fs.existsSync(`${resultDir}/expected_file.ts`)).toBe(true); + it('excludes the yaml file from the temp folder', async () => { + const { resultDir } = await setupTest('test-simple-template/template.yaml'); expect(fs.existsSync(`${resultDir}/template.yaml`)).toBe(false); }); + + it('resolves relative path from the template', async () => { + const { resultDir } = await setupTest('test-simple-template/template.yaml'); + expect(fs.existsSync(`${resultDir}/expected_file.ts`)).toBe(true); + }); + + it('resolves relative path from the nested template', async () => { + const { resultDir } = await setupTest('test-nested-template/template.yaml'); + expect(fs.existsSync(`${resultDir}/expected_file.ts`)).toBe(true); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/prepare/file.ts b/plugins/scaffolder-backend/src/scaffolder/prepare/file.ts index 184e1a6b1f..b9e8942aae 100644 --- a/plugins/scaffolder-backend/src/scaffolder/prepare/file.ts +++ b/plugins/scaffolder-backend/src/scaffolder/prepare/file.ts @@ -48,14 +48,15 @@ export class FilePreparer implements PreparerBase { path.join(os.tmpdir(), templateId), ); - const parentDirectory = path.dirname(templateEntityLocation); + const parentDirectory = path.resolve( + path.dirname(templateEntityLocation), + template.spec.path ?? '.', + ); await fs.copy(parentDirectory, tempDir, { filter: src => src !== templateEntityLocation, }); - // TODO(blam): Need to use the `spec.path` with path.resolve to ensure that the test case for test-nested-templates will work - return tempDir; } }