TempFile ensures a temp file for unit testing purposes. It returns the path to the directory (to which you will still need to join the filename) The returned directory is automatically removed when the test and all its subtests complete. tempdir := TempFile(t, "foo", []byte("bar")) filename :=
(t *testing.T, name string, data []byte)
| 46 | // tempdir := TempFile(t, "foo", []byte("bar")) |
| 47 | // filename := filepath.Join(tempdir, "foo") |
| 48 | func TempFile(t *testing.T, name string, data []byte) string { |
| 49 | t.Helper() |
| 50 | path := t.TempDir() |
| 51 | filename := filepath.Join(path, name) |
| 52 | if err := os.WriteFile(filename, data, 0o755); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | return path |
| 56 | } |
searching dependent graphs…