Skip to content

Commit

Permalink
[#153] Add hooks utils to contain postProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangmirs committed Apr 25, 2023
1 parent ed0e709 commit 82d7513
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 74 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nodejs 18.12.1
terraform 1.3.9
68 changes: 10 additions & 58 deletions src/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { prompt } from 'inquirer';

import { remove } from '@/helpers/file';
import { formatCode, detectTerraform } from '@/helpers/terraform';
import { postProcess } from '@/utils/hooks';

import Generator from '.';

jest.mock('inquirer');
jest.mock('@/helpers/terraform');
jest.mock('@/utils/hooks');

describe('Generator command', () => {
describe('given valid options', () => {
Expand Down Expand Up @@ -59,6 +59,10 @@ describe('Generator command', () => {
'The infrastructure template has been generated successfully!\n'
);
});

it('calls postProcess hook', () => {
expect(postProcess).toHaveBeenCalledTimes(1);
});
});

describe('given infrastructure type is advanced', () => {
Expand Down Expand Up @@ -117,6 +121,10 @@ describe('Generator command', () => {
'The infrastructure template has been generated successfully!\n'
);
});

it('calls postProcess hook', () => {
expect(postProcess).toHaveBeenCalledTimes(1);
});
});
});

Expand All @@ -139,61 +147,5 @@ describe('Generator command', () => {
);
});
});

describe('postProcess', () => {
const projectDir = 'postProcess-test';

describe('given current machine had terraform', () => {
beforeAll(async () => {
(prompt as unknown as jest.Mock)
.mockResolvedValueOnce({ provider: 'aws' })
.mockResolvedValueOnce({ infrastructureType: 'advanced' });

(detectTerraform as jest.Mock).mockImplementation(() => true);

await Generator.run([projectDir]);
});

afterAll(() => {
jest.resetAllMocks();
remove('/', projectDir);
});

it('runs formatCode', async () => {
await expect(formatCode).toHaveBeenCalled();
});
});

describe('given current machine did not have terraform', () => {
const consoleErrorSpy = jest.spyOn(global.console, 'error');

beforeAll(async () => {
(prompt as unknown as jest.Mock)
.mockResolvedValueOnce({ provider: 'aws' })
.mockResolvedValueOnce({ infrastructureType: 'advanced' });

(detectTerraform as jest.Mock).mockImplementation(() => {
throw new Error('terraform not found');
});

await Generator.run([projectDir]);
});

afterAll(() => {
jest.resetAllMocks();
remove('/', projectDir);
});

it('does NOT run formatCode', async () => {
await expect(formatCode).not.toHaveBeenCalled();
});

it('displays the error message', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
Error('terraform not found')
);
});
});
});
});
});
17 changes: 3 additions & 14 deletions src/commands/generate/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Args, Command, ux } from '@oclif/core';
import { prompt } from 'inquirer';

import { getProjectPath, remove } from '@/helpers/file';
import { detectTerraform, formatCode } from '@/helpers/terraform';
import { remove } from '@/helpers/file';
import {
applyVersionControl,
versionControlChoices,
} from '@/templates/addons/versionControl';
import { generateAwsTemplate } from '@/templates/aws';
import { applyCore } from '@/templates/core';
import { postProcess } from '@/utils/hooks';

type GeneralOptions = {
projectName: string;
Expand Down Expand Up @@ -60,6 +60,7 @@ export default class Generator extends Command {
};

await this.generate(generalOptions);
await postProcess(generalOptions);

ux.info('The infrastructure template has been generated successfully!');
}
Expand All @@ -76,8 +77,6 @@ export default class Generator extends Command {
default:
ux.error('This provider has not been implemented!');
}

await this.postProcess(generalOptions);
} catch (error) {
remove('/', generalOptions.projectName);

Expand All @@ -92,16 +91,6 @@ export default class Generator extends Command {
await applyCore(generalOptions);
await applyVersionControl(generalOptions);
}

private async postProcess(generalOptions: GeneralOptions) {
try {
if (await detectTerraform()) {
await formatCode(getProjectPath(generalOptions.projectName));
}
} catch (error) {
console.error(error);
}
}
}

export type { GeneralOptions };
7 changes: 7 additions & 0 deletions src/commands/installAddon/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { prompt } from 'inquirer';

import Generator from '@/commands/generate';
import { remove } from '@/helpers/file';
import { postProcess } from '@/utils/hooks';

import InstallAddon from '.';

jest.mock('inquirer');
jest.mock('@/utils/hooks');

describe('Install add-on command', () => {
describe('given AWS provider', () => {
Expand Down Expand Up @@ -72,6 +74,11 @@ describe('Install add-on command', () => {
`The \`vpc\` module has been installed to \`${projectDir}\` project successfully!\n`
);
});

it('calls postProcess hook', () => {
// One call for the generator and one for the add-on installation
expect(postProcess).toHaveBeenCalledTimes(2);
});
});

describe('given an INVALID add-on', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/commands/installAddon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Args, Flags, Command, ux } from '@oclif/core';
import { AwsOptions } from '@/templates/aws';
import { requireAWSModules } from '@/templates/core/dependencies';
import { awsModules } from '@/templates/core/types';
import { postProcess } from '@/utils/hooks';

import { GeneralOptions } from '../generate';

Expand Down Expand Up @@ -65,6 +66,8 @@ export default class InstallAddon extends Command {
this.error('This provider has not been implemented!');
}

await postProcess(options);

ux.info(
`The \`${args.moduleName}\` module has been installed to \`${projectName}\` project successfully!`
);
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/terraform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ describe('Terraform helper', () => {

await formatCode('/');

expect(runCommand).toHaveBeenCalledWith('terraform', ['fmt'], '/');
expect(runCommand).toHaveBeenCalledWith(
'terraform',
['fmt', '-recursive'],
'/'
);
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const detectTerraform = async () => {

const formatCode = async (projectDir: string) => {
try {
await runCommand('terraform', ['fmt'], projectDir);
await runCommand('terraform', ['fmt', '-recursive'], projectDir);
} catch (error) {
console.error(error);
}
Expand Down
55 changes: 55 additions & 0 deletions src/utils/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { remove } from '@/helpers/file';
import { formatCode, detectTerraform } from '@/helpers/terraform';

import { postProcess } from './hooks';

jest.mock('@/helpers/terraform');

describe('postProcess', () => {
const projectDir = 'postProcess-test';
const generalOptions = { projectName: projectDir, provider: 'aws' };

describe('given current machine had terraform', () => {
beforeAll(async () => {
(detectTerraform as jest.Mock).mockImplementation(() => true);

await postProcess(generalOptions);
});

afterAll(() => {
jest.resetAllMocks();
remove('/', projectDir);
});

it('runs formatCode', async () => {
await expect(formatCode).toHaveBeenCalled();
});
});

describe('given current machine did not have terraform', () => {
const consoleErrorSpy = jest.spyOn(global.console, 'error');

beforeAll(async () => {
(detectTerraform as jest.Mock).mockImplementation(() => {
throw new Error('terraform not found');
});

await postProcess(generalOptions);
});

afterAll(() => {
jest.resetAllMocks();
remove('/', projectDir);
});

it('does NOT run formatCode', async () => {
await expect(formatCode).not.toHaveBeenCalled();
});

it('displays the error message', () => {
expect(consoleErrorSpy).toHaveBeenCalledWith(
Error('terraform not found')
);
});
});
});
16 changes: 16 additions & 0 deletions src/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { GeneralOptions } from '@/commands/generate';
import { getProjectPath } from '@/helpers/file';
import { detectTerraform, formatCode } from '@/helpers/terraform';

const postProcess = async (generalOptions: GeneralOptions) => {
try {
if (await detectTerraform()) {
await formatCode(getProjectPath(generalOptions.projectName));
console.log('formatCode');
}
} catch (error) {
console.error(error);
}
};

export { postProcess };

0 comments on commit 82d7513

Please sign in to comment.