diff --git a/packages/cdktf-cli/lib/get/generator/provider-schema.ts b/packages/cdktf-cli/lib/get/generator/provider-schema.ts index b32ac6ffd1..771bbbe8c0 100644 --- a/packages/cdktf-cli/lib/get/generator/provider-schema.ts +++ b/packages/cdktf-cli/lib/get/generator/provider-schema.ts @@ -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' } diff --git a/packages/cdktf-cli/test/get/generator/__snapshots__/module-generator.test.ts.snap b/packages/cdktf-cli/test/get/generator/__snapshots__/module-generator.test.ts.snap index 48fb99eaf9..9fbb38e536 100644 --- a/packages/cdktf-cli/test/get/generator/__snapshots__/module-generator.test.ts.snap +++ b/packages/cdktf-cli/test/get/generator/__snapshots__/module-generator.test.ts.snap @@ -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; + } +} +" +`; diff --git a/packages/cdktf-cli/test/get/generator/fixtures/module-no-variable-type.test.fixture.tf b/packages/cdktf-cli/test/get/generator/fixtures/module-no-variable-type.test.fixture.tf new file mode 100644 index 0000000000..4923d5735a --- /dev/null +++ b/packages/cdktf-cli/test/get/generator/fixtures/module-no-variable-type.test.fixture.tf @@ -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 +} \ No newline at end of file diff --git a/packages/cdktf-cli/test/get/generator/module-generator.test.ts b/packages/cdktf-cli/test/get/generator/module-generator.test.ts index 878f6846a5..b8240e0040 100644 --- a/packages/cdktf-cli/test/get/generator/module-generator.test.ts +++ b/packages/cdktf-cli/test/get/generator/module-generator.test.ts @@ -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) @@ -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(); - }); -}); \ No newline at end of file +expectModuleToMatchSnapshot('typeless variables', 'generator', 'module-no-variable-type.test.fixture.tf'); \ No newline at end of file diff --git a/packages/cdktf-cli/test/get/util.ts b/packages/cdktf-cli/test/get/util.ts index ef5e1a828d..5ed24367a2 100644 --- a/packages/cdktf-cli/test/get/util.ts +++ b/packages/cdktf-cli/test/get/util.ts @@ -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); @@ -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'; @@ -28,4 +28,29 @@ export function expectImportMatchSnapshot(constraint: TerraformDependencyConstra expect(manifest).toMatchSnapshot(); }); }); +} + +export function expectModuleToMatchSnapshot(testName: string, testCategory: string, fixtureName: string) { + test(testName, async () => { + 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(); + }); + }); } \ No newline at end of file