Skip to content

Commit

Permalink
feat(lib): make asset paths change if asset content changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jun 29, 2021
1 parent 2050954 commit 7924925
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/cdktf/lib/terraform-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ export class TerraformAsset extends Resource {
public get path(): string {
return path.posix.join(
ASSETS_DIRECTORY,
this.stack.getLogicalId(Node.of(this)), // needed to get a human friendly, unique segment in the path
// readable name hash depending on content
this.stack.getLogicalId(Node.of(this)) + "-" + this.assetHash,
this.type === AssetType.DIRECTORY ? "" : this.fileName
);
}
Expand Down
4 changes: 2 additions & 2 deletions test/python/asset/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def __init__(self, scope: Construct, ns: str):
NullProvider(self, "null")
Resource(self, "null-resource")

fixture = TerraformAsset(self, 'fixture', path=os.path.abspath("./fixture.txt"))
fixtures = TerraformAsset(self, 'fixtures', path=os.path.abspath("./fixtures"), type=AssetType.ARCHIVE)
fixture = TerraformAsset(self, 'fixture', path=os.path.abspath("./fixture.txt"), assetHash="hash")
fixtures = TerraformAsset(self, 'fixtures', path=os.path.abspath("./fixtures"), assetHash="hash", type=AssetType.ARCHIVE)

TerraformOutput(self, 'fixture-output', value=fixture.path)
TerraformOutput(self, 'fixtures-output', value=fixtures.path)
Expand Down
3 changes: 3 additions & 0 deletions test/typescript/asset/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ export class HelloTerra extends TerraformStack {

const localAsset = new TerraformAsset(this, "local-asset", {
path: path.resolve(__dirname, "local-asset.txt"),
assetHash: "hash",
});

const fixtures = new TerraformAsset(this, "fixtures", {
path: path.resolve(__dirname, "fixtures"),
assetHash: "hash",
});

const zippedFixtures = new TerraformAsset(this, "zipped-fixtures", {
path: path.resolve(__dirname, "fixtures"),
assetHash: "hash",
type: AssetType.ARCHIVE,
});

Expand Down
10 changes: 5 additions & 5 deletions test/typescript/asset/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("full integration test", () => {
fs.readFileSync(
path.resolve(
driver.stackDirectory("stack"),
"assets/localasset/local-asset.txt"
"assets/localasset-hash/local-asset.txt"
),
"utf-8"
)
Expand All @@ -40,21 +40,21 @@ describe("full integration test", () => {
await driver.synth();
expect(
fs.readFileSync(
path.resolve(driver.stackDirectory("stack"), "assets/fixtures/a.txt"),
path.resolve(driver.stackDirectory("stack"), "assets/fixtures-hash/a.txt"),
"utf-8"
)
).toMatchSnapshot();
expect(
fs.readFileSync(
path.resolve(driver.stackDirectory("stack"), "assets/fixtures/b.txt"),
path.resolve(driver.stackDirectory("stack"), "assets/fixtures-hash/b.txt"),
"utf-8"
)
).toMatchSnapshot();
expect(
fs.readFileSync(
path.resolve(
driver.stackDirectory("stack"),
"assets/fixtures/foo/bar/c.txt"
"assets/fixtures-hash/foo/bar/c.txt"
),
"utf-8"
)
Expand All @@ -67,7 +67,7 @@ describe("full integration test", () => {
const stat = fs.statSync(
path.resolve(
driver.stackDirectory("stack"),
"assets/zippedfixtures/archive.zip"
"assets/zippedfixtures-hash/archive.zip"
)
);
expect(stat.isFile()).toBe(true);
Expand Down

0 comments on commit 7924925

Please sign in to comment.