// Copyright (c) HashiCorp, Inc // SPDX-License-Identifier: MPL-2.0 import { replaceCsharpImports, replaceGoImports, replaceJavaImports, replacePythonImports, } from "../jsii-rosetta-workarounds"; describe("jsii-rosetta-workarounds", () => { describe("replacePythonImports", () => { it("normal imports", () => { const code = ` import constructs as constructs # Provider bindings are generated by running cdktn get. # See https://cdktn.io/docs/concepts/providers#import-providers for more details. import ...gen.providers.scaleway as scaleway class MyConvertedCode(constructs.Construct): def __init__(self, scope, name): super().__init__(scope, name) scaleway.provider.ScalewayProvider(self, "scaleway", region="fr-par", zone="fr-par-1" )`; expect(replacePythonImports(code)).toEqual( expect.stringContaining("import imports.scaleway as scaleway"), ); }); it("granular imports", () => { const code = `from ...gen.providers.aws.lib.rdsCluster import RdsCluster`; expect(replacePythonImports(code)).toEqual( "from imports.aws.rdsCluster import RdsCluster", ); const withoutLibCode = `from ...gen.providers.azurerm.resource_group import ResourceGroup`; expect(replacePythonImports(withoutLibCode)).toEqual( "from imports.azurerm.resource_group import ResourceGroup", ); }); it("resource imports", () => { const code = `import ...gen.providers.aws.lib.rdsCluster as RdsCluster`; expect(replacePythonImports(code)).toEqual( "import imports.aws.rdsCluster as RdsCluster", ); }); it("fixes module imports", () => { const code = `import ...gen.modules.hello_module as HelloModule`; expect(replacePythonImports(code)).toEqual( "import imports.hello_module as HelloModule", ); }); }); describe("replaceCsharpImports", () => { it("normal imports", () => { const code = `using Gen.Providers.Aws.Lib.RdsCluster;`; expect(replaceCsharpImports(code)).toEqual("using aws.RdsCluster;"); }); it("fixes module imports", () => { const code = `using Gen.Modules.HelloModule`; expect(replaceCsharpImports(code)).toEqual("using HelloModule"); const anotherCode = `using Gen.Modules.Hello.Module`; expect(replaceCsharpImports(anotherCode)).toEqual("using Hello.Module"); }); }); describe("replaceJavaImports", () => { it("normal imports", () => { const code = `import gen.providers.scaleway.objectResource.*;`; expect(replaceJavaImports(code)).toEqual( "import imports.scaleway.objectResource.*;", ); }); it("precise imports", () => { const code = `import gen.providers.aws.lib.s3Bucket.S3Bucket;`; expect(replaceJavaImports(code)).toEqual( "import imports.aws.s3Bucket.S3Bucket;", ); const withoutLibImport = `import gen.providers.aws.s3Bucket.S3Bucket;`; expect(replaceJavaImports(withoutLibImport)).toEqual( "import imports.aws.s3Bucket.S3Bucket;", ); }); it("fixes module imports", () => { const code = `import gen.modules.hello.module.*;`; expect(replaceJavaImports(code)).toEqual( "import imports.hello.module.*;", ); const anotherCode = `import gen.modules.helloModule.*;`; expect(replaceJavaImports(anotherCode)).toEqual( "import imports.helloModule.*;", ); }); }); describe("replaceGoImports", () => { it("normal imports", () => { const code = `import "github.com/aws-samples/dummy/gen/providers/scaleway/objectBucket"`; expect(replaceGoImports(code)).toEqual( `import "cdk.tf/go/stack/generated/scaleway/objectBucket"`, ); }); it("precise imports", () => { const code = `import "github.com/aws-samples/dummy/gen/providers/aws/lib/dbInstance"`; expect(replaceGoImports(code)).toEqual( `import "cdk.tf/go/stack/generated/aws/dbInstance"`, ); }); it("fixes module imports", () => { const code = `import helloModule "github.com/aws-samples/dummy/gen/modules/hello_module"`; expect(replaceGoImports(code)).toEqual( // TODO: Set up cdktn.io/ static redirect? // "github.com/open-constructs/cdk-terrain/examples/go/documentation/generated/terraform-aws-modules/aws/vpc" `import helloModule "cdk.tf/go/stack/generated/hello_module"`, ); }); }); });