From 007ce2ac99510ba234ad8889203a393afe63b25a Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 12 Jan 2024 10:32:45 +0700 Subject: [PATCH] [#274] Add tests to project name processing --- src/commands/generate/index.test.ts | 22 +++++++++++++++++----- src/commands/generate/index.ts | 5 +++-- src/generators/terraform/index.ts | 3 +-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/commands/generate/index.test.ts b/src/commands/generate/index.test.ts index b8caed76..1f71f665 100644 --- a/src/commands/generate/index.test.ts +++ b/src/commands/generate/index.test.ts @@ -12,7 +12,8 @@ describe('Generator command', () => { describe('given valid options', () => { describe('given provider is AWS', () => { describe('given infrastructure type is blank', () => { - const projectDir = 'aws-blank-test'; + const originalDirectoryName = 'AWS blank test'; + const processedDirectoryName = 'aws-blank-test'; const stdoutSpy = jest.spyOn(process.stdout, 'write'); beforeAll(async () => { @@ -23,18 +24,18 @@ describe('Generator command', () => { terraformCloudEnabled: false, }); - await Generator.run([projectDir]); + await Generator.run([originalDirectoryName]); }); afterAll(() => { jest.clearAllMocks(); - remove('/', projectDir); + remove('/', processedDirectoryName); }); it('creates expected directories', () => { const expectedDirectories = ['core/', 'shared/']; - expect(projectDir).toHaveDirectories(expectedDirectories); + expect(processedDirectoryName).toHaveDirectories(expectedDirectories); }); it('creates expected files', () => { @@ -51,7 +52,7 @@ describe('Generator command', () => { 'shared/outputs.tf', ]; - expect(projectDir).toHaveFiles(expectedFiles); + expect(processedDirectoryName).toHaveFiles(expectedFiles); }); it('displays the success message', () => { @@ -63,6 +64,17 @@ describe('Generator command', () => { it('calls postProcess hook', () => { expect(postProcess).toHaveBeenCalledTimes(1); }); + + it('contains processed project name in main files', () => { + const mainFiles = ['shared/main.tf', 'core/main.tf']; + mainFiles.forEach((fileName) => { + expect(processedDirectoryName).toHaveContentInFile( + fileName, + `project_name = "${processedDirectoryName}"`, + { ignoreSpaces: true } + ); + }); + }); }); describe('given infrastructure type is advanced', () => { diff --git a/src/commands/generate/index.ts b/src/commands/generate/index.ts index fb80fd15..ac8d85ae 100644 --- a/src/commands/generate/index.ts +++ b/src/commands/generate/index.ts @@ -46,7 +46,6 @@ export default class Generator extends Command { const { args } = await this.parse(Generator); const generalPrompt = await prompt([...providerChoices]); - const projectName = args.projectName.toLowerCase().replace(/\s/g, '-'); const generalOptions: GeneralOptions = { projectName: projectName, @@ -56,7 +55,9 @@ export default class Generator extends Command { await this.generate(generalOptions); await postProcess(generalOptions); - ux.info(`The infrastructure code was generated at '${args.projectName}'`); + ux.info( + `The infrastructure code was generated at '${generalOptions.projectName}'` + ); } private async generate(generalOptions: GeneralOptions) { diff --git a/src/generators/terraform/index.ts b/src/generators/terraform/index.ts index 1222196d..2298de1a 100644 --- a/src/generators/terraform/index.ts +++ b/src/generators/terraform/index.ts @@ -12,11 +12,10 @@ const applyTerraformCore = async (generalOptions: GeneralOptions) => { copy('terraform/', '.', projectName); - // Use projectName to append the Namespace local in the main.tf file const coreLocalsContent = dedent` locals { project_name = "${projectName}" - env_namespace = "${projectName}-\${var.environment}" + env_namespace = "\${local.project_name}-\${var.environment}" }`; appendToFile(INFRA_CORE_MAIN_PATH, coreLocalsContent, projectName);