Skip to content

Commit 9d035e3

Browse files
committed
Refactoring for cleaner approach
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 80addee commit 9d035e3

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

cli/templatepull_test.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -235,55 +235,56 @@ func TestTemplatePull_LatestStdout(t *testing.T) {
235235
func TestTemplatePull_ToDir(t *testing.T) {
236236
t.Parallel()
237237

238-
// Prevents the tests from running in parallel.
239-
tmp := t.TempDir()
240-
expectedDest := filepath.Join(tmp, "expected")
241-
242238
tests := []struct {
243-
name string
244-
givenPath string
239+
name string
240+
destPath string
241+
useDefaultDest bool
245242
}{
246243
{
247-
name: "absolute path works",
248-
givenPath: filepath.Join(tmp, "actual"),
244+
name: "absolute path works",
245+
useDefaultDest: true,
249246
},
250247
{
251-
name: "relative path to specific dir is sanitized",
252-
givenPath: "./pulltmp",
248+
name: "relative path to specific dir is sanitized",
249+
destPath: "./pulltmp",
253250
},
254251
{
255-
name: "relative path to current dir is sanitized",
256-
givenPath: ".",
252+
name: "relative path to current dir is sanitized",
253+
destPath: ".",
257254
},
258255
{
259-
name: "directory traversal is acceptable",
260-
givenPath: "../mytmpl",
256+
name: "directory traversal is acceptable",
257+
destPath: "../mytmpl",
261258
},
262259
{
263-
name: "empty path falls back to using template name",
264-
givenPath: "",
260+
name: "empty path falls back to using template name",
261+
destPath: "",
265262
},
266263
}
267264

268-
// nolint: paralleltest // These tests all share expectedDest
265+
// nolint: paralleltest // These tests change the current working dir, and is therefore unsuitable for parallelisation.
269266
for _, tc := range tests {
270267
tc := tc
271268

272269
t.Run(tc.name, func(t *testing.T) {
273-
// Use a different working directory to not interfere with actual directory when using relative paths.
274-
newWD := t.TempDir()
275270
cwd, err := os.Getwd()
276271
require.NoError(t, err)
277-
require.NoError(t, os.Chdir(newWD))
278-
279272
t.Cleanup(func() {
280273
require.NoError(t, os.Chdir(cwd))
281274
})
282275

283-
t.Cleanup(func() {
284-
_ = os.RemoveAll(tc.givenPath)
285-
_ = os.RemoveAll(expectedDest)
286-
})
276+
dir := t.TempDir()
277+
278+
// Change working directory so that relative path tests don't affect the original working directory.
279+
newWd := filepath.Join(dir, "new-cwd")
280+
require.NoError(t, os.MkdirAll(newWd, 0o750))
281+
require.NoError(t, os.Chdir(newWd))
282+
283+
expectedDest := filepath.Join(dir, "expected")
284+
actualDest := tc.destPath
285+
if tc.useDefaultDest {
286+
actualDest = filepath.Join(dir, "actual")
287+
}
287288

288289
client := coderdtest.New(t, &coderdtest.Options{
289290
IncludeProvisionerDaemon: true,
@@ -316,15 +317,21 @@ func TestTemplatePull_ToDir(t *testing.T) {
316317
err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil)
317318
require.NoError(t, err)
318319

319-
inv, root := clitest.New(t, "templates", "pull", template.Name, tc.givenPath)
320+
ents, _ := os.ReadDir(actualDest)
321+
if len(ents) > 0 {
322+
t.Logf("%s is not empty", actualDest)
323+
t.FailNow()
324+
}
325+
326+
inv, root := clitest.New(t, "templates", "pull", template.Name, actualDest)
320327
clitest.SetupConfig(t, templateAdmin, root)
321328

322329
ptytest.New(t).Attach(inv)
323330

324331
require.NoError(t, inv.Run())
325332

326333
// Validate behaviour of choosing template name in the absence of an output path argument.
327-
destPath := tc.givenPath
334+
destPath := actualDest
328335
if destPath == "" {
329336
destPath = template.Name
330337
}

0 commit comments

Comments
 (0)