Skip to content

Commit

Permalink
test(CdktfConfig): add snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
gmeligio committed Jan 13, 2024
1 parent 9286b0b commit 17b1856
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 18 deletions.
20 changes: 14 additions & 6 deletions src/CdktfConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import { Component, JsonFile, Project } from 'projen';
import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4, validate } from 'uuid';

export interface TerraformDependencyConstraint {
/**
Expand Down Expand Up @@ -109,7 +109,7 @@ export class CdktfConfig extends Component {
/**
* Target language for building provider or module bindings. Currently supported: `typescript`, `python`, `java`, `csharp`, and `go`
*/
public readonly language: string;
public readonly language: Language;

/**
* Unique identifier for the project used to differentiate projects
Expand Down Expand Up @@ -138,8 +138,13 @@ export class CdktfConfig extends Component {
const projectIdKey = 'projectId';
const filePath = path.join(project.outdir, filename);

let projectId: string | undefined;
if (fs.existsSync(filePath)) {
let projectId = options.projectId;
// Validate is UUID
if (projectId !== undefined && !validate(projectId)) {
throw new Error(`Invalid UUID: ${projectId}. CDKTF projectID should be a valid UUID v4.`);
}

if (projectId === undefined && fs.existsSync(filePath)) {
const optionsFile = fs.readFileSync(filePath, 'utf8');
const optionsObject = JSON.parse(optionsFile);

Expand All @@ -153,12 +158,15 @@ export class CdktfConfig extends Component {
this.terraformProviders = options.terraformProviders ?? [];
this.terraformModules = options.terraformModules ?? [];

const cdktfOptions = {
const cdktfOptions: CdktfConfigOptions = {
app: this.app,
codeMakerOutput: options.codeMakerOutput,
language: this.language,
projectId: this.projectId,
sendCrashReports: this.sendCrashReports,
terraformProviders: this.terraformProviders,
terraformModules: this.terraformModules,
...options,
output: options.output,
};

this.json = new JsonFile(project, filename, {
Expand Down
64 changes: 52 additions & 12 deletions test/CdktfConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as fs from 'fs';
import { NodeProject } from 'projen/lib/javascript';
import { Testing } from 'projen/lib/testing';
import { TestProject } from './TestProject';
import { CdktfConfig, Language, RequirementDefinition } from '../src/CdktfConfig';

Expand All @@ -13,6 +15,24 @@ jest.mock('fs', () => {
});

describe('CdktfConfig', () => {
test('generate cdktf.json following schema', () => {
// Save snapshot in this tests that has a fixed projectId because snapshot should be deterministic and projectId is randomly generated when it doesnt
const project = new NodeProject({
name: 'test_project',
defaultReleaseBranch: 'main',
});

new CdktfConfig(project, {
app: 'npx ts-node main.ts',
language: Language.TYPESCRIPT,
projectId: 'c9e8d26b-bb1c-4788-a36a-2cdbc5d07833',
});

const snapshot = Testing.synth(project);

expect(snapshot['cdktf.json']).toMatchSnapshot();
});

describe('app', () => {
test('can be overriden with custom command', () => {
const command = 'npx ts-node --swc src/main.ts';
Expand All @@ -32,7 +52,18 @@ describe('CdktfConfig', () => {
jest.clearAllMocks();
});

test('should be `UUID`', () => {
test('should throw error for invalid UUID', () => {
const invalidUUID = 'invalidUUID';
expect(() => {
new CdktfConfig(new TestProject(), {
app: 'npx ts-node main.ts',
language: Language.TYPESCRIPT,
projectId: invalidUUID,
});
}).toThrow(`Invalid UUID: ${invalidUUID}. CDKTF projectID should be a valid UUID v4.`);
});

test('should have UUID format', () => {
const command = 'npx ts-node --swc src/main.ts';

const cdktfConfig = new CdktfConfig(new TestProject(), {
Expand All @@ -43,8 +74,19 @@ describe('CdktfConfig', () => {
expect(cdktfConfig.projectId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
});

test('should use provided projectId if it is valid UUID', () => {
const projectId = 'c9e8d26b-bb1c-4788-a36a-2cdbc5d07833';
const cdktfConfig = new CdktfConfig(new TestProject(), {
app: 'npx ts-node main.ts',
language: Language.TYPESCRIPT,
projectId: projectId,
});

expect(cdktfConfig.projectId).toStrictEqual(projectId);
});

test('should reuse existing `projectId` when `cdktf.json` exists', () => {
const existingProjectId = '12345678-1234-1234-1234-123456789012';
const existingProjectId = 'c9e8d26b-bb1c-4788-a36a-2cdbc5d07833';

// Mock the fs.existsSync function to return true for 'cdktf.json'
jest.spyOn(fs, 'existsSync').mockImplementation((p: fs.PathLike) => {
Expand All @@ -63,23 +105,21 @@ describe('CdktfConfig', () => {
// Then we need to check if the path contains cdktf.json
if (p.toString().includes('cdktf.json')) {
return JSON.stringify({
projectId: '12345678-1234-1234-1234-123456789012',
projectId: existingProjectId,
});
}

return '';
});

const cdktfConfig = new CdktfConfig(
new TestProject({
outdir: '.',
}),
{
app: 'npx ts-node main.ts',
language: Language.TYPESCRIPT,
}
);
const project = new TestProject({
outdir: '.',
});

const cdktfConfig = new CdktfConfig(project, {
app: 'npx ts-node main.ts',
language: Language.TYPESCRIPT,
});
expect(cdktfConfig.projectId).toStrictEqual(existingProjectId);
});
});
Expand Down
13 changes: 13 additions & 0 deletions test/__snapshots__/CdktfConfig.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17b1856

Please sign in to comment.