Skip to content

Commit d519340

Browse files
committed
refactor: use testify/require in tests and fix linting issues
1 parent 795945f commit d519340

File tree

3 files changed

+43
-103
lines changed

3 files changed

+43
-103
lines changed

capture_test.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66
"strings"
77
"testing"
8+
9+
"github.com/stretchr/testify/require"
810
)
911

1012
func TestCaptureStdout(t *testing.T) {
@@ -38,9 +40,7 @@ func TestCaptureStdout(t *testing.T) {
3840
for _, tt := range tests {
3941
t.Run(tt.name, func(t *testing.T) {
4042
got := CaptureStdout(t, tt.f)
41-
if got != tt.want {
42-
t.Errorf("CaptureStdout() = %q, want %q", got, tt.want)
43-
}
43+
require.Equal(t, tt.want, got, "CaptureStdout() returned unexpected output")
4444
})
4545
}
4646
}
@@ -76,9 +76,7 @@ func TestCaptureStderr(t *testing.T) {
7676
for _, tt := range tests {
7777
t.Run(tt.name, func(t *testing.T) {
7878
got := CaptureStderr(t, tt.f)
79-
if got != tt.want {
80-
t.Errorf("CaptureStderr() = %q, want %q", got, tt.want)
81-
}
79+
require.Equal(t, tt.want, got, "CaptureStderr() returned unexpected output")
8280
})
8381
}
8482
}
@@ -129,12 +127,8 @@ func TestCaptureStdoutAndStderr(t *testing.T) {
129127
for _, tt := range tests {
130128
t.Run(tt.name, func(t *testing.T) {
131129
gotOut, gotErr := CaptureStdoutAndStderr(t, tt.f)
132-
if gotOut != tt.wantOut {
133-
t.Errorf("CaptureStdoutAndStderr() stdout = %q, want %q", gotOut, tt.wantOut)
134-
}
135-
if gotErr != tt.wantErr {
136-
t.Errorf("CaptureStdoutAndStderr() stderr = %q, want %q", gotErr, tt.wantErr)
137-
}
130+
require.Equal(t, tt.wantOut, gotOut, "CaptureStdoutAndStderr() stdout returned unexpected output")
131+
require.Equal(t, tt.wantErr, gotErr, "CaptureStdoutAndStderr() stderr returned unexpected output")
138132
})
139133
}
140134
}

file_utils_test.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package testutils
33
import (
44
"os"
55
"testing"
6+
7+
"github.com/stretchr/testify/require"
68
)
79

810
func TestWriteTestFile(t *testing.T) {
@@ -11,19 +13,13 @@ func TestWriteTestFile(t *testing.T) {
1113
filePath := WriteTestFile(t, content)
1214

1315
// check if file exists
14-
if _, err := os.Stat(filePath); os.IsNotExist(err) {
15-
t.Errorf("WriteTestFile did not create file at %s", filePath)
16-
}
17-
16+
_, err := os.Stat(filePath)
17+
require.False(t, os.IsNotExist(err), "WriteTestFile did not create file at %s", filePath)
18+
1819
// check content
1920
data, err := os.ReadFile(filePath)
20-
if err != nil {
21-
t.Errorf("Failed to read test file: %v", err)
22-
}
23-
24-
if string(data) != content {
25-
t.Errorf("Expected content %q, got %q", content, string(data))
26-
}
27-
21+
require.NoError(t, err, "Failed to read test file")
22+
require.Equal(t, content, string(data), "File content doesn't match expected")
23+
2824
// file should be cleaned up automatically at the end of the test
2925
}

http_utils_test.go

+29-79
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import (
66
"net/http"
77
"strings"
88
"testing"
9+
10+
"github.com/stretchr/testify/require"
911
)
1012

1113
func TestMockHTTPServer(t *testing.T) {
12-
// create a simple handler
14+
// create a simple handler using ResponseRecorder for write error checking
1315
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1416
w.Header().Set("Content-Type", "application/json")
1517
w.WriteHeader(http.StatusOK)
18+
// In real request handling, the error is typically ignored as it's a disconnect which HTTP server handles
19+
// ResponseWriter interface doesn't have a way to check for errors in tests
20+
// nolint:errcheck // http.ResponseWriter errors are handled by the HTTP server
1621
w.Write([]byte(`{"status":"ok"}`))
1722
})
1823

@@ -22,36 +27,25 @@ func TestMockHTTPServer(t *testing.T) {
2227

2328
// make a request to the server
2429
resp, err := http.Get(serverURL + "/test")
25-
if err != nil {
26-
t.Fatalf("Failed to make request: %v", err)
27-
}
30+
require.NoError(t, err, "Failed to make request")
2831
defer resp.Body.Close()
2932

3033
// check response
31-
if resp.StatusCode != http.StatusOK {
32-
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
33-
}
34-
35-
contentType := resp.Header.Get("Content-Type")
36-
if contentType != "application/json" {
37-
t.Errorf("Expected Content-Type %q, got %q", "application/json", contentType)
38-
}
34+
require.Equal(t, http.StatusOK, resp.StatusCode, "Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
35+
require.Equal(t, "application/json", resp.Header.Get("Content-Type"), "Wrong Content-Type header")
3936

4037
body, err := io.ReadAll(resp.Body)
41-
if err != nil {
42-
t.Fatalf("Failed to read response body: %v", err)
43-
}
38+
require.NoError(t, err, "Failed to read response body")
4439

4540
expectedBody := `{"status":"ok"}`
46-
if string(body) != expectedBody {
47-
t.Errorf("Expected body %q, got %q", expectedBody, string(body))
48-
}
41+
require.Equal(t, expectedBody, string(body), "Wrong response body")
4942
}
5043

5144
func TestHTTPRequestCaptor(t *testing.T) {
5245
// create a test handler that will receive forwarded requests
5346
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5447
w.WriteHeader(http.StatusOK)
48+
// nolint:errcheck // http.ResponseWriter errors are handled by the HTTP server
5549
w.Write([]byte("response"))
5650
})
5751

@@ -63,92 +57,48 @@ func TestHTTPRequestCaptor(t *testing.T) {
6357

6458
// make GET request
6559
_, err := http.Get(serverURL + "/get-path?param=value")
66-
if err != nil {
67-
t.Fatalf("Failed to make GET request: %v", err)
68-
}
60+
require.NoError(t, err, "Failed to make GET request")
6961

7062
// make POST request with a body
7163
postBody := `{"key":"value"}`
7264
_, err = http.Post(serverURL+"/post-path", "application/json", strings.NewReader(postBody))
73-
if err != nil {
74-
t.Fatalf("Failed to make POST request: %v", err)
75-
}
65+
require.NoError(t, err, "Failed to make POST request")
7666

7767
// make PUT request with different content type
7868
req, _ := http.NewRequest(http.MethodPut, serverURL+"/put-path", bytes.NewBuffer([]byte("text data")))
7969
req.Header.Set("Content-Type", "text/plain")
8070
req.Header.Set("Authorization", "Bearer token123")
8171
_, err = http.DefaultClient.Do(req)
82-
if err != nil {
83-
t.Fatalf("Failed to make PUT request: %v", err)
84-
}
72+
require.NoError(t, err, "Failed to make PUT request")
8573

8674
// check the captured requests
87-
if captor.Len() != 3 {
88-
t.Errorf("Expected 3 captured requests, got %d", captor.Len())
89-
}
75+
require.Equal(t, 3, captor.Len(), "Wrong number of captured requests")
9076

9177
// check GET request
9278
getReq, ok := captor.GetRequest(0)
93-
if !ok {
94-
t.Fatalf("Failed to get first request")
95-
}
96-
97-
if getReq.Method != http.MethodGet {
98-
t.Errorf("Expected method %s, got %s", http.MethodGet, getReq.Method)
99-
}
100-
101-
if getReq.Path != "/get-path" {
102-
t.Errorf("Expected path %s, got %s", "/get-path", getReq.Path)
103-
}
79+
require.True(t, ok, "Failed to get first request")
80+
require.Equal(t, http.MethodGet, getReq.Method, "Wrong request method")
81+
require.Equal(t, "/get-path", getReq.Path, "Wrong request path")
10482

10583
// check POST request
10684
postReq, ok := captor.GetRequest(1)
107-
if !ok {
108-
t.Fatalf("Failed to get second request")
109-
}
110-
111-
if postReq.Method != http.MethodPost {
112-
t.Errorf("Expected method %s, got %s", http.MethodPost, postReq.Method)
113-
}
114-
115-
if postReq.Path != "/post-path" {
116-
t.Errorf("Expected path %s, got %s", "/post-path", postReq.Path)
117-
}
118-
119-
if string(postReq.Body) != postBody {
120-
t.Errorf("Expected body %q, got %q", postBody, string(postReq.Body))
121-
}
85+
require.True(t, ok, "Failed to get second request")
86+
require.Equal(t, http.MethodPost, postReq.Method, "Wrong request method")
87+
require.Equal(t, "/post-path", postReq.Path, "Wrong request path")
88+
require.Equal(t, postBody, string(postReq.Body), "Wrong request body")
12289

12390
// check PUT request with headers
12491
putReq, ok := captor.GetRequest(2)
125-
if !ok {
126-
t.Fatalf("Failed to get third request")
127-
}
128-
129-
if putReq.Method != http.MethodPut {
130-
t.Errorf("Expected method %s, got %s", http.MethodPut, putReq.Method)
131-
}
132-
133-
authHeader := putReq.Headers.Get("Authorization")
134-
if authHeader != "Bearer token123" {
135-
t.Errorf("Expected Authorization header %q, got %q", "Bearer token123", authHeader)
136-
}
137-
138-
contentType := putReq.Headers.Get("Content-Type")
139-
if contentType != "text/plain" {
140-
t.Errorf("Expected Content-Type header %q, got %q", "text/plain", contentType)
141-
}
92+
require.True(t, ok, "Failed to get third request")
93+
require.Equal(t, http.MethodPut, putReq.Method, "Wrong request method")
94+
require.Equal(t, "Bearer token123", putReq.Headers.Get("Authorization"), "Wrong Authorization header")
95+
require.Equal(t, "text/plain", putReq.Headers.Get("Content-Type"), "Wrong Content-Type header")
14296

14397
// test GetRequests
14498
allRequests := captor.GetRequests()
145-
if len(allRequests) != 3 {
146-
t.Errorf("Expected 3 requests from GetRequests, got %d", len(allRequests))
147-
}
99+
require.Equal(t, 3, len(allRequests), "Wrong number of requests from GetRequests")
148100

149101
// test Reset
150102
captor.Reset()
151-
if captor.Len() != 0 {
152-
t.Errorf("Expected 0 requests after Reset, got %d", captor.Len())
153-
}
103+
require.Equal(t, 0, captor.Len(), "Reset didn't clear requests")
154104
}

0 commit comments

Comments
 (0)