@@ -10,6 +10,7 @@ import (
10
10
"net/http"
11
11
"net/http/httptest"
12
12
"os"
13
+ "path/filepath"
13
14
"strings"
14
15
"testing"
15
16
"time"
@@ -29,24 +30,20 @@ func TestLoadConfigFromFile(t *testing.T) {
29
30
"gtfs_rt_api_key": "",
30
31
"gtfs_rt_api_value": ""
31
32
}]`
32
- tmpFile , err := os .CreateTemp ("" , "config-*.json" )
33
- if err != nil {
34
- t .Fatalf ("Failed to create temporary file: %v" , err )
35
- }
36
- defer os .Remove (tmpFile .Name ())
37
33
38
- if _ , err := tmpFile .Write ([]byte (content )); err != nil {
39
- t .Fatalf ("Failed to write to temporary file: %v" , err )
34
+ dir := t .TempDir ()
35
+ fp := filepath .Join (dir , "config.json" )
36
+ if err := os .WriteFile (fp , []byte (content ), 0o600 ); err != nil {
37
+ t .Fatalf ("write config.json: %v" , err )
40
38
}
41
- tmpFile .Close ()
42
39
43
- servers , err := loadConfigFromFile (tmpFile . Name () )
40
+ servers , err := loadConfigFromFile (fp )
44
41
if err != nil {
45
42
t .Fatalf ("loadConfigFromFile failed: %v" , err )
46
43
}
47
44
48
45
if len (servers ) != 1 {
49
- t .Fatalf ("Expected 1 server, got %d" , len (servers ))
46
+ t .Fatalf ("expected 1 server, got %d" , len (servers ))
50
47
}
51
48
52
49
expected := models.ObaServer {
@@ -62,33 +59,29 @@ func TestLoadConfigFromFile(t *testing.T) {
62
59
}
63
60
64
61
if servers [0 ] != expected {
65
- t .Errorf ("Expected server %+v, got %+v" , expected , servers [0 ])
62
+ t .Errorf ("expected %+v, got %+v" , expected , servers [0 ])
66
63
}
67
64
})
68
65
69
66
t .Run ("InvalidJSON" , func (t * testing.T ) {
67
+ dir := t .TempDir ()
68
+ fp := filepath .Join (dir , "config.json" )
70
69
content := `{ this is not valid JSON }`
71
- tmpFile , err := os .CreateTemp ("" , "invalid-config-*.json" )
72
- if err != nil {
73
- t .Fatalf ("Failed to create temporary file: %v" , err )
70
+ if err := os .WriteFile (fp , []byte (content ), 0o600 ); err != nil {
71
+ t .Fatalf ("write invalid config.json: %v" , err )
74
72
}
75
- defer os .Remove (tmpFile .Name ())
76
73
77
- if _ , err := tmpFile .Write ([]byte (content )); err != nil {
78
- t .Fatalf ("Failed to write to temporary file: %v" , err )
79
- }
80
- tmpFile .Close ()
81
-
82
- _ , err = loadConfigFromFile (tmpFile .Name ())
83
- if err == nil {
84
- t .Errorf ("Expected error with invalid JSON, got none" )
74
+ if _ , err := loadConfigFromFile (fp ); err == nil {
75
+ t .Errorf ("expected error with invalid JSON, got none" )
85
76
}
86
77
})
87
78
88
79
t .Run ("NonExistentFile" , func (t * testing.T ) {
89
- _ , err := loadConfigFromFile ("non-existent-file.json" )
90
- if err == nil {
91
- t .Errorf ("Expected error for non-existent file, got none" )
80
+ dir := t .TempDir ()
81
+ fp := filepath .Join (dir , "config.json" )
82
+
83
+ if _ , err := loadConfigFromFile (fp ); err == nil {
84
+ t .Errorf ("expected error for non-existent file, got none" )
92
85
}
93
86
})
94
87
}
0 commit comments