Skip to content

Commit 099f72b

Browse files
committed
improve test coverage to >90%
1 parent 19b61f5 commit 099f72b

File tree

4 files changed

+204
-18
lines changed

4 files changed

+204
-18
lines changed

capture_test.go

+76-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
"sync"
78
"testing"
8-
9+
910
"github.com/stretchr/testify/require"
1011
)
1112

@@ -113,6 +114,12 @@ func TestCaptureStdoutAndStderr(t *testing.T) {
113114
fmt.Fprintln(os.Stderr, "stderr")
114115
},
115116
},
117+
{
118+
name: "empty function",
119+
wantOut: "",
120+
wantErr: "",
121+
f: func() {},
122+
},
116123
{
117124
name: "large output",
118125
wantOut: strings.Repeat("a", 100000) + "\n",
@@ -122,13 +129,79 @@ func TestCaptureStdoutAndStderr(t *testing.T) {
122129
fmt.Fprintln(os.Stderr, strings.Repeat("b", 100000))
123130
},
124131
},
132+
{
133+
name: "binary data",
134+
wantOut: string([]byte{0, 1, 2, 3}),
135+
wantErr: string([]byte{4, 5, 6, 7}),
136+
f: func() {
137+
os.Stdout.Write([]byte{0, 1, 2, 3})
138+
os.Stderr.Write([]byte{4, 5, 6, 7})
139+
},
140+
},
141+
{
142+
name: "concurrent writes",
143+
wantOut: "out1\nout2\nout3\n",
144+
wantErr: "err1\nerr2\nerr3\n",
145+
f: func() {
146+
var wg sync.WaitGroup
147+
wg.Add(6)
148+
149+
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out1") }()
150+
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err1") }()
151+
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out2") }()
152+
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err2") }()
153+
go func() { defer wg.Done(); fmt.Fprintln(os.Stdout, "out3") }()
154+
go func() { defer wg.Done(); fmt.Fprintln(os.Stderr, "err3") }()
155+
156+
wg.Wait()
157+
},
158+
},
125159
}
126160

127161
for _, tt := range tests {
128162
t.Run(tt.name, func(t *testing.T) {
129163
gotOut, gotErr := CaptureStdoutAndStderr(t, tt.f)
130-
require.Equal(t, tt.wantOut, gotOut, "CaptureStdoutAndStderr() stdout returned unexpected output")
131-
require.Equal(t, tt.wantErr, gotErr, "CaptureStdoutAndStderr() stderr returned unexpected output")
164+
165+
// for concurrent writes, we can't guarantee order
166+
if tt.name == "concurrent writes" {
167+
// check that all expected lines are present, ignoring order
168+
for _, line := range []string{"out1", "out2", "out3"} {
169+
require.Contains(t, gotOut, line)
170+
}
171+
for _, line := range []string{"err1", "err2", "err3"} {
172+
require.Contains(t, gotErr, line)
173+
}
174+
} else {
175+
require.Equal(t, tt.wantOut, gotOut, "CaptureStdoutAndStderr() stdout returned unexpected output")
176+
require.Equal(t, tt.wantErr, gotErr, "CaptureStdoutAndStderr() stderr returned unexpected output")
177+
}
132178
})
133179
}
134180
}
181+
182+
// TestCaptureFunctionErrors tests edge cases for the capture functions
183+
func TestCaptureFunctionErrors(t *testing.T) {
184+
t.Run("panic in function", func(t *testing.T) {
185+
defer func() {
186+
if r := recover(); r == nil {
187+
t.Errorf("Expected panic")
188+
}
189+
}()
190+
191+
CaptureStdout(t, func() {
192+
panic("test panic")
193+
})
194+
})
195+
196+
t.Run("stderr with panic", func(t *testing.T) {
197+
defer func() {
198+
if r := recover(); r == nil {
199+
t.Errorf("Expected panic")
200+
}
201+
}()
202+
203+
CaptureStderr(t, func() {
204+
panic("test panic")
205+
})
206+
})
207+
}

containers/mongo_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package containers
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"github.com/stretchr/testify/assert"
@@ -54,4 +55,47 @@ func TestMongoTestContainer(t *testing.T) {
5455

5556
assert.NotEqual(t, coll1.Name(), coll2.Name())
5657
})
58+
59+
t.Run("close with original environment variable", func(t *testing.T) {
60+
// save current MONGO_TEST value
61+
origEnv := os.Getenv("MONGO_TEST")
62+
testValue := "mongodb://original-value:27017"
63+
os.Setenv("MONGO_TEST", testValue)
64+
defer func() {
65+
// restore original value
66+
if origEnv == "" {
67+
os.Unsetenv("MONGO_TEST")
68+
} else {
69+
os.Setenv("MONGO_TEST", origEnv)
70+
}
71+
}()
72+
73+
mongo := NewMongoTestContainer(ctx, t, 7)
74+
err := mongo.Close(ctx)
75+
require.NoError(t, err)
76+
77+
// verify environment restored
78+
restoredValue := os.Getenv("MONGO_TEST")
79+
assert.Equal(t, testValue, restoredValue, "Original environment value should be restored")
80+
})
81+
82+
t.Run("close with no original environment variable", func(t *testing.T) {
83+
// save current MONGO_TEST value
84+
origEnv := os.Getenv("MONGO_TEST")
85+
os.Unsetenv("MONGO_TEST")
86+
defer func() {
87+
// restore original value
88+
if origEnv != "" {
89+
os.Setenv("MONGO_TEST", origEnv)
90+
}
91+
}()
92+
93+
mongo := NewMongoTestContainer(ctx, t, 7)
94+
err := mongo.Close(ctx)
95+
require.NoError(t, err)
96+
97+
// verify environment is unset
98+
_, exists := os.LookupEnv("MONGO_TEST")
99+
assert.False(t, exists, "MONGO_TEST environment variable should be unset")
100+
})
57101
}

file_utils_test.go

+61-15
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,70 @@ package testutils
22

33
import (
44
"os"
5+
"path/filepath"
56
"testing"
6-
7+
78
"github.com/stretchr/testify/require"
89
)
910

1011
func TestWriteTestFile(t *testing.T) {
11-
// test creating a file with content
12-
content := "test content"
13-
filePath := WriteTestFile(t, content)
14-
15-
// check if file exists
16-
_, err := os.Stat(filePath)
17-
require.False(t, os.IsNotExist(err), "WriteTestFile did not create file at %s", filePath)
18-
19-
// check content
20-
data, err := os.ReadFile(filePath)
21-
require.NoError(t, err, "Failed to read test file")
22-
require.Equal(t, content, string(data), "File content doesn't match expected")
23-
24-
// file should be cleaned up automatically at the end of the test
12+
t.Run("standard file creation", func(t *testing.T) {
13+
// test creating a file with content
14+
content := "test content"
15+
filePath := WriteTestFile(t, content)
16+
17+
// check if file exists
18+
_, err := os.Stat(filePath)
19+
require.False(t, os.IsNotExist(err), "WriteTestFile did not create file at %s", filePath)
20+
21+
// check content
22+
data, err := os.ReadFile(filePath)
23+
require.NoError(t, err, "Failed to read test file")
24+
require.Equal(t, content, string(data), "File content doesn't match expected")
25+
26+
// verify directory structure
27+
dir := filepath.Dir(filePath)
28+
require.Contains(t, dir, "testutils-", "Temp directory should contain expected prefix")
29+
30+
// file should be cleaned up automatically at the end of the test
31+
})
32+
33+
t.Run("with empty content", func(t *testing.T) {
34+
filePath := WriteTestFile(t, "")
35+
36+
// check empty file was created
37+
info, err := os.Stat(filePath)
38+
require.NoError(t, err, "File should exist")
39+
require.Zero(t, info.Size(), "File should be empty")
40+
})
41+
42+
t.Run("with multi-line content", func(t *testing.T) {
43+
content := "line 1\nline 2\nline 3"
44+
filePath := WriteTestFile(t, content)
45+
46+
data, err := os.ReadFile(filePath)
47+
require.NoError(t, err)
48+
require.Equal(t, content, string(data))
49+
})
50+
51+
t.Run("cleanup by direct call", func(t *testing.T) {
52+
// create a test file
53+
content := "test cleanup"
54+
filePath := WriteTestFile(t, content)
55+
56+
// get the directory to be cleaned up
57+
dir := filepath.Dir(filePath)
58+
59+
// verify directory and file exist
60+
require.DirExists(t, dir)
61+
require.FileExists(t, filePath)
62+
63+
// manually clean up (simulating what t.Cleanup would do)
64+
err := os.RemoveAll(dir)
65+
require.NoError(t, err)
66+
67+
// after manual cleanup, the file should no longer exist
68+
_, err = os.Stat(filePath)
69+
require.True(t, os.IsNotExist(err), "File should be removed after cleanup")
70+
})
2571
}

http_utils_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@ func TestMockHTTPServer(t *testing.T) {
4141
require.Equal(t, expectedBody, string(body), "Wrong response body")
4242
}
4343

44+
func TestRequestCaptorGetRequest(t *testing.T) {
45+
// test the edge cases of GetRequest
46+
captor := &RequestCaptor{}
47+
48+
// test with empty requests
49+
_, ok := captor.GetRequest(0)
50+
require.False(t, ok, "GetRequest should return false for empty requests")
51+
52+
// test with index out of bounds
53+
_, ok = captor.GetRequest(-1)
54+
require.False(t, ok, "GetRequest should return false for negative index")
55+
56+
// test with index out of bounds (positive)
57+
_, ok = captor.GetRequest(5)
58+
require.False(t, ok, "GetRequest should return false for index beyond length")
59+
60+
// add a sample request to test valid access
61+
captor.add(RequestRecord{Method: "GET", Path: "/test"})
62+
req, ok := captor.GetRequest(0)
63+
require.True(t, ok, "GetRequest should return true for valid index")
64+
require.Equal(t, "GET", req.Method, "Request method should match")
65+
}
66+
4467
func TestHTTPRequestCaptor(t *testing.T) {
4568
// create a test handler that will receive forwarded requests
4669
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)