Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix module variables without type #630

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/cdktf-cli/lib/get/generator/provider-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ const transformVariables = (variables: any) => {
if (variable.hasOwnProperty('type') == false && variable.hasOwnProperty('default') == true) {
switch (typeof variable['default']) {
case "boolean": variableType = 'bool' ; break;
case "number": variableType = 'number' ; break;
default: variableType = 'any';
}
} else {
const matched = (variable['type'] as string).match(/\$\{(.*)\}/)
const matched = (variable['type'] as string)?.match(/\$\{(.*)\}/)
variableType = matched ? matched[1] : 'any'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,3 +709,47 @@ export class Module extends TerraformModule {
}
"
`;

exports[`typeless variables 1`] = `
"// generated by cdktf get
// ./module
import { TerraformModule } from 'cdktf';
import { Construct } from 'constructs';
export interface ModuleOptions {
/**
* My example var without type set, but with default
* @default 1
*/
readonly myDefaultTypevar?: number;
/**
* My example var without type set
*/
readonly myTypelessVar: any;
}
export class Module extends TerraformModule {
private readonly inputs: { [name: string]: any } = { }
public constructor(scope: Construct, id: string, options: ModuleOptions) {
super(scope, id, {
source: './module',
});
this.myDefaultTypevar = options.myDefaultTypevar;
this.myTypelessVar = options.myTypelessVar;
}
public get myDefaultTypevar(): number | undefined {
return this.inputs['my_default_typevar'] as number | undefined;
}
public set myDefaultTypevar(value: number | undefined) {
this.inputs['my_default_typevar'] = value;
}
public get myTypelessVar(): any {
return this.inputs['my_typeless_var'] as any;
}
public set myTypelessVar(value: any) {
this.inputs['my_typeless_var'] = value;
}
protected synthesizeAttributes() {
return this.inputs;
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
variable "my_typeless_var" {
description = "My example var without type set"
}

variable "my_default_typevar" {
description = "My example var without type set, but with default"
default = 1
}
25 changes: 3 additions & 22 deletions packages/cdktf-cli/test/get/generator/module-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as os from 'os';
import * as path from 'path';
import { ConstructsMaker, Language } from '../../../lib/get/constructs-maker'
import { TerraformModuleConstraint } from '../../../lib/config'
import { withTempDir } from '../../../lib/util';
import { expectModuleToMatchSnapshot } from '../util';

test('generate some modules', async () => {
jest.setTimeout(20000)
Expand All @@ -18,25 +18,6 @@ test('generate some modules', async () => {
expect(output).toMatchSnapshot();
});

test('no module outputs', async () => {
await withTempDir('no-output-module.test', async () => {
const curdir = process.cwd();
fs.mkdirSync('module');
fs.copyFileSync(path.join(__dirname, 'fixtures', 'module-no-outputs.test.fixture.tf'), path.join(curdir, 'module', 'main.tf'));
expectModuleToMatchSnapshot('no module outputs', 'generator', 'module-no-outputs.test.fixture.tf');

const constraint = new TerraformModuleConstraint({
source: './module',
name: 'module',
fqn: 'module'
});

fs.mkdirSync('work');
const workdir = path.join(curdir, 'work');

const maker = new ConstructsMaker({codeMakerOutput: workdir, targetLanguage: Language.TYPESCRIPT}, [constraint])
await maker.generate();

const output = fs.readFileSync(path.join(workdir, 'modules/module.ts'), 'utf-8');
expect(output).toMatchSnapshot();
});
});
expectModuleToMatchSnapshot('typeless variables', 'generator', 'module-no-variable-type.test.fixture.tf');
33 changes: 29 additions & 4 deletions packages/cdktf-cli/test/get/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { promises as fs } from 'fs';
import { mkdtemp } from "../../lib/util";
import * as fs from 'fs';
import { mkdtemp, withTempDir } from "../../lib/util";
import { Language, ConstructsMaker } from "../../lib/get/constructs-maker";
import * as path from 'path';
import { TerraformDependencyConstraint } from '../../lib/config';
import { TerraformDependencyConstraint, TerraformModuleConstraint } from '../../lib/config';

export function expectImportMatchSnapshot(constraint: TerraformDependencyConstraint) {
jest.setTimeout(60_000);
Expand All @@ -19,7 +19,7 @@ export function expectImportMatchSnapshot(constraint: TerraformDependencyConstra

await maker.generate()

const manifest = JSON.parse(await fs.readFile(jsiiPath, 'utf-8'));
const manifest = JSON.parse(await fs.promises.readFile(jsiiPath, 'utf-8'));

// patch cdktf version in manifest because it's not stable
manifest.dependencies.cdktf = '999.999.999';
Expand All @@ -28,4 +28,29 @@ export function expectImportMatchSnapshot(constraint: TerraformDependencyConstra
expect(manifest).toMatchSnapshot();
});
});
}

export function expectModuleToMatchSnapshot(testName: string, testCategory: string, fixtureName: string) {
test(testName, async () => {
ansgarm marked this conversation as resolved.
Show resolved Hide resolved
await withTempDir(`${testName.replace(/\s*/, '-')}.test`, async () => {
const curdir = process.cwd();
fs.mkdirSync('module');
fs.copyFileSync(path.join(__dirname, testCategory, 'fixtures', fixtureName), path.join(curdir, 'module', 'main.tf'));

const constraint = new TerraformModuleConstraint({
source: './module',
name: 'module',
fqn: 'module'
});

fs.mkdirSync('work');
const workdir = path.join(curdir, 'work');

const maker = new ConstructsMaker({codeMakerOutput: workdir, targetLanguage: Language.TYPESCRIPT}, [constraint])
await maker.generate();

const output = fs.readFileSync(path.join(workdir, 'modules/module.ts'), 'utf-8');
expect(output).toMatchSnapshot();
});
});
}