Skip to content

Commit

Permalink
fix(cli): fix bug that required cdktf.json in parent directory when r…
Browse files Browse the repository at this point in the history
…unning convert

`path.resolve(rootPath, ..)` will return rootPath if there's no more parent directory.
This caused an infinite loop ending in `RangeError: Maximum call stack size exceeded`.

Resolves #1247
  • Loading branch information
ansgarm committed Nov 4, 2021
1 parent 258d373 commit a6026e9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/cdktf-cli/bin/cmds/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ function findFileAboveCwd(
return fullPath;
}

if (fs.existsSync(path.resolve(rootPath, ".."))) {
return findFileAboveCwd(file, path.resolve(rootPath, ".."));
const parentDir = path.resolve(rootPath, "..");
if (fs.existsSync(parentDir) && parentDir !== rootPath) {
return findFileAboveCwd(file, parentDir);
}

return null;
Expand Down
68 changes: 68 additions & 0 deletions packages/cdktf-cli/test/cmds/convert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import execa from "execa";
import path from "path";
import { promises as fs } from "fs";
import { mkdtemp } from "../../lib/util";

const cdktfBin = path.resolve(__dirname, "../../bin/cdktf");
const input = `
resource "null_resource" "dummy" {}
`;

describe("convert command", () => {
it("proposes specifying a provider version", async () => {
await mkdtemp(async (cwd) => {
await fs.writeFile(
path.resolve(cwd, "cdktf.json"),
JSON.stringify({ terraformProviders: [] })
);
const child = execa(cdktfBin, ["convert"], { stdio: "pipe", cwd, input });
const result = await child;
expect(result.stderr).toEqual("");
expect(result.stdout).toContain(
`The following providers are missing schema information and might need manual adjustments to synthesize correctly: null.`
);
expect(result.stdout).toContain(
`import * as NullProvider from "./.gen/providers/null";`
);
expect(result.stdout).toContain(
`new NullProvider.Resource(this, "dummy", {});`
);
});
});
it("reads provider version from existing cdktf.json", async () => {
await mkdtemp(async (cwd) => {
await fs.writeFile(
path.resolve(cwd, "cdktf.json"),
JSON.stringify({ terraformProviders: ["hashicorp/null@~> 2.0"] })
);
const child = execa(cdktfBin, ["convert"], { stdio: "pipe", cwd, input });
const result = await child;
expect(result.stderr).toEqual("");
expect(result.stdout).not.toContain(
`The following providers are missing schema information and might need manual adjustments to synthesize correctly: null.`
);
expect(result.stdout).toContain(
`import * as NullProvider from "./.gen/providers/null";`
);
expect(result.stdout).toContain(
`new NullProvider.Resource(this, "dummy", {});`
);
});
});
it("works if no cdktf.json could be found", async () => {
await mkdtemp(async (cwd) => {
const child = execa(cdktfBin, ["convert"], { stdio: "pipe", cwd, input });
const result = await child;
expect(result.stderr).toEqual("");
expect(result.stdout).toContain(
`The following providers are missing schema information and might need manual adjustments to synthesize correctly: null.`
);
expect(result.stdout).toContain(
`import * as NullProvider from "./.gen/providers/null";`
);
expect(result.stdout).toContain(
`new NullProvider.Resource(this, "dummy", {});`
);
});
});
});

0 comments on commit a6026e9

Please sign in to comment.