@@ -235,55 +235,56 @@ func TestTemplatePull_LatestStdout(t *testing.T) {
235
235
func TestTemplatePull_ToDir (t * testing.T ) {
236
236
t .Parallel ()
237
237
238
- // Prevents the tests from running in parallel.
239
- tmp := t .TempDir ()
240
- expectedDest := filepath .Join (tmp , "expected" )
241
-
242
238
tests := []struct {
243
- name string
244
- givenPath string
239
+ name string
240
+ destPath string
241
+ useDefaultDest bool
245
242
}{
246
243
{
247
- name : "absolute path works" ,
248
- givenPath : filepath . Join ( tmp , "actual" ) ,
244
+ name : "absolute path works" ,
245
+ useDefaultDest : true ,
249
246
},
250
247
{
251
- name : "relative path to specific dir is sanitized" ,
252
- givenPath : "./pulltmp" ,
248
+ name : "relative path to specific dir is sanitized" ,
249
+ destPath : "./pulltmp" ,
253
250
},
254
251
{
255
- name : "relative path to current dir is sanitized" ,
256
- givenPath : "." ,
252
+ name : "relative path to current dir is sanitized" ,
253
+ destPath : "." ,
257
254
},
258
255
{
259
- name : "directory traversal is acceptable" ,
260
- givenPath : "../mytmpl" ,
256
+ name : "directory traversal is acceptable" ,
257
+ destPath : "../mytmpl" ,
261
258
},
262
259
{
263
- name : "empty path falls back to using template name" ,
264
- givenPath : "" ,
260
+ name : "empty path falls back to using template name" ,
261
+ destPath : "" ,
265
262
},
266
263
}
267
264
268
- // nolint: paralleltest // These tests all share expectedDest
265
+ // nolint: paralleltest // These tests change the current working dir, and is therefore unsuitable for parallelisation.
269
266
for _ , tc := range tests {
270
267
tc := tc
271
268
272
269
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 ()
275
270
cwd , err := os .Getwd ()
276
271
require .NoError (t , err )
277
- require .NoError (t , os .Chdir (newWD ))
278
-
279
272
t .Cleanup (func () {
280
273
require .NoError (t , os .Chdir (cwd ))
281
274
})
282
275
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
+ }
287
288
288
289
client := coderdtest .New (t , & coderdtest.Options {
289
290
IncludeProvisionerDaemon : true ,
@@ -316,15 +317,21 @@ func TestTemplatePull_ToDir(t *testing.T) {
316
317
err = extract .Tar (ctx , bytes .NewReader (expected ), expectedDest , nil )
317
318
require .NoError (t , err )
318
319
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 )
320
327
clitest .SetupConfig (t , templateAdmin , root )
321
328
322
329
ptytest .New (t ).Attach (inv )
323
330
324
331
require .NoError (t , inv .Run ())
325
332
326
333
// Validate behaviour of choosing template name in the absence of an output path argument.
327
- destPath := tc . givenPath
334
+ destPath := actualDest
328
335
if destPath == "" {
329
336
destPath = template .Name
330
337
}
0 commit comments