Skip to content

Commit 7d07e67

Browse files
authored
chore: Improve test cleanup (coder#3112)
1 parent 75ff579 commit 7d07e67

File tree

4 files changed

+9
-24
lines changed

4 files changed

+9
-24
lines changed

agent/agent_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func TestAgent(t *testing.T) {
206206

207207
t.Run("StartupScript", func(t *testing.T) {
208208
t.Parallel()
209-
tempPath := filepath.Join(os.TempDir(), "content.txt")
209+
tempPath := filepath.Join(t.TempDir(), "content.txt")
210210
content := "somethingnice"
211211
setupAgent(t, agent.Metadata{
212212
StartupScript: fmt.Sprintf("echo %s > %s", content, tempPath),
@@ -324,12 +324,7 @@ func TestAgent(t *testing.T) {
324324
t.Skip("Unix socket forwarding isn't supported on Windows")
325325
}
326326

327-
tmpDir, err := os.MkdirTemp("", "coderd_agent_test_")
328-
require.NoError(t, err, "create temp dir for unix listener")
329-
t.Cleanup(func() {
330-
_ = os.RemoveAll(tmpDir)
331-
})
332-
327+
tmpDir := t.TempDir()
333328
l, err := net.Listen("unix", filepath.Join(tmpDir, "test.sock"))
334329
require.NoError(t, err, "create UDP listener")
335330
return l

cli/create_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestCreate(t *testing.T) {
192192
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
193193
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
194194
tempDir := t.TempDir()
195+
removeTmpDirUntilSuccessAfterTest(t, tempDir)
195196
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
196197
_, _ = parameterFile.WriteString("region: \"bingo\"\nusername: \"boingo\"")
197198
cmd, root := clitest.New(t, "create", "", "--parameter-file", parameterFile.Name())
@@ -217,7 +218,6 @@ func TestCreate(t *testing.T) {
217218
pty.WriteLine(value)
218219
}
219220
<-doneChan
220-
removeTmpDirUntilSuccess(t, tempDir)
221221
})
222222

223223
t.Run("WithParameterFileNotContainingTheValue", func(t *testing.T) {
@@ -234,6 +234,7 @@ func TestCreate(t *testing.T) {
234234
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
235235
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
236236
tempDir := t.TempDir()
237+
removeTmpDirUntilSuccessAfterTest(t, tempDir)
237238
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
238239
_, _ = parameterFile.WriteString("zone: \"bananas\"")
239240
cmd, root := clitest.New(t, "create", "my-workspace", "--template", template.Name, "--parameter-file", parameterFile.Name())
@@ -248,7 +249,6 @@ func TestCreate(t *testing.T) {
248249
assert.EqualError(t, err, "Parameter value absent in parameter file for \"region\"!")
249250
}()
250251
<-doneChan
251-
removeTmpDirUntilSuccess(t, tempDir)
252252
})
253253

254254
t.Run("FailedDryRun", func(t *testing.T) {

cli/portforward_test.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,13 @@ func TestPortForward(t *testing.T) {
119119
t.Skip("Unix socket forwarding isn't supported on Windows")
120120
}
121121

122-
tmpDir, err := os.MkdirTemp("", "coderd_agent_test_")
123-
require.NoError(t, err, "create temp dir for unix listener")
124-
t.Cleanup(func() {
125-
_ = os.RemoveAll(tmpDir)
126-
})
127-
122+
tmpDir := t.TempDir()
128123
l, err := net.Listen("unix", filepath.Join(tmpDir, "test.sock"))
129124
require.NoError(t, err, "create UDP listener")
130125
return l
131126
},
132127
setupLocal: func(t *testing.T) (string, string) {
133-
tmpDir, err := os.MkdirTemp("", "coderd_agent_test_")
134-
require.NoError(t, err, "create temp dir for unix listener")
135-
t.Cleanup(func() {
136-
_ = os.RemoveAll(tmpDir)
137-
})
138-
128+
tmpDir := t.TempDir()
139129
path := filepath.Join(tmpDir, "test.sock")
140130
return path, path
141131
},

cli/templatecreate_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func TestTemplateCreate(t *testing.T) {
132132
ProvisionDryRun: echo.ProvisionComplete,
133133
})
134134
tempDir := t.TempDir()
135+
removeTmpDirUntilSuccessAfterTest(t, tempDir)
135136
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
136137
_, _ = parameterFile.WriteString("region: \"bananas\"")
137138
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--parameter-file", parameterFile.Name())
@@ -158,7 +159,6 @@ func TestTemplateCreate(t *testing.T) {
158159
}
159160

160161
require.NoError(t, <-execDone)
161-
removeTmpDirUntilSuccess(t, tempDir)
162162
})
163163

164164
t.Run("WithParameterFileNotContainingTheValue", func(t *testing.T) {
@@ -171,6 +171,7 @@ func TestTemplateCreate(t *testing.T) {
171171
ProvisionDryRun: echo.ProvisionComplete,
172172
})
173173
tempDir := t.TempDir()
174+
removeTmpDirUntilSuccessAfterTest(t, tempDir)
174175
parameterFile, _ := os.CreateTemp(tempDir, "testParameterFile*.yaml")
175176
_, _ = parameterFile.WriteString("zone: \"bananas\"")
176177
cmd, root := clitest.New(t, "templates", "create", "my-template", "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--parameter-file", parameterFile.Name())
@@ -196,7 +197,6 @@ func TestTemplateCreate(t *testing.T) {
196197
}
197198

198199
require.EqualError(t, <-execDone, "Parameter value absent in parameter file for \"region\"!")
199-
removeTmpDirUntilSuccess(t, tempDir)
200200
})
201201

202202
t.Run("Recreate template with same name (create, delete, create)", func(t *testing.T) {
@@ -267,7 +267,7 @@ func createTestParseResponse() []*proto.Parse_Response {
267267

268268
// Need this for Windows because of a known issue with Go:
269269
// https://github.com/golang/go/issues/52986
270-
func removeTmpDirUntilSuccess(t *testing.T, tempDir string) {
270+
func removeTmpDirUntilSuccessAfterTest(t *testing.T, tempDir string) {
271271
t.Helper()
272272
t.Cleanup(func() {
273273
err := os.RemoveAll(tempDir)

0 commit comments

Comments
 (0)