Skip to content

Commit

Permalink
Merge branch 'main' into fix-rmdir-depreciation-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt authored Jan 7, 2022
2 parents 1550eb0 + c02901a commit 565238c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/cdktf/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export function hashMapper(elementMapper: Mapper): Mapper {
return x;
}

// We can't treat strings as hashes (likely a token or a misconfiguration)
if (typeof x === "string") {
return x;
}

const ret: any = {};

Object.keys(x).forEach((key) => {
Expand Down
21 changes: 21 additions & 0 deletions packages/cdktf/test/functions.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Testing, TerraformStack, TerraformOutput, Fn } from "../lib";
import { TerraformVariable } from "../lib/terraform-variable";
import { TerraformLocal } from "../lib/terraform-local";
import { ref } from "../lib/tfExpression";
import { Token } from "../lib/tokens/token";

test("static values", () => {
const app = Testing.app();
Expand Down Expand Up @@ -490,3 +492,22 @@ test("rawString escapes correctly", () => {
expect(json.locals.test).toHaveProperty("doublequotes", `${bslsh}"${bslsh}"`);
expect(json.locals.test).toHaveProperty("template", "$${TEMPLATE}");
});

test("tomap does not destroy incoming ref", () => {
expect(
Testing.synthScope(
(scope) =>
new TerraformLocal(
scope,
"test",
Fn.tomap(Token.asString(ref("test.instance.attr")))
)
)
).toMatchInlineSnapshot(`
"{
\\"locals\\": {
\\"test\\": \\"\${tomap(test.instance.attr)}\\"
}
}"
`);
});
86 changes: 86 additions & 0 deletions packages/cdktf/test/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { resolve } from "../lib/_tokens";
import { listMapper, hashMapper, anyToTerraform } from "../lib/runtime";
import { ref } from "../lib/tfExpression";
import { Token } from "../lib/tokens/token";
import { Fn } from "../lib/terraform-functions";

const resolveExpression = (expr: any) => resolve(null as any, expr);

describe("Runtime", () => {
describe("listmapper", () => {
it("maps through the list", () => {
const identity = jest.fn().mockImplementation((x: any) => x);

expect(resolveExpression(listMapper(identity)(["a", "b", "c", "d"])))
.toMatchInlineSnapshot(`
Array [
"a",
"b",
"c",
"d",
]
`);
expect(identity).toHaveBeenCalledTimes(4);
});

it("leaves references in tact", () => {
const identity = jest.fn().mockImplementation((x: any) => x);
const reference = ref("some_resource.my_resource.some_attribute_array");

expect(
resolveExpression(listMapper(identity)(["a", reference, "b", "c", "d"]))
).toMatchInlineSnapshot(`
Array [
"a",
"\${some_resource.my_resource.some_attribute_array}",
"b",
"c",
"d",
]
`);
expect(identity).toHaveBeenCalledTimes(5);
});

it("leaves directly passed references in tact with a list", () => {
const identity = jest.fn().mockImplementation((x: any) => x);
const reference = ref("some_resource.my_resource.some_attribute_array");

expect(
resolveExpression(listMapper(identity)(reference))
).toMatchInlineSnapshot(
`"\${some_resource.my_resource.some_attribute_array}"`
);
});

it("leaves directly passed references intact with a tokenized list", () => {
const identity = jest.fn().mockImplementation((x: any) => x);
const reference = ref("some_resource.my_resource.some_attribute_array");

expect(
resolveExpression({
x: listMapper(identity)(Token.asList(reference)),
})
).toMatchInlineSnapshot(`
Object {
"x": "\${some_resource.my_resource.some_attribute_array}",
}
`);
});

it("works together with a hashmapper", () => {
const reference = Token.asString(
ref("some_resource.my_resource.some_attribute_array")
);

expect(
resolveExpression({
match_labels: Fn.tomap(hashMapper(anyToTerraform)(reference)),
})
).toMatchInlineSnapshot(`
Object {
"match_labels": "\${tomap(some_resource.my_resource.some_attribute_array)}",
}
`);
});
});
});

0 comments on commit 565238c

Please sign in to comment.