@cdktn/provider-generator 0.25.0 → 0.25.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/build/__tests__/erasable-syntax.test.d.ts +2 -0
  2. package/build/__tests__/erasable-syntax.test.js +96 -0
  3. package/build/get/__tests__/generator/empty-provider-resources.test.js +5 -1
  4. package/build/get/__tests__/generator/module-docs-registry.test.js +8 -10
  5. package/build/get/__tests__/generator/module-generator.test.js +7 -10
  6. package/build/get/__tests__/target-versions.test.js +4 -2
  7. package/build/get/constructs-maker.js +3 -3
  8. package/build/get/generator/custom-defaults.js +2 -1
  9. package/build/get/generator/models/scope.js +2 -14
  10. package/build/get/generator/resource-parser.js +3 -3
  11. package/eslint.config.mjs +11 -0
  12. package/jest.config.js +0 -1
  13. package/package.json +5 -5
  14. package/package.sh +0 -0
  15. package/src/__tests__/edge-provider-schema/builder.ts +0 -201
  16. package/src/__tests__/edge-provider-schema/cli.ts +0 -51
  17. package/src/__tests__/edge-provider-schema/index.ts +0 -284
  18. package/src/__tests__/edge-provider-schema.test.ts +0 -24
  19. package/src/__tests__/provider.test.ts +0 -208
  20. package/src/get/__tests__/constructs-maker.test.ts +0 -242
  21. package/src/get/__tests__/generator/complex-computed-types.test.ts +0 -28
  22. package/src/get/__tests__/generator/deep-nested-attributes.test.ts +0 -56
  23. package/src/get/__tests__/generator/description-escaping.test.ts +0 -84
  24. package/src/get/__tests__/generator/empty-provider-resources.test.ts +0 -26
  25. package/src/get/__tests__/generator/ephemeral-resources.test.ts +0 -46
  26. package/src/get/__tests__/generator/export-sharding.test.ts +0 -169
  27. package/src/get/__tests__/generator/import-style.test.ts +0 -129
  28. package/src/get/__tests__/generator/module-docs-registry.test.ts +0 -89
  29. package/src/get/__tests__/generator/module-generator.test.ts +0 -528
  30. package/src/get/__tests__/generator/nested-types.test.ts +0 -54
  31. package/src/get/__tests__/generator/provider-functions-name-collision.test.ts +0 -84
  32. package/src/get/__tests__/generator/provider-functions.test.ts +0 -262
  33. package/src/get/__tests__/generator/provider-source-matching.test.ts +0 -84
  34. package/src/get/__tests__/generator/provider.test.ts +0 -59
  35. package/src/get/__tests__/generator/resource-types.test.ts +0 -118
  36. package/src/get/__tests__/generator/skipped-attributes.test.ts +0 -72
  37. package/src/get/__tests__/generator/supported-stored-classes.test.ts +0 -147
  38. package/src/get/__tests__/generator/types.test.ts +0 -686
  39. package/src/get/__tests__/generator/versions-file.test.ts +0 -77
  40. package/src/get/__tests__/generator/write-only.test.ts +0 -71
  41. package/src/get/__tests__/registry-class-invalidation.test.ts +0 -123
  42. package/src/get/__tests__/target-versions.test.ts +0 -249
  43. package/src/get/__tests__/util.ts +0 -80
  44. package/src/get/constructs-maker.ts +0 -991
  45. package/src/get/generator/custom-defaults.ts +0 -493
  46. package/src/get/generator/emitter/attributes-emitter.ts +0 -316
  47. package/src/get/generator/emitter/index.ts +0 -6
  48. package/src/get/generator/emitter/provider-functions-emitter.ts +0 -117
  49. package/src/get/generator/emitter/resource-emitter.ts +0 -330
  50. package/src/get/generator/emitter/struct-emitter.ts +0 -683
  51. package/src/get/generator/loop-detection.ts +0 -81
  52. package/src/get/generator/models/attribute-model.ts +0 -273
  53. package/src/get/generator/models/attribute-type-model.ts +0 -559
  54. package/src/get/generator/models/index.ts +0 -8
  55. package/src/get/generator/models/provider-function-model.ts +0 -872
  56. package/src/get/generator/models/resource-model.ts +0 -206
  57. package/src/get/generator/models/scope.ts +0 -54
  58. package/src/get/generator/models/struct.ts +0 -124
  59. package/src/get/generator/models/supported-stored-classes.ts +0 -88
  60. package/src/get/generator/module-generator.ts +0 -236
  61. package/src/get/generator/provider-generator.ts +0 -451
  62. package/src/get/generator/resource-parser.ts +0 -782
  63. package/src/get/generator/sanitized-comments.ts +0 -49
  64. package/src/get/generator/skipped-attributes.ts +0 -27
  65. package/src/index.ts +0 -40
  66. package/src/util.ts +0 -26
@@ -1,77 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import * as fs from "fs-extra";
4
- import * as path from "path";
5
- import { Language, TerraformDependencyConstraint } from "@cdktn/commons";
6
- import { ConstructsMaker, GetOptions } from "../../constructs-maker";
7
- import { createTmpHelper } from "../util";
8
-
9
- const tmp = createTmpHelper();
10
-
11
- jest.setTimeout(600_000);
12
- global.setImmediate =
13
- global.setImmediate ||
14
- ((fn: any, ...args: any[]) => global.setTimeout(fn, 0, ...args));
15
-
16
- describe("versions.json file generation", () => {
17
- const languages = [Language.TYPESCRIPT, Language.PYTHON];
18
-
19
- const constraints: TerraformDependencyConstraint[] = [
20
- {
21
- fqn: "hashicorp/aws",
22
- name: "aws",
23
- source: "hashicorp/aws",
24
- version: "4.0.0",
25
- },
26
- {
27
- fqn: "hashicorp/random",
28
- name: "random",
29
- source: "hashicorp/random",
30
- version: "3.1.3",
31
- },
32
- {
33
- fqn: "kreuzwerker/docker",
34
- name: "docker",
35
- source: "kreuzwerker/docker",
36
- // OpenTofu rejects everything below 3.7.0 as unsigned; see #440.
37
- version: "3.9.0",
38
- },
39
- ];
40
-
41
- beforeAll(() => {});
42
-
43
- test.each(languages)(
44
- "generates a file for the %s language",
45
- async (language) => {
46
- const workdir = tmp("versions-file.test");
47
-
48
- const options: GetOptions = {
49
- codeMakerOutput: workdir,
50
- targetLanguage: language,
51
- };
52
-
53
- // Ignore warnings to pop up from the generate function
54
- process.env.NODE_OPTIONS = "--max-old-space-size=16384";
55
- const constructMaker = new ConstructsMaker(
56
- options,
57
- process.env.CDKTF_EXPERIMENTAL_PROVIDER_SCHEMA_CACHE_PATH,
58
- );
59
-
60
- await constructMaker.generate(constraints);
61
-
62
- const output = fs.readFileSync(
63
- path.join(workdir, "versions.json"),
64
- "utf-8",
65
- );
66
- // versions.json is keyed by fully qualified name, and the host is
67
- // whichever registry the fetching CLI used - terraform's or tofu's - so
68
- // assert on the provider part rather than pinning one registry.
69
- const keys = Object.keys(JSON.parse(output)).map((k) =>
70
- k.split("/").slice(1).join("/"),
71
- );
72
- expect(keys).toEqual(
73
- expect.arrayContaining(constraints.map((c) => c.fqn)),
74
- );
75
- },
76
- );
77
- });
@@ -1,71 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import * as fs from "fs";
4
- import * as path from "path";
5
- import { TerraformProviderGenerator } from "../../generator/provider-generator";
6
- import { CodeMaker } from "codemaker";
7
- import { createTmpHelper } from "../util";
8
-
9
- const tmp = createTmpHelper();
10
-
11
- test("generate a vault_alicloud_secret_backend resource with a write-only attribute", async () => {
12
- const code = new CodeMaker();
13
- const workdir = tmp("write-only.test");
14
- const spec = JSON.parse(
15
- fs.readFileSync(
16
- path.join(__dirname, "fixtures", "write-only.test.fixture.json"),
17
- "utf-8",
18
- ),
19
- );
20
- new TerraformProviderGenerator(code, spec).generateAll();
21
- await code.save(workdir);
22
-
23
- const output = fs.readFileSync(
24
- path.join(workdir, "providers/vault/alicloud-secret-backend/index.ts"),
25
- "utf-8",
26
- );
27
- expect(output).toMatchSnapshot();
28
-
29
- // The write-only attribute's getter is deprecated ...
30
- expect(output).toMatch(
31
- /@deprecated Write-only: the provider never returns this value[\s\S]*?public get secretKeyWo\(\)/,
32
- );
33
- // ... its setter is a plain assignment ...
34
- expect(output).toMatch(
35
- /public set secretKeyWo\(value: string\) \{\n\s*this\._secretKeyWo = value;\n\s*\}/,
36
- );
37
- // ... and usage registration happens at resolve time, from inside
38
- // synthesizeAttributes()/synthesizeHclAttributes(), by wrapping the
39
- // mapped value in markWriteOnlyAttribute() ...
40
- expect(output).toMatch(
41
- /secret_key_wo: this\.markWriteOnlyAttribute\(cdktn\.stringToTerraform\(this\._secretKeyWo\)\),/,
42
- );
43
- expect(output).toMatch(
44
- /value: this\.markWriteOnlyAttribute\(cdktn\.stringToHclTerraform\(this\._secretKeyWo\)\),/,
45
- );
46
-
47
- // The required write-only attribute gets no reset method (required
48
- // attributes never do) ...
49
- expect(output).not.toMatch(/resetSecretKeyWo\b/);
50
- // ... but the OPTIONAL write-only sibling does, exercising the reset ->
51
- // clear -> synth-passes path end to end at the generator level.
52
- expect(output).toMatch(
53
- /public resetSecondarySecretKeyWo\(\) \{\n\s*this\._secondarySecretKeyWo = undefined;\n\s*\}/,
54
- );
55
- expect(output).toMatch(
56
- /secondary_secret_key_wo: this\.markWriteOnlyAttribute\(cdktn\.stringToTerraform\(this\._secondarySecretKeyWo\)\),/,
57
- );
58
-
59
- // The non-write-only sibling attribute is untouched: its getter is
60
- // emitted directly (no @deprecated JSDoc immediately above), its setter
61
- // has no registration call, and its synthesis isn't wrapped.
62
- expect(output).toMatch(
63
- /private _secretKeyWoVersion\?: number; \n\s*public get secretKeyWoVersion\(\)/,
64
- );
65
- expect(output).toMatch(
66
- /public set secretKeyWoVersion\(value: number\) \{\n\s*this\._secretKeyWoVersion = value;\n\s*\}/,
67
- );
68
- expect(output).toMatch(
69
- /secret_key_wo_version: cdktn\.numberToTerraform\(this\._secretKeyWoVersion\),/,
70
- );
71
- });
@@ -1,123 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import * as fs from "fs-extra";
4
- import * as path from "path";
5
- import {
6
- Language,
7
- TerraformProviderConstraint,
8
- TerraformTargetVersions,
9
- } from "@cdktn/commons";
10
- import { createTmpHelper } from "./util";
11
-
12
- const mockReadSchema = jest.fn();
13
- jest.mock("@cdktn/provider-schema", () => ({
14
- readSchema: (...args: unknown[]) => mockReadSchema(...args),
15
- }));
16
-
17
- // Imported after the mock so ConstructsMaker picks up the mocked readSchema.
18
- import { ConstructsMaker } from "../constructs-maker";
19
-
20
- const tmp = createTmpHelper();
21
-
22
- // No resources, so generation stays in TypeScript emission.
23
- const fakeSchema = {
24
- providerSchema: {
25
- format_version: "0.2" as const,
26
- provider_schemas: {
27
- "registry.terraform.io/hashicorp/random": {
28
- resource_schemas: null,
29
- data_source_schemas: null,
30
- },
31
- },
32
- provider_versions: { "registry.terraform.io/hashicorp/random": "3.2.0" },
33
- cli_name: "terraform",
34
- cli_version: "1.7.5",
35
- },
36
- };
37
-
38
- describe("filterAlreadyGenerated: registry class", () => {
39
- const constraint = new TerraformProviderConstraint("hashicorp/random@=3.2.0");
40
- let outdir: string;
41
-
42
- const maker = (targetVersions?: TerraformTargetVersions) =>
43
- new ConstructsMaker({
44
- codeMakerOutput: outdir,
45
- targetLanguage: Language.TYPESCRIPT,
46
- targetVersions,
47
- });
48
-
49
- /** A real previous `cdktn get`, so constraints.json is what generation writes. */
50
- const generateWith = (targetVersions?: TerraformTargetVersions) =>
51
- maker(targetVersions).generate([constraint]);
52
-
53
- beforeEach(() => {
54
- mockReadSchema.mockReset();
55
- mockReadSchema.mockResolvedValue(fakeSchema);
56
- outdir = tmp("registry-class");
57
- });
58
-
59
- it.each<
60
- [
61
- string,
62
- TerraformTargetVersions | undefined,
63
- TerraformTargetVersions | undefined,
64
- ]
65
- >([
66
- ["nothing changed", undefined, undefined],
67
- [
68
- "both products declared, the same class as nothing",
69
- undefined,
70
- { terraform: ">=1.5.7", opentofu: ">=1.6.0" },
71
- ],
72
- [
73
- "a range moving within the OpenTofu class",
74
- { opentofu: ">=1.6.0" },
75
- { opentofu: ">=1.8.0" },
76
- ],
77
- ])("skips regeneration: %s", async (_name, before, after) => {
78
- await generateWith(before);
79
-
80
- await expect(
81
- maker(after).filterAlreadyGenerated([constraint]),
82
- ).resolves.toEqual([]);
83
- });
84
-
85
- it("regenerates when the project switches to OpenTofu-only", async () => {
86
- await generateWith(undefined);
87
-
88
- await expect(
89
- maker({ opentofu: ">=1.6.0" }).filterAlreadyGenerated([constraint]),
90
- ).resolves.toEqual([constraint]);
91
- });
92
-
93
- it("regenerates when the project switches away from OpenTofu-only", async () => {
94
- await generateWith({ opentofu: ">=1.6.0" });
95
-
96
- await expect(
97
- maker({ terraform: ">=1.5.7" }).filterAlreadyGenerated([constraint]),
98
- ).resolves.toEqual([constraint]);
99
- });
100
-
101
- // constraints.json written before targetVersions was stamped has no such field;
102
- // it must read as the Terraform class, which is what it was generated for.
103
- describe("constraints.json from before targetVersions was recorded", () => {
104
- beforeEach(async () => {
105
- await generateWith({ terraform: ">=1.5.7" });
106
- const file = path.join(outdir, "constraints.json");
107
- const { targetVersions: _dropped, ...older } = fs.readJsonSync(file);
108
- fs.writeJsonSync(file, older);
109
- });
110
-
111
- it("is fresh for a Terraform project", async () => {
112
- await expect(
113
- maker().filterAlreadyGenerated([constraint]),
114
- ).resolves.toEqual([]);
115
- });
116
-
117
- it("is stale for an OpenTofu-only project", async () => {
118
- await expect(
119
- maker({ opentofu: ">=1.6.0" }).filterAlreadyGenerated([constraint]),
120
- ).resolves.toEqual([constraint]);
121
- });
122
- });
123
- });
@@ -1,249 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import * as fs from "fs-extra";
4
- import * as path from "path";
5
- import {
6
- ConstructsMakerModuleTarget,
7
- Language,
8
- TerraformDependencyConstraint,
9
- TerraformModuleConstraint,
10
- TerraformTargetVersions,
11
- } from "@cdktn/commons";
12
- import { createTmpHelper } from "./util";
13
-
14
- const mockReadSchema = jest.fn();
15
- jest.mock("@cdktn/provider-schema", () => ({
16
- readSchema: (...args: unknown[]) => mockReadSchema(...args),
17
- }));
18
-
19
- // Imported after the mock so ConstructsMaker picks up the mocked readSchema.
20
- import { ConstructsMaker, GetOptions } from "../constructs-maker";
21
-
22
- const tmp = createTmpHelper();
23
-
24
- const constraint: TerraformDependencyConstraint = {
25
- fqn: "hashicorp/random",
26
- name: "random",
27
- source: "hashicorp/random",
28
- version: "3.1.3",
29
- };
30
-
31
- // Minimal schema - no resources/data sources - so generation stays within
32
- // pure TypeScript emission (no jsii-pacmak invocation), mirroring the
33
- // "empty-provider-resources" fixture used elsewhere in this package.
34
- const fakeSchema = {
35
- providerSchema: {
36
- format_version: "0.2" as const,
37
- provider_schemas: {
38
- "registry.terraform.io/hashicorp/random": {
39
- resource_schemas: null,
40
- data_source_schemas: null,
41
- },
42
- },
43
- provider_versions: {
44
- "registry.terraform.io/hashicorp/random": "3.1.3",
45
- },
46
- cli_name: "terraform",
47
- cli_version: "1.7.5",
48
- },
49
- };
50
-
51
- describe("targetVersions threading through 'cdktn get'", () => {
52
- beforeEach(() => {
53
- mockReadSchema.mockReset();
54
- mockReadSchema.mockResolvedValue(fakeSchema);
55
- });
56
-
57
- it("passes GetOptions.targetVersions through to readSchema", async () => {
58
- const workdir = tmp("target-versions-thread.test");
59
- const targetVersions: TerraformTargetVersions = { terraform: ">=1.5.7" };
60
- const options: GetOptions = {
61
- codeMakerOutput: workdir,
62
- targetLanguage: Language.TYPESCRIPT,
63
- targetVersions,
64
- };
65
-
66
- const maker = new ConstructsMaker(options, "/some/cache/path");
67
- await maker.generate([constraint]);
68
-
69
- expect(mockReadSchema).toHaveBeenCalledWith(
70
- [constraint],
71
- "/some/cache/path",
72
- targetVersions,
73
- );
74
- });
75
-
76
- it("omits targetVersions from the readSchema call when not configured", async () => {
77
- const workdir = tmp("target-versions-absent.test");
78
- const options: GetOptions = {
79
- codeMakerOutput: workdir,
80
- targetLanguage: Language.TYPESCRIPT,
81
- };
82
-
83
- const maker = new ConstructsMaker(options);
84
- await maker.generate([constraint]);
85
-
86
- expect(mockReadSchema).toHaveBeenCalledWith(
87
- [constraint],
88
- undefined,
89
- undefined,
90
- );
91
- });
92
-
93
- it("stamps constraints.json with targetVersions + cli identity, and older constraints.json files without those fields are still treated as fresh (no forced regeneration)", async () => {
94
- const workdir = tmp("target-versions-stamp.test");
95
- const targetVersions: TerraformTargetVersions = { terraform: ">=1.5.7" };
96
- const options: GetOptions = {
97
- codeMakerOutput: workdir,
98
- targetLanguage: Language.TYPESCRIPT,
99
- targetVersions,
100
- };
101
-
102
- const maker = new ConstructsMaker(options);
103
- await maker.generate([constraint]);
104
-
105
- const constraintsFile = JSON.parse(
106
- await fs.readFile(path.join(workdir, "constraints.json"), "utf8"),
107
- );
108
- expect(constraintsFile.targetVersions).toEqual(targetVersions);
109
- expect(constraintsFile.cli).toEqual({
110
- name: "terraform",
111
- version: "1.7.5",
112
- });
113
-
114
- // Rewrite constraints.json as it would have looked before this feature
115
- // landed (no targetVersions/cli fields), keeping the rest identical -
116
- // this simulates re-running `cdktn get` against an output directory
117
- // generated by an older cdktn version.
118
- const {
119
- targetVersions: _tv,
120
- cli: _cli,
121
- ...legacyConstraints
122
- } = constraintsFile;
123
- await fs.writeFile(
124
- path.join(workdir, "constraints.json"),
125
- JSON.stringify(legacyConstraints, null, 2),
126
- );
127
-
128
- const maker2 = new ConstructsMaker(options);
129
- const toGenerate = await maker2.filterAlreadyGenerated([constraint]);
130
- expect(toGenerate).toEqual([]);
131
- });
132
-
133
- it("threads a partial targetVersions (single product) through unchanged and stamps it verbatim", async () => {
134
- const workdir = tmp("target-versions-partial.test");
135
- const targetVersions: TerraformTargetVersions = { opentofu: ">=1.11.0" };
136
- const options: GetOptions = {
137
- codeMakerOutput: workdir,
138
- targetLanguage: Language.TYPESCRIPT,
139
- targetVersions,
140
- };
141
-
142
- const maker = new ConstructsMaker(options);
143
- await maker.generate([constraint]);
144
-
145
- expect(mockReadSchema).toHaveBeenCalledWith(
146
- [constraint],
147
- undefined,
148
- targetVersions,
149
- );
150
-
151
- // The stamp records exactly what the project declared - no terraform
152
- // default is injected at get-time (defaulting for the undeclared
153
- // product happens only at synth-time, in resolveTargetVersions).
154
- const constraintsFile = JSON.parse(
155
- await fs.readFile(path.join(workdir, "constraints.json"), "utf8"),
156
- );
157
- expect(constraintsFile.targetVersions).toEqual({ opentofu: ">=1.11.0" });
158
- });
159
-
160
- it("omits the cli stamp when the fetched schema carries no CLI version metadata", async () => {
161
- const workdir = tmp("target-versions-no-cli.test");
162
- const {
163
- cli_name: _cliName,
164
- cli_version: _cliVersion,
165
- ...schemaWithoutCli
166
- } = fakeSchema.providerSchema;
167
- mockReadSchema.mockResolvedValue({ providerSchema: schemaWithoutCli });
168
-
169
- const targetVersions: TerraformTargetVersions = { terraform: ">=1.5.7" };
170
- const options: GetOptions = {
171
- codeMakerOutput: workdir,
172
- targetLanguage: Language.TYPESCRIPT,
173
- targetVersions,
174
- };
175
-
176
- const maker = new ConstructsMaker(options);
177
- await maker.generate([constraint]);
178
-
179
- const constraintsFile = JSON.parse(
180
- await fs.readFile(path.join(workdir, "constraints.json"), "utf8"),
181
- );
182
- expect(constraintsFile.cli).toBeUndefined();
183
- expect(constraintsFile.targetVersions).toEqual(targetVersions);
184
- });
185
-
186
- it("does not force regeneration when targetVersions change between runs", async () => {
187
- // The stamp is diagnostic-only: per the GetOptions.targetVersions doc,
188
- // targetVersions never filters codegen (the full schema surface is
189
- // always generated), so freshness only compares the providers map and
190
- // the cdktf version.
191
- const workdir = tmp("target-versions-change.test");
192
- const options: GetOptions = {
193
- codeMakerOutput: workdir,
194
- targetLanguage: Language.TYPESCRIPT,
195
- targetVersions: { terraform: ">=1.5.7" },
196
- };
197
-
198
- const maker = new ConstructsMaker(options);
199
- await maker.generate([constraint]);
200
-
201
- const maker2 = new ConstructsMaker({
202
- ...options,
203
- targetVersions: { terraform: ">=1.11.0" },
204
- });
205
- const toGenerate = await maker2.filterAlreadyGenerated([constraint]);
206
- expect(toGenerate).toEqual([]);
207
- });
208
- });
209
-
210
- describe("targetVersions reaching generated module bindings", () => {
211
- const moduleConstraint = new TerraformModuleConstraint(
212
- "terraform-aws-modules/vpc/aws@3.19.0",
213
- );
214
-
215
- const emittedSources = (dir: string): string[] =>
216
- fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
217
- const full = path.join(dir, entry.name);
218
- if (entry.isDirectory()) return emittedSources(full);
219
- return entry.name.endsWith(".ts") ? [fs.readFileSync(full, "utf8")] : [];
220
- });
221
-
222
- beforeEach(() => {
223
- mockReadSchema.mockReset();
224
- const target = ConstructsMakerModuleTarget.from(
225
- moduleConstraint,
226
- Language.TYPESCRIPT,
227
- ) as ConstructsMakerModuleTarget;
228
- mockReadSchema.mockResolvedValue({
229
- moduleSchema: {
230
- [target.moduleKey]: { name: target.name, inputs: [], outputs: [] },
231
- },
232
- });
233
- });
234
-
235
- it("links an OpenTofu-only project's modules to the OpenTofu registry", async () => {
236
- const workdir = tmp("target-versions-module.test");
237
- const maker = new ConstructsMaker({
238
- codeMakerOutput: workdir,
239
- targetLanguage: Language.TYPESCRIPT,
240
- targetVersions: { opentofu: ">=1.6.0" },
241
- });
242
-
243
- await maker.generate([moduleConstraint]);
244
-
245
- expect(emittedSources(workdir).join("\n")).toContain(
246
- "https://search.opentofu.org/module/terraform-aws-modules/vpc/aws/v3.19.0",
247
- );
248
- });
249
- });
@@ -1,80 +0,0 @@
1
- // Copyright (c) HashiCorp, Inc
2
- // SPDX-License-Identifier: MPL-2.0
3
- import * as fs from "fs";
4
- import * as os from "os";
5
- import {
6
- Language,
7
- TerraformModuleConstraint,
8
- withTempDir,
9
- } from "@cdktn/commons";
10
- import { ConstructsMaker } from "../constructs-maker";
11
- import * as path from "path";
12
-
13
- /**
14
- * Creates a per-test-file temp-dir helper.
15
- *
16
- * Returns a `tmp(prefix)` function that creates a uniquely-named directory
17
- * under `os.tmpdir()` and tracks it. An `afterAll` hook is registered on
18
- * the calling test file's scope; when the file finishes running, every
19
- * tracked directory is removed (recursively, force).
20
- */
21
- export function createTmpHelper(): (prefix: string) => string {
22
- const dirs: string[] = [];
23
- afterAll(() => {
24
- for (const d of dirs) fs.rmSync(d, { recursive: true, force: true });
25
- });
26
- return (prefix: string) => {
27
- const d = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
28
- dirs.push(d);
29
- return d;
30
- };
31
- }
32
-
33
- export function expectModuleToMatchSnapshot(
34
- testName: string,
35
- testCategory: string,
36
- fixtureNames: string[],
37
- ) {
38
- test(testName, async () => {
39
- // Test names become a directory name, so collapse anything that is not
40
- // filename-safe. The previous /\s*/ matched the empty string at index 0
41
- // (no global flag either), which only prepended a "-" and left spaces --
42
- // and characters like "*" in a test title are illegal in Windows paths.
43
- const safeName = testName.replace(/[^a-zA-Z0-9._-]+/g, "-");
44
- await withTempDir(`${safeName}.test`, async () => {
45
- const curdir = process.cwd();
46
- fs.mkdirSync("module");
47
-
48
- fixtureNames.forEach((fixtureName) => {
49
- fs.copyFileSync(
50
- path.join(__dirname, testCategory, "fixtures", fixtureName),
51
- path.join(curdir, "module", fixtureName),
52
- );
53
- });
54
-
55
- const constraint = new TerraformModuleConstraint({
56
- source: "./module",
57
- name: "module",
58
- fqn: "module",
59
- });
60
-
61
- fs.mkdirSync("work");
62
- const workdir = path.join(curdir, "work");
63
-
64
- const maker = new ConstructsMaker(
65
- {
66
- codeMakerOutput: workdir,
67
- targetLanguage: Language.TYPESCRIPT,
68
- },
69
- process.env.CDKTF_EXPERIMENTAL_PROVIDER_SCHEMA_CACHE_PATH,
70
- );
71
- await maker.generate([constraint]);
72
-
73
- const output = fs.readFileSync(
74
- path.join(workdir, "modules/module.ts"),
75
- "utf-8",
76
- );
77
- expect(output).toMatchSnapshot();
78
- });
79
- });
80
- }