@@ -15,7 +15,7 @@ import (
15
15
"net/http/httptest"
16
16
"net/url"
17
17
"os"
18
- "path"
18
+ "path/filepath "
19
19
"reflect"
20
20
"strconv"
21
21
"strings"
@@ -68,29 +68,23 @@ func setup() (client *Client, mux *http.ServeMux, serverURL string, teardown fun
68
68
69
69
// openTestFile creates a new file with the given name and content for testing.
70
70
// In order to ensure the exact file name, this function will create a new temp
71
- // directory, and create the file in that directory. It is the caller's
72
- // responsibility to remove the directory and its contents when no longer needed.
73
- func openTestFile (name , content string ) (file * os.File , dir string , err error ) {
74
- dir , err = os .MkdirTemp ("" , "go-github" )
71
+ // directory, and create the file in that directory. The file is automatically
72
+ // cleaned up after the test.
73
+ func openTestFile (t * testing.T , name , content string ) * os.File {
74
+ t .Helper ()
75
+ fname := filepath .Join (t .TempDir (), name )
76
+ err := os .WriteFile (fname , []byte (content ), 0600 )
75
77
if err != nil {
76
- return nil , dir , err
78
+ t . Fatal ( err )
77
79
}
78
-
79
- file , err = os .OpenFile (path .Join (dir , name ), os .O_RDWR | os .O_CREATE | os .O_EXCL , 0600 )
80
+ file , err := os .Open (fname )
80
81
if err != nil {
81
- return nil , dir , err
82
+ t . Fatal ( err )
82
83
}
83
84
84
- fmt .Fprint (file , content )
85
-
86
- // close and re-open the file to keep file.Stat() happy
87
- file .Close ()
88
- file , err = os .Open (file .Name ())
89
- if err != nil {
90
- return nil , dir , err
91
- }
85
+ t .Cleanup (func () { file .Close () })
92
86
93
- return file , dir , err
87
+ return file
94
88
}
95
89
96
90
func testMethod (t * testing.T , r * http.Request , want string ) {
0 commit comments