Skip to content

Commit b48daf9

Browse files
committed
fix ~ edge cases and cover in tests
1 parent 822cf15 commit b48daf9

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

registry/coder/modules/archive/main.test.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ describe("archive", () => {
159159

160160
const { id } = await installArchive(state);
161161

162+
const createTestdata = await bashRun(
163+
id,
164+
`mkdir ~/gzip; touch ~/gzip/defaults.txt`,
165+
);
166+
ensureRunOk("create testdata", createTestdata);
167+
162168
const run = await bashRun(id, `${BIN_DIR}/coder-archive-create`);
163169
ensureRunOk("archive-create default run", run);
164170

@@ -172,35 +178,39 @@ describe("archive", () => {
172178

173179
const list = await listTar(id, "/tmp/coder-archive.tar.gz");
174180
ensureRunOk("list default archive", list);
175-
176-
// We don't assert specific entries to avoid environment coupling.
177-
expect(list.stdout.length).toBeGreaterThan(0);
181+
expect(list.stdout).toContain("gzip/defaults.txt");
178182
}, 20000);
179183

180184
it("creates a gzip archive with explicit -f and includes extra CLI paths", async () => {
181185
const state = await runTerraformApply(import.meta.dir, {
182186
agent_id: "agent-123",
183187
// Provide a simple default path so we can assert contents.
184-
paths: `["/etc/hostname"]`,
188+
paths: `["~/gzip"]`,
185189
compression: "gzip",
186190
});
187191

188192
const { id } = await installArchive(state);
189193

194+
const createTestdata = await bashRun(
195+
id,
196+
`mkdir ~/gzip; touch ~/gzip/test.txt; touch ~/gziptest.txt`,
197+
);
198+
ensureRunOk("create testdata", createTestdata);
199+
190200
const out = "/tmp/backup/test-archive.tar.gz";
191201
const run = await bashRun(
192202
id,
193-
`${BIN_DIR}/coder-archive-create -f ${out} /etc/hosts`,
203+
`${BIN_DIR}/coder-archive-create -f ${out} ~/gziptest.txt`,
194204
);
195205
ensureRunOk("archive-create gzip explicit -f", run);
196206

197207
expect(run.stdout.trim()).toEqual(out);
198208
expect(await fileExists(id, out)).toBe(true);
199209

200-
const tarList = await sh(id, `tar -tzf ${out}`);
201-
ensureRunOk("tar -tzf contents (gzip)", tarList);
202-
expect(tarList.stdout).toContain("etc/hosts");
203-
expect(tarList.stdout).toContain("etc/hostname");
210+
const list = await sh(id, `tar -tzf ${out}`);
211+
ensureRunOk("tar -tzf contents (gzip)", list);
212+
expect(list.stdout).toContain("gzip/test.txt");
213+
expect(list.stdout).toContain("gziptest.txt");
204214
}, 20000);
205215

206216
it("creates a zstd-compressed archive when requested via CLI override", async () => {

registry/coder/modules/archive/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ locals {
7878

7979
# Ensure ~ is expanded because it cannot be expanded inside quotes in a
8080
# templated shell script.
81-
paths = [for v in var.paths : replace(v, "/^~/", "$$HOME")]
82-
exclude_patterns = [for v in var.exclude_patterns : replace(v, "/^~/", "$$HOME")]
83-
directory = replace(var.directory, "/^~/", "$$HOME")
84-
output_dir = replace(var.output_dir, "/^~/", "$$HOME")
81+
paths = [for v in var.paths : replace(v, "/^~(\\/|$)/", "$$HOME$1")]
82+
exclude_patterns = [for v in var.exclude_patterns : replace(v, "/^~(\\/|$)/", "$$HOME$1")]
83+
directory = replace(var.directory, "/^~(\\/|$)/", "$$HOME$1")
84+
output_dir = replace(var.output_dir, "/^~(\\/|$)/", "$$HOME$1")
8585

8686
archive_path = "${local.output_dir}/${var.archive_name}${local.extension}"
8787
}

0 commit comments

Comments
 (0)