Skip to content

Commit

Permalink
[#274] Add tests to project name processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihisil committed Feb 5, 2024
1 parent ded9e06 commit 32b091e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
22 changes: 17 additions & 5 deletions src/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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', () => {
Expand All @@ -51,7 +52,7 @@ describe('Generator command', () => {
'shared/outputs.tf',
];

expect(projectDir).toHaveFiles(expectedFiles);
expect(processedDirectoryName).toHaveFiles(expectedFiles);
});

it('displays the success message', () => {
Expand All @@ -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', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default class Generator extends Command {
const { args } = await this.parse(Generator);

const generalPrompt = await prompt<GeneralOptions>([...providerChoices]);

const projectName = args.projectName.toLowerCase().replace(/\s/g, '-');
const generalOptions: GeneralOptions = {
projectName: projectName,
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/generators/terraform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 32b091e

Please sign in to comment.