@cdktn/provider-generator 0.24.0-pre.45 → 0.24.0-pre.48
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.
- package/LICENSE +355 -0
- package/README.md +1 -1
- package/build/__tests__/edge-provider-schema/builder.js +3 -3
- package/build/__tests__/edge-provider-schema/cli.js +15 -20
- package/build/__tests__/edge-provider-schema.test.js +8 -18
- package/build/__tests__/provider.test.js +8 -18
- package/build/get/__tests__/constructs-maker.test.js +8 -18
- package/build/get/__tests__/generator/complex-computed-types.test.js +8 -18
- package/build/get/__tests__/generator/deep-nested-attributes.test.js +8 -18
- package/build/get/__tests__/generator/description-escaping.test.js +8 -18
- package/build/get/__tests__/generator/empty-provider-resources.test.js +8 -18
- package/build/get/__tests__/generator/export-sharding.test.js +8 -18
- package/build/get/__tests__/generator/import-style.test.js +8 -18
- package/build/get/__tests__/generator/module-generator.test.js +8 -18
- package/build/get/__tests__/generator/nested-types.test.js +8 -18
- package/build/get/__tests__/generator/provider.test.js +8 -18
- package/build/get/__tests__/generator/resource-types.test.js +8 -18
- package/build/get/__tests__/generator/skipped-attributes.test.js +8 -18
- package/build/get/__tests__/generator/types.test.js +8 -18
- package/build/get/__tests__/generator/versions-file.test.js +8 -18
- package/build/get/__tests__/util.js +11 -20
- package/build/get/constructs-maker.js +19 -24
- package/build/get/generator/emitter/struct-emitter.js +8 -18
- package/build/get/generator/loop-detection.js +3 -2
- package/build/get/generator/models/attribute-model.js +3 -3
- package/build/get/generator/resource-parser.js +3 -3
- package/build/get/generator/sanitized-comments.js +3 -2
- package/build/get/generator/skipped-attributes.js +3 -2
- package/build/index.js +3 -3
- package/build/util.js +4 -3
- package/package.json +17 -15
- package/package.sh +1 -1
- package/src/__tests__/edge-provider-schema/builder.ts +185 -0
- package/src/__tests__/edge-provider-schema/cli.ts +51 -0
- package/src/__tests__/edge-provider-schema/index.ts +161 -0
- package/src/__tests__/edge-provider-schema.test.ts +24 -0
- package/src/__tests__/provider.test.ts +180 -0
- package/src/get/__tests__/constructs-maker.test.ts +118 -0
- package/src/get/__tests__/generator/complex-computed-types.test.ts +28 -0
- package/src/get/__tests__/generator/deep-nested-attributes.test.ts +56 -0
- package/src/get/__tests__/generator/description-escaping.test.ts +84 -0
- package/src/get/__tests__/generator/empty-provider-resources.test.ts +26 -0
- package/src/get/__tests__/generator/export-sharding.test.ts +169 -0
- package/src/get/__tests__/generator/import-style.test.ts +129 -0
- package/src/get/__tests__/generator/module-generator.test.ts +528 -0
- package/src/get/__tests__/generator/nested-types.test.ts +54 -0
- package/src/get/__tests__/generator/provider.test.ts +51 -0
- package/src/get/__tests__/generator/resource-types.test.ts +118 -0
- package/src/get/__tests__/generator/skipped-attributes.test.ts +72 -0
- package/src/get/__tests__/generator/types.test.ts +611 -0
- package/src/get/__tests__/generator/versions-file.test.ts +72 -0
- package/src/get/__tests__/util.ts +75 -0
- package/src/get/constructs-maker.ts +822 -0
- package/src/get/generator/custom-defaults.ts +493 -0
- package/src/get/generator/emitter/attributes-emitter.ts +225 -0
- package/src/get/generator/emitter/index.ts +5 -0
- package/src/get/generator/emitter/resource-emitter.ts +226 -0
- package/src/get/generator/emitter/struct-emitter.ts +683 -0
- package/src/get/generator/loop-detection.ts +81 -0
- package/src/get/generator/models/attribute-model.ts +216 -0
- package/src/get/generator/models/attribute-type-model.ts +448 -0
- package/src/get/generator/models/index.ts +7 -0
- package/src/get/generator/models/resource-model.ts +161 -0
- package/src/get/generator/models/scope.ts +54 -0
- package/src/get/generator/models/struct.ts +116 -0
- package/src/get/generator/module-generator.ts +234 -0
- package/src/get/generator/provider-generator.ts +355 -0
- package/src/get/generator/resource-parser.ts +762 -0
- package/src/get/generator/sanitized-comments.ts +49 -0
- package/src/get/generator/skipped-attributes.ts +27 -0
- package/src/index.ts +40 -0
- package/src/util.ts +26 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc
|
|
2
|
+
// SPDX-License-Identifier: MPL-2.0
|
|
3
|
+
import * as fs from "fs-extra";
|
|
4
|
+
import * as os from "os";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import {
|
|
7
|
+
Language,
|
|
8
|
+
ConstructsMakerModuleTarget,
|
|
9
|
+
ConstructsMakerProviderTarget,
|
|
10
|
+
} from "@cdktn/commons";
|
|
11
|
+
import { determineGoModuleName } from "../constructs-maker";
|
|
12
|
+
|
|
13
|
+
describe("constructsMaker", () => {
|
|
14
|
+
describe("determineGoModuleName", () => {
|
|
15
|
+
let tmpDir: string;
|
|
16
|
+
let emptySubDir: string, validSubdir: string, invalidSubdir: string;
|
|
17
|
+
|
|
18
|
+
beforeAll(async () => {
|
|
19
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "cdktf.test"));
|
|
20
|
+
/* test directory layout
|
|
21
|
+
* <tmpDir>
|
|
22
|
+
* ├── go.mod
|
|
23
|
+
* └── subdir
|
|
24
|
+
* ├── empty
|
|
25
|
+
* ├── valid
|
|
26
|
+
* │ └── go.mod
|
|
27
|
+
* └── invalid
|
|
28
|
+
* └── go.mod
|
|
29
|
+
*/
|
|
30
|
+
emptySubDir = path.join(tmpDir, "subdir", "empty");
|
|
31
|
+
validSubdir = path.join(tmpDir, "subdir", "valid");
|
|
32
|
+
invalidSubdir = path.join(tmpDir, "subdir", "invalid");
|
|
33
|
+
|
|
34
|
+
await fs.mkdirs(emptySubDir);
|
|
35
|
+
await fs.mkdirs(validSubdir);
|
|
36
|
+
await fs.mkdirs(invalidSubdir);
|
|
37
|
+
|
|
38
|
+
const root = `module cdk.tf/test/go`;
|
|
39
|
+
const valid = `module cdk.tf/test/go-valid-subdir`;
|
|
40
|
+
const invalid = `malformed go mod`;
|
|
41
|
+
|
|
42
|
+
await fs.writeFile(path.join(tmpDir, "go.mod"), root);
|
|
43
|
+
await fs.writeFile(path.join(validSubdir, "go.mod"), valid);
|
|
44
|
+
await fs.writeFile(path.join(invalidSubdir, "go.mod"), invalid);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
afterAll(async () => {
|
|
48
|
+
await fs.remove(tmpDir);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("works in root directory", async () => {
|
|
52
|
+
const moduleName = await determineGoModuleName(tmpDir);
|
|
53
|
+
expect(moduleName).toBe("cdk.tf/test/go");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("can walk upwards from empty directory", async () => {
|
|
57
|
+
const moduleName = await determineGoModuleName(emptySubDir);
|
|
58
|
+
expect(moduleName).toBe("cdk.tf/test/go/subdir/empty");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("works in subdirectory with go.mod", async () => {
|
|
62
|
+
const moduleName = await determineGoModuleName(validSubdir);
|
|
63
|
+
expect(moduleName).toBe("cdk.tf/test/go-valid-subdir");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("throws if go.mod is invalid", async () => {
|
|
67
|
+
await expect(determineGoModuleName(invalidSubdir)).rejects.toThrow(
|
|
68
|
+
`Could not determine the root Go module name. Found ${path.join(
|
|
69
|
+
invalidSubdir,
|
|
70
|
+
"go.mod",
|
|
71
|
+
)} but failed to regex match the module name directive`,
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("works from subdirectory that does not exist yet", async () => {
|
|
76
|
+
const moduleName = await determineGoModuleName(
|
|
77
|
+
path.join(tmpDir, "subdir", "that", "does", "not", "exist", "yet"),
|
|
78
|
+
);
|
|
79
|
+
expect(moduleName).toBe("cdk.tf/test/go/subdir/that/does/not/exist/yet");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("throws if nothing could be found", async () => {
|
|
83
|
+
const dir = "/cdktf-test/this/dir/does/not/exist"; //... and has no go.mod in any parent dir
|
|
84
|
+
await expect(determineGoModuleName(dir)).rejects.toThrow(
|
|
85
|
+
`Could not determine the root Go module name. No go.mod found in ${dir} and any parent directories`,
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe("ConstructsMakerProviderTarget", () => {
|
|
90
|
+
it("returns valid package name for Go", () => {
|
|
91
|
+
const target = new ConstructsMakerProviderTarget(
|
|
92
|
+
{
|
|
93
|
+
name: "google-beta",
|
|
94
|
+
fqn: "google-beta",
|
|
95
|
+
source: "google-beta",
|
|
96
|
+
version: "~> 4.0",
|
|
97
|
+
},
|
|
98
|
+
Language.GO,
|
|
99
|
+
);
|
|
100
|
+
expect(target.srcMakName).toEqual("google_beta");
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
describe("ConstructsMakerModuleTarget", () => {
|
|
104
|
+
it("returns valid package name for Go", () => {
|
|
105
|
+
const target = new ConstructsMakerModuleTarget(
|
|
106
|
+
{
|
|
107
|
+
name: "security-group",
|
|
108
|
+
source: "terraform-aws-modules/security-group/aws",
|
|
109
|
+
fqn: "terraform-aws-modules/security-group/aws",
|
|
110
|
+
version: "4.9.0",
|
|
111
|
+
namespace: "terraform-aws-modules/aws",
|
|
112
|
+
},
|
|
113
|
+
Language.GO,
|
|
114
|
+
);
|
|
115
|
+
expect(target.srcMakName).toEqual("security_group");
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
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 an acm certifacte resource with complex computed types", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("complex-computed-types.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(__dirname, "fixtures", "aws_acm_certificate.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/aws/acm-certificate/index.ts"),
|
|
25
|
+
"utf-8",
|
|
26
|
+
);
|
|
27
|
+
expect(output).toMatchSnapshot(`acm-certificate`);
|
|
28
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Copyright (c) HashiCorp, Inc // SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
import * as fs from "fs";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { TerraformProviderGenerator } from "../../generator/provider-generator";
|
|
5
|
+
import { CodeMaker } from "codemaker";
|
|
6
|
+
import { createTmpHelper } from "../util";
|
|
7
|
+
|
|
8
|
+
const tmp = createTmpHelper();
|
|
9
|
+
|
|
10
|
+
test("generate a resource with attribute that's a list of a map of an object", async () => {
|
|
11
|
+
const code = new CodeMaker();
|
|
12
|
+
const workdir = tmp("deep-nested-attributes.test");
|
|
13
|
+
const spec = JSON.parse(
|
|
14
|
+
fs.readFileSync(
|
|
15
|
+
path.join(__dirname, "fixtures", "stripe-schema.test.fixture.json"),
|
|
16
|
+
"utf-8",
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
20
|
+
await code.save(workdir);
|
|
21
|
+
|
|
22
|
+
const output = fs.readFileSync(
|
|
23
|
+
path.join(workdir, "providers/stripe/invoiceitems/index.ts"),
|
|
24
|
+
"utf-8",
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
expect(output).toEqual(
|
|
28
|
+
expect.stringContaining(
|
|
29
|
+
`export class InvoiceitemsDiscountsMapList extends cdktn.MapList {`,
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("generate a resource with attribute that's a map of a list of an object", async () => {
|
|
35
|
+
const code = new CodeMaker();
|
|
36
|
+
const workdir = tmp("deep-nested-attributes-list.test");
|
|
37
|
+
const spec = JSON.parse(
|
|
38
|
+
fs.readFileSync(
|
|
39
|
+
path.join(__dirname, "fixtures", "deep-attributes.test.fixture.json"),
|
|
40
|
+
"utf-8",
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
44
|
+
await code.save(workdir);
|
|
45
|
+
|
|
46
|
+
const output = fs.readFileSync(
|
|
47
|
+
path.join(workdir, "providers/stripe/invoiceitems/index.ts"),
|
|
48
|
+
"utf-8",
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(output).toEqual(
|
|
52
|
+
expect.stringContaining(
|
|
53
|
+
`export class InvoiceitemsDiscountsListMap extends cdktn.ComplexMap {`,
|
|
54
|
+
),
|
|
55
|
+
);
|
|
56
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
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("broken attribute description comments", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("description-escaping.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(
|
|
17
|
+
__dirname,
|
|
18
|
+
"fixtures",
|
|
19
|
+
"description-escaping.test.fixture.json",
|
|
20
|
+
),
|
|
21
|
+
"utf-8",
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
25
|
+
await code.save(workdir);
|
|
26
|
+
|
|
27
|
+
const output = fs.readFileSync(
|
|
28
|
+
path.join(workdir, "providers/google/description-escaping/index.ts"),
|
|
29
|
+
"utf-8",
|
|
30
|
+
);
|
|
31
|
+
expect(output).toMatchSnapshot();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// That popped up in the google-beta provider which transforms markdown fenced
|
|
35
|
+
// code blocks from the actual markdown docs to the provider schema (``` => ''')
|
|
36
|
+
// see
|
|
37
|
+
// - https://raw.githubusercontent.com/hashicorp/terraform-provider-google-beta/4094e3bdd530ca853a046a6d938807f8a131c7c7/website/docs/r/iam_workload_identity_pool_provider.html.markdown
|
|
38
|
+
// - https://github.com/hashicorp/terraform-provider-google-beta/blob/4094e3bdd530ca853a046a6d938807f8a131c7c7/google-beta/resource_iam_beta_workload_identity_pool_provider.go
|
|
39
|
+
|
|
40
|
+
test("malformed code blocks which break in python rst", async () => {
|
|
41
|
+
const code = new CodeMaker();
|
|
42
|
+
const workdir = tmp("markdown-description-with-code-blocks.test");
|
|
43
|
+
const spec = JSON.parse(
|
|
44
|
+
fs.readFileSync(
|
|
45
|
+
path.join(
|
|
46
|
+
__dirname,
|
|
47
|
+
"fixtures",
|
|
48
|
+
"markdown-description-with-code-blocks.test.fixture.json",
|
|
49
|
+
),
|
|
50
|
+
"utf-8",
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
54
|
+
await code.save(workdir);
|
|
55
|
+
|
|
56
|
+
const output = fs.readFileSync(
|
|
57
|
+
path.join(workdir, "providers/aws/code-blocks/index.ts"),
|
|
58
|
+
"utf-8",
|
|
59
|
+
);
|
|
60
|
+
expect(output).toMatchSnapshot();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("comment ending sequence in comment", async () => {
|
|
64
|
+
const code = new CodeMaker();
|
|
65
|
+
const workdir = tmp("comment-ending-sequence.test");
|
|
66
|
+
const spec = JSON.parse(
|
|
67
|
+
fs.readFileSync(
|
|
68
|
+
path.join(
|
|
69
|
+
__dirname,
|
|
70
|
+
"fixtures",
|
|
71
|
+
"comment-ending-sequence.test.fixture.json",
|
|
72
|
+
),
|
|
73
|
+
"utf-8",
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
77
|
+
await code.save(workdir);
|
|
78
|
+
|
|
79
|
+
const output = fs.readFileSync(
|
|
80
|
+
path.join(workdir, "providers/aws/code-blocks/index.ts"),
|
|
81
|
+
"utf-8",
|
|
82
|
+
);
|
|
83
|
+
expect(output).toMatchSnapshot();
|
|
84
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
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("provider with no resources", async () => {
|
|
12
|
+
const code = new CodeMaker();
|
|
13
|
+
const workdir = tmp("empty-provider-resources.test");
|
|
14
|
+
const spec = JSON.parse(
|
|
15
|
+
fs.readFileSync(
|
|
16
|
+
path.join(
|
|
17
|
+
__dirname,
|
|
18
|
+
"fixtures",
|
|
19
|
+
"empty-provider-resources.test.fixture.json",
|
|
20
|
+
),
|
|
21
|
+
"utf-8",
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
25
|
+
await code.save(workdir);
|
|
26
|
+
});
|
|
@@ -0,0 +1,169 @@
|
|
|
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
|
+
// this is a workaround for a bug introduced in TS 3.9.x and seems to be unlikely to get fixed.
|
|
12
|
+
// Since jsii relies on TS < 4 at the moment we can't use newer TS versions which have this fixed (^4.1).
|
|
13
|
+
// see https://github.com/hashicorp/terraform-cdk/pull/1248 for more context
|
|
14
|
+
test("shard exports across multiple files to avoid generating files with more than a 1000 exports", async () => {
|
|
15
|
+
const code = new CodeMaker();
|
|
16
|
+
const workdir = tmp("export-sharding.test");
|
|
17
|
+
|
|
18
|
+
const spec = JSON.parse(
|
|
19
|
+
fs.readFileSync(
|
|
20
|
+
path.join(__dirname, "fixtures", "aws_wafv2_web_acl.test.fixture.json"),
|
|
21
|
+
"utf-8",
|
|
22
|
+
),
|
|
23
|
+
);
|
|
24
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
25
|
+
await code.save(workdir);
|
|
26
|
+
|
|
27
|
+
const output = fs.readFileSync(
|
|
28
|
+
path.join(workdir, "providers/test/wafv2-web-acl/index.ts"),
|
|
29
|
+
"utf-8",
|
|
30
|
+
);
|
|
31
|
+
expect(output).toMatchSnapshot(`wafv2-web-acl-resource`);
|
|
32
|
+
|
|
33
|
+
const outputStructsIndex = fs.readFileSync(
|
|
34
|
+
path.join(workdir, "providers/test/wafv2-web-acl/index-structs/index.ts"),
|
|
35
|
+
"utf-8",
|
|
36
|
+
);
|
|
37
|
+
expect(outputStructsIndex).toMatchSnapshot(`structs-index`);
|
|
38
|
+
|
|
39
|
+
const outputStructs0 = fs.readFileSync(
|
|
40
|
+
path.join(
|
|
41
|
+
workdir,
|
|
42
|
+
"providers/test/wafv2-web-acl/index-structs/structs0.ts",
|
|
43
|
+
),
|
|
44
|
+
"utf-8",
|
|
45
|
+
);
|
|
46
|
+
expect(outputStructs0).toMatchSnapshot(`structs0`);
|
|
47
|
+
|
|
48
|
+
const outputStructs400 = fs.readFileSync(
|
|
49
|
+
path.join(
|
|
50
|
+
workdir,
|
|
51
|
+
"providers/test/wafv2-web-acl/index-structs/structs400.ts",
|
|
52
|
+
),
|
|
53
|
+
"utf-8",
|
|
54
|
+
);
|
|
55
|
+
expect(outputStructs400).toMatchSnapshot(`structs400`);
|
|
56
|
+
|
|
57
|
+
const outputStructs800 = fs.readFileSync(
|
|
58
|
+
path.join(
|
|
59
|
+
workdir,
|
|
60
|
+
"providers/test/wafv2-web-acl/index-structs/structs800.ts",
|
|
61
|
+
),
|
|
62
|
+
"utf-8",
|
|
63
|
+
);
|
|
64
|
+
expect(outputStructs800).toMatchSnapshot(`structs800`);
|
|
65
|
+
|
|
66
|
+
const outputStructs1200 = fs.readFileSync(
|
|
67
|
+
path.join(
|
|
68
|
+
workdir,
|
|
69
|
+
"providers/test/wafv2-web-acl/index-structs/structs1200.ts",
|
|
70
|
+
),
|
|
71
|
+
"utf-8",
|
|
72
|
+
);
|
|
73
|
+
expect(outputStructs1200).toMatchSnapshot(`structs1200`);
|
|
74
|
+
|
|
75
|
+
const outputStructs1600 = fs.readFileSync(
|
|
76
|
+
path.join(
|
|
77
|
+
workdir,
|
|
78
|
+
"providers/test/wafv2-web-acl/index-structs/structs1600.ts",
|
|
79
|
+
),
|
|
80
|
+
"utf-8",
|
|
81
|
+
);
|
|
82
|
+
expect(outputStructs1600).toMatchSnapshot(`structs1600`);
|
|
83
|
+
|
|
84
|
+
const outputStructs2000 = fs.readFileSync(
|
|
85
|
+
path.join(
|
|
86
|
+
workdir,
|
|
87
|
+
"providers/test/wafv2-web-acl/index-structs/structs2000.ts",
|
|
88
|
+
),
|
|
89
|
+
"utf-8",
|
|
90
|
+
);
|
|
91
|
+
expect(outputStructs2000).toMatchSnapshot(`structs2000`);
|
|
92
|
+
|
|
93
|
+
const outputStructs2400 = fs.readFileSync(
|
|
94
|
+
path.join(
|
|
95
|
+
workdir,
|
|
96
|
+
"providers/test/wafv2-web-acl/index-structs/structs2400.ts",
|
|
97
|
+
),
|
|
98
|
+
"utf-8",
|
|
99
|
+
);
|
|
100
|
+
expect(outputStructs2400).toMatchSnapshot(`structs2400`);
|
|
101
|
+
|
|
102
|
+
const outputStructs2800 = fs.readFileSync(
|
|
103
|
+
path.join(
|
|
104
|
+
workdir,
|
|
105
|
+
"providers/test/wafv2-web-acl/index-structs/structs2800.ts",
|
|
106
|
+
),
|
|
107
|
+
"utf-8",
|
|
108
|
+
);
|
|
109
|
+
expect(outputStructs2800).toMatchSnapshot(`structs2800`);
|
|
110
|
+
|
|
111
|
+
const outputStructs3200 = fs.readFileSync(
|
|
112
|
+
path.join(
|
|
113
|
+
workdir,
|
|
114
|
+
"providers/test/wafv2-web-acl/index-structs/structs3200.ts",
|
|
115
|
+
),
|
|
116
|
+
"utf-8",
|
|
117
|
+
);
|
|
118
|
+
expect(outputStructs3200).toMatchSnapshot(`structs3200`);
|
|
119
|
+
|
|
120
|
+
const outputStructs3600 = fs.readFileSync(
|
|
121
|
+
path.join(
|
|
122
|
+
workdir,
|
|
123
|
+
"providers/test/wafv2-web-acl/index-structs/structs3600.ts",
|
|
124
|
+
),
|
|
125
|
+
"utf-8",
|
|
126
|
+
);
|
|
127
|
+
expect(outputStructs3600).toMatchSnapshot(`structs3600`);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("shard exports across multiple files to avoid generating files with more than a 1000 exports in a provider without namespaces", async () => {
|
|
131
|
+
const code = new CodeMaker();
|
|
132
|
+
const workdir = tmp("export-sharding-no-namespace.test");
|
|
133
|
+
|
|
134
|
+
const spec = JSON.parse(
|
|
135
|
+
fs.readFileSync(
|
|
136
|
+
path.join(__dirname, "fixtures", "datadog_dashboard.test.fixture.json"),
|
|
137
|
+
"utf-8",
|
|
138
|
+
),
|
|
139
|
+
);
|
|
140
|
+
new TerraformProviderGenerator(code, spec).generateAll();
|
|
141
|
+
await code.save(workdir);
|
|
142
|
+
|
|
143
|
+
const output = fs.readFileSync(
|
|
144
|
+
path.join(workdir, "providers/datadog/dashboard/index.ts"),
|
|
145
|
+
"utf-8",
|
|
146
|
+
);
|
|
147
|
+
expect(output).toMatchSnapshot(`dashboard-resource`);
|
|
148
|
+
|
|
149
|
+
const outputStructsIndex = fs.readFileSync(
|
|
150
|
+
path.join(workdir, "providers/datadog/dashboard/index-structs/index.ts"),
|
|
151
|
+
"utf-8",
|
|
152
|
+
);
|
|
153
|
+
expect(outputStructsIndex).toMatchSnapshot(`structs-index`);
|
|
154
|
+
|
|
155
|
+
const outputStructs0 = fs.readFileSync(
|
|
156
|
+
path.join(workdir, "providers/datadog/dashboard/index-structs/structs0.ts"),
|
|
157
|
+
"utf-8",
|
|
158
|
+
);
|
|
159
|
+
expect(outputStructs0).toMatchSnapshot(`structs0`);
|
|
160
|
+
|
|
161
|
+
const outputStructs400 = fs.readFileSync(
|
|
162
|
+
path.join(
|
|
163
|
+
workdir,
|
|
164
|
+
"providers/datadog/dashboard/index-structs/structs400.ts",
|
|
165
|
+
),
|
|
166
|
+
"utf-8",
|
|
167
|
+
);
|
|
168
|
+
expect(outputStructs400).toMatchSnapshot(`structs400`);
|
|
169
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
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 * as path from "path";
|
|
6
|
+
import { CodeMaker } from "codemaker";
|
|
7
|
+
import {
|
|
8
|
+
TerraformProviderGenerator,
|
|
9
|
+
type TerraformProviderGeneratorOptions,
|
|
10
|
+
} from "../../generator/provider-generator";
|
|
11
|
+
|
|
12
|
+
const fixture = JSON.parse(
|
|
13
|
+
fs.readFileSync(
|
|
14
|
+
path.join(__dirname, "fixtures", "aws_wafv2_web_acl.test.fixture.json"),
|
|
15
|
+
"utf-8",
|
|
16
|
+
),
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
async function generate(options: TerraformProviderGeneratorOptions = {}) {
|
|
20
|
+
const code = new CodeMaker();
|
|
21
|
+
const workdir = fs.mkdtempSync(path.join(os.tmpdir(), "import-style.test"));
|
|
22
|
+
new TerraformProviderGenerator(code, fixture, options).generateAll();
|
|
23
|
+
await code.save(workdir);
|
|
24
|
+
return workdir;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const RESOURCE_INDEX = "providers/test/wafv2-web-acl/index.ts";
|
|
28
|
+
const STRUCTS_INDEX = "providers/test/wafv2-web-acl/index-structs/index.ts";
|
|
29
|
+
const STRUCTS_DIR = "providers/test/wafv2-web-acl/index-structs";
|
|
30
|
+
const PROVIDER_INDEX = "providers/test/index.ts";
|
|
31
|
+
|
|
32
|
+
function readAllShards(workdir: string): string {
|
|
33
|
+
return fs
|
|
34
|
+
.readdirSync(path.join(workdir, STRUCTS_DIR))
|
|
35
|
+
.filter((f) => /^structs\d+\.ts$/.test(f))
|
|
36
|
+
.map((f) => fs.readFileSync(path.join(workdir, STRUCTS_DIR, f), "utf-8"))
|
|
37
|
+
.join("\n");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
test("default (no importExtension): emits explicit /index for folders, bare for files", async () => {
|
|
41
|
+
const workdir = await generate();
|
|
42
|
+
|
|
43
|
+
const resource = fs.readFileSync(path.join(workdir, RESOURCE_INDEX), "utf-8");
|
|
44
|
+
expect(resource).toContain(`} from './index-structs/index'`);
|
|
45
|
+
expect(resource).toContain(`export * from './index-structs/index'`);
|
|
46
|
+
expect(resource).not.toMatch(/from '\.\/[^']*\.(js|ts)'/);
|
|
47
|
+
|
|
48
|
+
const structsIndex = fs.readFileSync(
|
|
49
|
+
path.join(workdir, STRUCTS_INDEX),
|
|
50
|
+
"utf-8",
|
|
51
|
+
);
|
|
52
|
+
expect(structsIndex).toContain(`export * from './structs0'`);
|
|
53
|
+
expect(structsIndex).not.toMatch(/from '\.\/[^']*\.(js|ts)'/);
|
|
54
|
+
|
|
55
|
+
const providerIndex = fs.readFileSync(
|
|
56
|
+
path.join(workdir, PROVIDER_INDEX),
|
|
57
|
+
"utf-8",
|
|
58
|
+
);
|
|
59
|
+
expect(providerIndex).toMatch(/from '\.\/[^']+\/index';/);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("importExtension '.js': emits fully-qualified ./<folder>/index.js and ./<file>.js", async () => {
|
|
63
|
+
const workdir = await generate({ importExtension: ".js" });
|
|
64
|
+
|
|
65
|
+
const resource = fs.readFileSync(path.join(workdir, RESOURCE_INDEX), "utf-8");
|
|
66
|
+
expect(resource).toContain(`} from './index-structs/index.js'`);
|
|
67
|
+
expect(resource).toContain(`export * from './index-structs/index.js'`);
|
|
68
|
+
|
|
69
|
+
const structsIndex = fs.readFileSync(
|
|
70
|
+
path.join(workdir, STRUCTS_INDEX),
|
|
71
|
+
"utf-8",
|
|
72
|
+
);
|
|
73
|
+
expect(structsIndex).toContain(`export * from './structs0.js'`);
|
|
74
|
+
|
|
75
|
+
const allShards = readAllShards(workdir);
|
|
76
|
+
expect(allShards).toMatch(/from '\.\/structs\d+\.js'/);
|
|
77
|
+
|
|
78
|
+
const providerIndex = fs.readFileSync(
|
|
79
|
+
path.join(workdir, PROVIDER_INDEX),
|
|
80
|
+
"utf-8",
|
|
81
|
+
);
|
|
82
|
+
expect(providerIndex).toMatch(/from '\.\/[^']+\/index\.js';/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("importExtension '.ts': emits fully-qualified ./<folder>/index.ts and ./<file>.ts", async () => {
|
|
86
|
+
const workdir = await generate({ importExtension: ".ts" });
|
|
87
|
+
|
|
88
|
+
const resource = fs.readFileSync(path.join(workdir, RESOURCE_INDEX), "utf-8");
|
|
89
|
+
expect(resource).toContain(`} from './index-structs/index.ts'`);
|
|
90
|
+
expect(resource).toContain(`export * from './index-structs/index.ts'`);
|
|
91
|
+
|
|
92
|
+
const structsIndex = fs.readFileSync(
|
|
93
|
+
path.join(workdir, STRUCTS_INDEX),
|
|
94
|
+
"utf-8",
|
|
95
|
+
);
|
|
96
|
+
expect(structsIndex).toContain(`export * from './structs0.ts'`);
|
|
97
|
+
|
|
98
|
+
const allShards = readAllShards(workdir);
|
|
99
|
+
expect(allShards).toMatch(/from '\.\/structs\d+\.ts'/);
|
|
100
|
+
|
|
101
|
+
const providerIndex = fs.readFileSync(
|
|
102
|
+
path.join(workdir, PROVIDER_INDEX),
|
|
103
|
+
"utf-8",
|
|
104
|
+
);
|
|
105
|
+
expect(providerIndex).toMatch(/from '\.\/[^']+\/index\.ts';/);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("importExtension '': emits explicit /index for folders, bare for files", async () => {
|
|
109
|
+
const workdir = await generate({ importExtension: "" });
|
|
110
|
+
|
|
111
|
+
const resource = fs.readFileSync(path.join(workdir, RESOURCE_INDEX), "utf-8");
|
|
112
|
+
expect(resource).toContain(`} from './index-structs/index'`);
|
|
113
|
+
expect(resource).toContain(`export * from './index-structs/index'`);
|
|
114
|
+
expect(resource).not.toMatch(/from '\.\/[^']*\.(js|ts)'/);
|
|
115
|
+
|
|
116
|
+
const structsIndex = fs.readFileSync(
|
|
117
|
+
path.join(workdir, STRUCTS_INDEX),
|
|
118
|
+
"utf-8",
|
|
119
|
+
);
|
|
120
|
+
// file targets get the (empty) extension, so they stay bare
|
|
121
|
+
expect(structsIndex).toContain(`export * from './structs0'`);
|
|
122
|
+
expect(structsIndex).not.toMatch(/from '\.\/[^']*\.(js|ts)'/);
|
|
123
|
+
|
|
124
|
+
const providerIndex = fs.readFileSync(
|
|
125
|
+
path.join(workdir, PROVIDER_INDEX),
|
|
126
|
+
"utf-8",
|
|
127
|
+
);
|
|
128
|
+
expect(providerIndex).toMatch(/from '\.\/[^']+\/index';/);
|
|
129
|
+
});
|