Skip to content

Commit 56ebb0d

Browse files
author
takeshi iijima
committed
fix: resolve go vet issues and improve test code
- Fix range variable copies lock issue in config_test.go - Add SetWriter method to Logger for testing - Fix unused imports in test files - Update handler.go to resolve unused variable warning - Ensure all tests pass with go vet checks
1 parent dff4c1e commit 56ebb0d

File tree

8 files changed

+146
-123
lines changed

8 files changed

+146
-123
lines changed

go-json-server_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"testing"
@@ -13,17 +12,17 @@ import (
1312

1413
func TestMainComponents(t *testing.T) {
1514
// This test doesn't run the main function itself but tests key components used by main
16-
15+
1716
// Create a temporary directory for test files
1817
tempDir, err := os.MkdirTemp("", "go-json-server-test")
1918
assert.NoError(t, err)
2019
defer os.RemoveAll(tempDir)
21-
20+
2221
// Create a test JSON file for the endpoint
2322
jsonFile := filepath.Join(tempDir, "test.json")
2423
err = os.WriteFile(jsonFile, []byte(`{"message":"test"}`), 0644)
2524
assert.NoError(t, err)
26-
25+
2726
// Create a test config file
2827
configPath := filepath.Join(tempDir, "config.json")
2928
configContent := `{
@@ -41,13 +40,13 @@ func TestMainComponents(t *testing.T) {
4140
}`
4241
err = os.WriteFile(configPath, []byte(configContent), 0644)
4342
assert.NoError(t, err)
44-
43+
4544
// Test loading config
4645
cfg, err := config.LoadConfig(configPath)
4746
assert.NoError(t, err)
4847
assert.Equal(t, 8080, cfg.Port)
4948
assert.Equal(t, "info", cfg.LogLevel)
50-
49+
5150
// Test creating logger
5251
logConfig := logger.LogConfig{
5352
Level: logger.ParseLogLevel(cfg.LogLevel),
@@ -57,7 +56,7 @@ func TestMainComponents(t *testing.T) {
5756
log, err := logger.NewLogger(logConfig)
5857
assert.NoError(t, err)
5958
assert.NotNil(t, log)
60-
59+
6160
// Test config validation
6261
err = cfg.Validate()
6362
assert.NoError(t, err)

src/config/config.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (c *Config) Reload(path string) error {
121121

122122
c.mu.Lock()
123123
defer c.mu.Unlock()
124-
124+
125125
c.Host = newConfig.Host
126126
c.Port = newConfig.Port
127127
c.LogLevel = newConfig.LogLevel
@@ -136,11 +136,11 @@ func (c *Config) Reload(path string) error {
136136
func (c *Config) GetEndpoints() []Endpoint {
137137
c.mu.RLock()
138138
defer c.mu.RUnlock()
139-
139+
140140
// Create a copy to avoid race conditions
141141
endpoints := make([]Endpoint, len(c.Endpoints))
142142
copy(endpoints, c.Endpoints)
143-
143+
144144
return endpoints
145145
}
146146

@@ -171,35 +171,35 @@ func WatchConfig(configPath string, config *Config, reloadCh chan<- bool) error
171171
if err != nil {
172172
return fmt.Errorf("failed to create file watcher: %w", err)
173173
}
174-
174+
175175
go func() {
176176
defer watcher.Close()
177-
177+
178178
dir := filepath.Dir(configPath)
179179
if err := watcher.Add(dir); err != nil {
180180
fmt.Printf("Error watching directory %s: %v\n", dir, err)
181181
return
182182
}
183-
183+
184184
for {
185185
select {
186186
case event, ok := <-watcher.Events:
187187
if !ok {
188188
return
189189
}
190-
190+
191191
if event.Name == configPath && (event.Op&fsnotify.Write == fsnotify.Write) {
192192
// Add a small delay to wait for write completion
193193
time.Sleep(100 * time.Millisecond)
194-
194+
195195
fmt.Println("Config file changed, reloading...")
196196
if err := config.Reload(configPath); err != nil {
197197
fmt.Printf("Error reloading config: %v\n", err)
198198
} else if reloadCh != nil {
199199
reloadCh <- true
200200
}
201201
}
202-
202+
203203
case err, ok := <-watcher.Errors:
204204
if !ok {
205205
return
@@ -208,6 +208,6 @@ func WatchConfig(configPath string, config *Config, reloadCh chan<- bool) error
208208
}
209209
}
210210
}()
211-
211+
212212
return nil
213213
}

src/config/config_test.go

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package config
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"testing"
8-
"time"
97

108
"github.com/stretchr/testify/assert"
119
)
@@ -67,78 +65,95 @@ func TestConfig_Validate(t *testing.T) {
6765
err = os.Mkdir(testFolder, 0755)
6866
assert.NoError(t, err)
6967

70-
tests := []struct {
68+
type testCase struct {
7169
name string
72-
config Config
70+
setupFn func() Config
7371
wantError bool
74-
}{
72+
}
73+
74+
tests := []testCase{
7575
{
7676
name: "Valid config",
77-
config: Config{
78-
Endpoints: []Endpoint{
79-
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
80-
},
77+
setupFn: func() Config {
78+
return Config{
79+
Endpoints: []Endpoint{
80+
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
81+
},
82+
}
8183
},
8284
wantError: false,
8385
},
8486
{
8587
name: "Valid config with file server",
86-
config: Config{
87-
Endpoints: []Endpoint{
88-
{Path: "/static", Folder: testFolder},
89-
},
88+
setupFn: func() Config {
89+
return Config{
90+
Endpoints: []Endpoint{
91+
{Path: "/static", Folder: testFolder},
92+
},
93+
}
9094
},
9195
wantError: false,
9296
},
9397
{
9498
name: "No endpoints",
95-
config: Config{
96-
Endpoints: []Endpoint{},
99+
setupFn: func() Config {
100+
return Config{
101+
Endpoints: []Endpoint{},
102+
}
97103
},
98104
wantError: true,
99105
},
100106
{
101107
name: "Empty path",
102-
config: Config{
103-
Endpoints: []Endpoint{
104-
{Method: "GET", Path: "", JsonPath: jsonFile, Status: 200},
105-
},
108+
setupFn: func() Config {
109+
return Config{
110+
Endpoints: []Endpoint{
111+
{Method: "GET", Path: "", JsonPath: jsonFile, Status: 200},
112+
},
113+
}
106114
},
107115
wantError: true,
108116
},
109117
{
110118
name: "Duplicate endpoint",
111-
config: Config{
112-
Endpoints: []Endpoint{
113-
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
114-
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
115-
},
119+
setupFn: func() Config {
120+
return Config{
121+
Endpoints: []Endpoint{
122+
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
123+
{Method: "GET", Path: "/test", JsonPath: jsonFile, Status: 200},
124+
},
125+
}
116126
},
117127
wantError: true,
118128
},
119129
{
120130
name: "JSON file not found",
121-
config: Config{
122-
Endpoints: []Endpoint{
123-
{Method: "GET", Path: "/test", JsonPath: filepath.Join(tempDir, "notfound.json"), Status: 200},
124-
},
131+
setupFn: func() Config {
132+
return Config{
133+
Endpoints: []Endpoint{
134+
{Method: "GET", Path: "/test", JsonPath: filepath.Join(tempDir, "notfound.json"), Status: 200},
135+
},
136+
}
125137
},
126138
wantError: true,
127139
},
128140
{
129141
name: "Folder not found",
130-
config: Config{
131-
Endpoints: []Endpoint{
132-
{Path: "/static", Folder: filepath.Join(tempDir, "notfound")},
133-
},
142+
setupFn: func() Config {
143+
return Config{
144+
Endpoints: []Endpoint{
145+
{Path: "/static", Folder: filepath.Join(tempDir, "notfound")},
146+
},
147+
}
134148
},
135149
wantError: true,
136150
},
137151
}
138152

139153
for _, tt := range tests {
140154
t.Run(tt.name, func(t *testing.T) {
141-
err := tt.config.Validate()
155+
config := tt.setupFn()
156+
err := config.Validate()
142157
if tt.wantError {
143158
assert.Error(t, err)
144159
} else {

0 commit comments

Comments
 (0)