MCPcopy Index your code
hub / github.com/coder/coder / TempFile

Function TempFile

testutil/temp.go:12–33  ·  view source on GitHub ↗

TempFile returns the name of a temporary file that does not exist.

(t *testing.T, dir, pattern string)

Source from the content-addressed store, hash-verified

10
11// TempFile returns the name of a temporary file that does not exist.
12func TempFile(t *testing.T, dir, pattern string) string {
13 t.Helper()
14
15 if dir == "" {
16 dir = t.TempDir()
17 }
18 f, err := os.CreateTemp(dir, pattern)
19 require.NoError(t, err, "create temp file")
20 name := f.Name()
21 err = f.Close()
22 require.NoError(t, err, "close temp file")
23 err = os.Remove(name)
24 require.NoError(t, err, "remove temp file")
25
26 t.Cleanup(func() {
27 // The test might have created created and it may have already removed it,
28 // so we ignore the error.
29 _ = os.Remove(name)
30 })
31
32 return name
33}
34
35// CreateTemp is a convenience function for creating a temporary file, like
36// os.CreateTemp, but it also registers a cleanup function to close and remove

Callers 2

TestServerFunction · 0.92

Calls 6

HelperMethod · 0.65
TempDirMethod · 0.65
NameMethod · 0.65
CloseMethod · 0.65
RemoveMethod · 0.65
CleanupMethod · 0.65

Tested by 2

TestServerFunction · 0.74