Skip to content

Commit f916ab8

Browse files
committed
Address some golangci-lint issues
1 parent 2a814b1 commit f916ab8

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

internal/agent/api/server_test.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,41 @@ func TestServer(t *testing.T) {
2727
close(errCh)
2828
}()
2929

30+
testErrCh := make(chan error, 1)
3031
go func() {
3132
defer s.Close()
3233
config.GlobalConfig.ApiKey = "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc"
3334
buf := bytes.NewBufferString(`{"key": "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc", "actions":[ "capture 12321", "capture 2341", "capture findmydeviced"] }`)
3435
resp, err := http.Post(fmt.Sprintf("http://%s/action", s.Addr()), "text", buf)
3536
if err != nil {
36-
t.Fatal(err)
37+
testErrCh <- err
38+
return
3739
}
3840

3941
if resp.Body != nil {
4042
all, err := io.ReadAll(resp.Body)
4143
if err != nil {
42-
t.Fatal(err)
44+
testErrCh <- err
45+
return
4346
}
4447
all = bytes.TrimSpace(all)
4548
if string(all) != `{"Code":0,"Msg":""}` {
46-
t.Fatal(string(all), all)
49+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
50+
return
4751
}
4852
}
53+
testErrCh <- nil
4954
}()
5055

5156
select {
5257
case err, ok := <-errCh:
5358
if ok {
5459
t.Fatal(err)
5560
}
61+
case err := <-testErrCh:
62+
if err != nil {
63+
t.Fatal(err)
64+
}
5665
}
5766
}
5867

@@ -72,32 +81,41 @@ func TestServerCmdActions(t *testing.T) {
7281
close(errCh)
7382
}()
7483

84+
testErrCh := make(chan error, 1)
7585
go func() {
7686
defer s.Close()
7787
config.GlobalConfig.ApiKey = "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc"
7888
buf := bytes.NewBufferString(`{"key": "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc", "actions":[ "date", "capture 2341", "echo $pid"] }`)
7989
resp, err := http.Post(fmt.Sprintf("http://%s/action", s.Addr()), "text", buf)
8090
if err != nil {
81-
t.Fatal(err)
91+
testErrCh <- err
92+
return
8293
}
8394

8495
if resp.Body != nil {
8596
all, err := io.ReadAll(resp.Body)
8697
if err != nil {
87-
t.Fatal(err)
98+
testErrCh <- err
99+
return
88100
}
89101
all = bytes.TrimSpace(all)
90102
if !bytes.HasPrefix(all, []byte(`{"Code":0`)) {
91-
t.Fatalf("%s, %x", string(all), all)
103+
testErrCh <- fmt.Errorf("unexpected response: %s, %x", string(all), all)
104+
return
92105
}
93106
}
107+
testErrCh <- nil
94108
}()
95109

96110
select {
97111
case err, ok := <-errCh:
98112
if ok {
99113
t.Fatal(err)
100114
}
115+
case err := <-testErrCh:
116+
if err != nil {
117+
t.Fatal(err)
118+
}
101119
}
102120
}
103121

@@ -132,32 +150,38 @@ func TestServerForward(t *testing.T) {
132150
close(rerrCh)
133151
}()
134152

153+
testErrCh := make(chan error, 1)
135154
go func() {
136155
defer s.Close()
137156
defer rs.Close()
138157
config.GlobalConfig.ApiKey = "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc"
139158
buf := bytes.NewBufferString(`{"key": "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc", "actions":[ "capture 12321", "capture 2341", "capture findmydeviced"] }`)
140159
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/action", s.Addr()), buf)
141160
if err != nil {
142-
t.Fatal(err)
161+
testErrCh <- err
162+
return
143163
}
144164
req.Close = true
145165
req.Header.Add("ycrash-forward", fmt.Sprintf("http://%s/action", rs.Addr()))
146166
resp, err := http.DefaultClient.Do(req)
147167
if err != nil {
148-
t.Fatal(err)
168+
testErrCh <- err
169+
return
149170
}
150171

151172
if resp.Body != nil {
152173
all, err := io.ReadAll(resp.Body)
153174
if err != nil {
154-
t.Fatal(err)
175+
testErrCh <- err
176+
return
155177
}
156178
all = bytes.TrimSpace(all)
157179
if string(all) != `{"Code":0,"Msg":""}` {
158-
t.Fatal(string(all), all)
180+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
181+
return
159182
}
160183
}
184+
testErrCh <- nil
161185
}()
162186

163187
select {
@@ -169,6 +193,10 @@ func TestServerForward(t *testing.T) {
169193
if ok {
170194
t.Fatal(err)
171195
}
196+
case err := <-testErrCh:
197+
if err != nil {
198+
t.Fatal(err)
199+
}
172200
}
173201
}
174202

@@ -188,33 +216,42 @@ func TestAttendanceAPI(t *testing.T) {
188216
close(errCh)
189217
}()
190218

219+
testErrCh := make(chan error, 1)
191220
go func() {
192221
defer s.Close()
193222
config.GlobalConfig.Server = "https://test.gceasy.io"
194223
config.GlobalConfig.ApiKey = "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc"
195224
buf := bytes.NewBufferString(`{"key": "buggycompany@e094aasdsa-c3eb-4c9a-8254-f0dd107245cc", "actions":[ "attendance"] }`)
196225
resp, err := http.Post(fmt.Sprintf("http://%s/action", s.Addr()), "text", buf)
197226
if err != nil {
198-
t.Fatal(err)
227+
testErrCh <- err
228+
return
199229
}
200230

201231
if resp.Body != nil {
202232
all, err := io.ReadAll(resp.Body)
203233
if err != nil {
204-
t.Fatal(err)
234+
testErrCh <- err
235+
return
205236
}
206237
all = bytes.TrimSpace(all)
207238
if string(all) != `{"Code":0,"Msg":""}` {
208-
t.Fatal(all)
239+
testErrCh <- fmt.Errorf("unexpected response: %s", string(all))
240+
return
209241
}
210242
}
243+
testErrCh <- nil
211244
}()
212245

213246
select {
214247
case err, ok := <-errCh:
215248
if ok {
216249
t.Fatal(err)
217250
}
251+
case err := <-testErrCh:
252+
if err != nil {
253+
t.Fatal(err)
254+
}
218255
}
219256
}
220257

internal/capture/extended_data_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestExtendedData_Run_Success(t *testing.T) {
3333
scriptFile := ""
3434

3535
// Step 3: Create a simple .bat script
36-
var scriptContent = ""
36+
var scriptContent string
3737
if runtime.GOOS == "windows" {
3838
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
3939
scriptFile = filepath.Join(scriptDir, "test-script.bat")
@@ -108,7 +108,7 @@ func TestExtendedData_Run_ScriptTimeout(t *testing.T) {
108108
scriptFile := filepath.Join(scriptDir, "test-script.bat")
109109

110110
// Step 3: Create a simple .bat script
111-
var scriptContent = ""
111+
var scriptContent string
112112
if runtime.GOOS == "windows" {
113113
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
114114
} else {
@@ -177,7 +177,7 @@ func TestExtendedData_Run_FolderDeleting(t *testing.T) {
177177
scriptFile := filepath.Join(scriptDir, "test-script.bat")
178178

179179
// Step 4: Create a simple .bat or shell script
180-
var scriptContent = ""
180+
var scriptContent string
181181
if runtime.GOOS == "windows" {
182182
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
183183
} else {
@@ -211,7 +211,7 @@ func TestExtendedData_Run_FolderDeleting(t *testing.T) {
211211
ed.SetEndpoint("http://localhost:8080/ycrash-receiver?de=localhost")
212212

213213
// // Step 8: Run the script
214-
_, err = ed.Run()
214+
ed.Run()
215215

216216
// Step 9: Assert hello.txt still exists
217217
if _, err := os.Stat(helloFilePath); os.IsNotExist(err) {
@@ -240,7 +240,7 @@ func TestExtendedData_Run_ScriptFolderSameAsDataFolder(t *testing.T) {
240240
scriptFile := filepath.Join(scriptDir, "test-script.bat")
241241

242242
// Step 4: Create a simple .bat or shell script
243-
var scriptContent = ""
243+
var scriptContent string
244244
if runtime.GOOS == "windows" {
245245
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
246246
} else {
@@ -274,7 +274,7 @@ func TestExtendedData_Run_ScriptFolderSameAsDataFolder(t *testing.T) {
274274
ed.SetEndpoint("http://localhost:8080/ycrash-receiver?de=localhost")
275275

276276
// // Step 8: Run the script
277-
result, err := ed.Run()
277+
result, _ := ed.Run()
278278
if !result.Ok {
279279
t.Fatalf("Run not OK: %s", result.Msg)
280280
}
@@ -303,7 +303,7 @@ func TestExtendedData_Run_EmptyDataFolder(t *testing.T) {
303303
scriptFile := filepath.Join(scriptDir, "test-script.bat")
304304

305305
// Step 4: Create a simple .bat or shell script
306-
var scriptContent = ""
306+
var scriptContent string
307307
if runtime.GOOS == "windows" {
308308
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
309309
} else {
@@ -371,7 +371,7 @@ func TestExtendedData_Run_EmptyScript(t *testing.T) {
371371
scriptFile := filepath.Join(scriptDir, "test-script.bat")
372372

373373
// Step 4: Create a simple .bat or shell script
374-
var scriptContent = ""
374+
var scriptContent string
375375
if runtime.GOOS == "windows" {
376376
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
377377
} else {
@@ -437,7 +437,7 @@ func TestExtendedData_Run_RelativePath(t *testing.T) {
437437
scriptFile := filepath.Join(scriptDir, "test-script.bat")
438438

439439
// Step 3: Create a simple .bat script
440-
var scriptContent = ""
440+
var scriptContent string
441441
if runtime.GOOS == "windows" {
442442
scriptContent = "@echo off\r\necho test-data > \"" + outputFile + "\"\r\n"
443443
} else {

internal/capture/gc_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package capture
22

33
import (
44
"errors"
5-
"fmt"
65
"io"
76
"os"
87
"path/filepath"
@@ -35,7 +34,7 @@ func TestProcessLogFile(t *testing.T) {
3534
t.Fatal(err)
3635
}
3736
s := string(all)
38-
if s != fmt.Sprintf("test\ntest") {
37+
if s != "test\ntest" {
3938
t.Fatal(s)
4039
}
4140
}

internal/capture/healthcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func sanitizeAppNameForFileName(name string) string {
138138
// This ensures consistent logging across multiple outputs.
139139
func logToFileAndLogger(file *os.File, format string, values ...interface{}) {
140140
logger.Log(format, values...)
141-
file.WriteString(fmt.Sprintf(format+"\n", values...))
141+
fmt.Fprintf(file, format+"\n", values...)
142142
}
143143

144144
func newHTTPClient() *http.Client {

internal/capture/heap.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ func (t *HeapDump) heapDump(requestedFilePath string) (actualDumpPath string, er
330330
output, err = executils.CommandCombinedOutput(executils.Command{path.Join(t.JavaHome, "/bin/jcmd"), strconv.Itoa(t.Pid), "GC.heap_dump", requestedFilePath}, executils.SudoHooker{PID: t.Pid})
331331
logger.Log("heap dump output from jcmd: %s, %v", output, err)
332332
if err != nil ||
333-
bytes.Index(output, []byte("No such file")) >= 0 ||
334-
bytes.Index(output, []byte("Permission denied")) >= 0 {
333+
bytes.Contains(output, []byte("No such file")) ||
334+
bytes.Contains(output, []byte("Permission denied")) {
335335
if len(output) > 1 {
336336
err = fmt.Errorf("%w because %s", err, output)
337337
}
@@ -342,8 +342,8 @@ func (t *HeapDump) heapDump(requestedFilePath string) (actualDumpPath string, er
342342
executils.SudoHooker{PID: t.Pid})
343343
logger.Log("heap dump output from jattach: %s, %v", output, e2)
344344
if e2 != nil ||
345-
bytes.Index(output, []byte("No such file")) >= 0 ||
346-
bytes.Index(output, []byte("Permission denied")) >= 0 {
345+
bytes.Contains(output, []byte("No such file")) ||
346+
bytes.Contains(output, []byte("Permission denied")) {
347347
if len(output) > 1 {
348348
e2 = fmt.Errorf("%w because %s", e2, output)
349349
}
@@ -360,8 +360,8 @@ func (t *HeapDump) heapDump(requestedFilePath string) (actualDumpPath string, er
360360
executils.SudoHooker{PID: t.Pid})
361361
logger.Log("heap dump output from tmp jattach: %s, %v", output, e3)
362362
if e3 != nil ||
363-
bytes.Index(output, []byte("No such file")) >= 0 ||
364-
bytes.Index(output, []byte("Permission denied")) >= 0 {
363+
bytes.Contains(output, []byte("No such file")) ||
364+
bytes.Contains(output, []byte("Permission denied")) {
365365
if len(output) > 1 {
366366
e3 = fmt.Errorf("%w because %s", e3, output)
367367
}

internal/config/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,19 @@ func ParseFlags(args []string) error {
236236
return err
237237
}
238238

239-
if GlobalConfig.Options.ConfigPath == "" {
239+
if GlobalConfig.ConfigPath == "" {
240240
return nil
241241
}
242242

243-
file, err := os.Open(GlobalConfig.Options.ConfigPath)
243+
file, err := os.Open(GlobalConfig.ConfigPath)
244244
if err != nil {
245245
dir, _ := os.Getwd()
246-
return fmt.Errorf("workdir %s read config file path %s failed: %w", dir, GlobalConfig.Options.ConfigPath, err)
246+
return fmt.Errorf("workdir %s read config file path %s failed: %w", dir, GlobalConfig.ConfigPath, err)
247247
}
248248
decoder := yaml.NewDecoder(file)
249249
err = decoder.Decode(&GlobalConfig)
250250
if err != nil {
251-
return fmt.Errorf("decode config file path %s failed: %w", GlobalConfig.Options.ConfigPath, err)
251+
return fmt.Errorf("decode config file path %s failed: %w", GlobalConfig.ConfigPath, err)
252252
}
253253

254254
return copyFlagsValue(&GlobalConfig.Options, result)

0 commit comments

Comments
 (0)