Skip to content

Commit d1a2acd

Browse files
authored
[test-improver] Improve tests for sys package (#7848)
# Test Improvements: `internal/sys/container_test.go` + `container_detect_test.go` ## File Analyzed - **Test Files**: `internal/sys/container_test.go`, `internal/sys/container_detect_test.go` - **Package**: `internal/sys` ## Improvements Made ### 1. Scanner-overflow coverage (new test case) Added a test case `"single line exceeding scanner buffer limit returns empty string"` to `TestExtractContainerIDFromContent`. This covers the previously-untested `scanner.Err() != nil` branch in `extractContainerIDFromContent` (triggered when a single line exceeds `bufio.MaxScanTokenSize` = 65,536 bytes): ```go { name: "single line exceeding scanner buffer limit returns empty string", input: strings.Repeat("x", bufio.MaxScanTokenSize+1), want: "", }, ``` ### 2. Parallel test execution Added `t.Parallel()` to all pure-function tests (safe to run concurrently — no environment mutation, no shared state): - `TestExtractContainerIDFromContent` + all 16 subtests - `TestContainsAny_Helper` + all subtests - 6 × `TestExtractContainerIDFromCgroupFiles_*` tests in `container_detect_test.go` (temp-file only, no `t.Setenv`) ### 3. Increased Coverage - **Previous coverage**: `extractContainerIDFromContent` → 92.3%, package total → 98.9% - **New coverage**: `extractContainerIDFromContent` → **100.0%**, package total → **100.0%** - **Improvement**: +1.1% package, +7.7% on the targeted function ## Test Execution ``` ok github.com/github/gh-aw-mcpg/internal/sys coverage: 100.0% of statements ``` All tests pass with `-race` flag enabled. Tests run multiple times with consistent results. ## Why These Changes? `extractContainerIDFromContent` used `bufio.Scanner` to parse cgroup content but the error path (`scanner.Err() != nil`) was never exercised — it requires a line longer than 64 KiB, an unusual but valid edge case. Adding the overflow test ensures the error path is verified. The parallelism additions are low-risk improvements to pure-function tests, reducing wall-clock test time with no behavior changes. --- *Generated by Test Improver Workflow* *Focuses on better patterns, increased coverage, and more stable tests* > Generated by [Test Improver](https://github.com/github/gh-aw-mcpg/actions/runs/27887903074) · 1.8K AIC · ⊞ 29.5K · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-improver%22&type=pullrequests) <!-- gh-aw-agentic-workflow: Test Improver, engine: copilot, version: 1.0.60, model: claude-sonnet-4.6, id: 27887903074, workflow_id: test-improver, run: https://github.com/github/gh-aw-mcpg/actions/runs/27887903074 --> <!-- gh-aw-workflow-id: test-improver --> <!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-improver -->
2 parents 9708740 + e871e13 commit d1a2acd

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

internal/sys/container_detect_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ func TestDetectContainerIDWithPaths_MultipleIndicatorLines(t *testing.T) {
217217
// TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound tests that
218218
// extractContainerIDFromCgroupFiles returns the ID from the first file that yields one.
219219
func TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound(t *testing.T) {
220+
t.Parallel()
221+
220222
dir := t.TempDir()
221223

222224
first := writeTempFile(t, dir, "cgroup1", "0::/docker/aabbccdd1234\n")
@@ -229,6 +231,8 @@ func TestExtractContainerIDFromCgroupFiles_ReturnsFirstFound(t *testing.T) {
229231
// TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile tests that a file with
230232
// no container ID is skipped and the next file is tried.
231233
func TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile(t *testing.T) {
234+
t.Parallel()
235+
232236
dir := t.TempDir()
233237

234238
noMatch := writeTempFile(t, dir, "cgroup1", "0::/user.slice\n")
@@ -241,6 +245,8 @@ func TestExtractContainerIDFromCgroupFiles_SkipsEmptyFile(t *testing.T) {
241245
// TestExtractContainerIDFromCgroupFiles_AllEmpty tests that "" is returned when
242246
// no files yield a container ID.
243247
func TestExtractContainerIDFromCgroupFiles_AllEmpty(t *testing.T) {
248+
t.Parallel()
249+
244250
dir := t.TempDir()
245251

246252
f1 := writeTempFile(t, dir, "cgroup1", "0::/\n")
@@ -252,19 +258,22 @@ func TestExtractContainerIDFromCgroupFiles_AllEmpty(t *testing.T) {
252258

253259
// TestExtractContainerIDFromCgroupFiles_NilPaths tests the nil-slice edge case.
254260
func TestExtractContainerIDFromCgroupFiles_NilPaths(t *testing.T) {
261+
t.Parallel()
255262
id := extractContainerIDFromCgroupFiles(nil)
256263
assert.Empty(t, id)
257264
}
258265

259266
// TestExtractContainerIDFromCgroupFiles_EmptyPaths tests the empty-slice edge case.
260267
func TestExtractContainerIDFromCgroupFiles_EmptyPaths(t *testing.T) {
268+
t.Parallel()
261269
id := extractContainerIDFromCgroupFiles([]string{})
262270
assert.Empty(t, id)
263271
}
264272

265273
// TestExtractContainerIDFromCgroupFiles_NonexistentPaths tests that missing files
266274
// are silently skipped and "" is returned when none exist.
267275
func TestExtractContainerIDFromCgroupFiles_NonexistentPaths(t *testing.T) {
276+
t.Parallel()
268277
id := extractContainerIDFromCgroupFiles([]string{"/nonexistent/a", "/nonexistent/b"})
269278
assert.Empty(t, id)
270279
}

internal/sys/container_test.go

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

33
import (
4+
"bufio"
45
"os"
56
"strings"
67
"testing"
@@ -273,6 +274,8 @@ func TestDetectContainerID_ConsistentWithIsRunningInContainer(t *testing.T) {
273274
}
274275

275276
func TestExtractContainerIDFromContent(t *testing.T) {
277+
t.Parallel()
278+
276279
tests := []struct {
277280
name string
278281
content string
@@ -348,10 +351,18 @@ func TestExtractContainerIDFromContent(t *testing.T) {
348351
content: "0::/system.slice/docker.service",
349352
expected: "",
350353
},
354+
{
355+
// A line that exceeds bufio.MaxScanTokenSize (64 KiB) causes the scanner
356+
// to fail with bufio.ErrTooLong. The function logs the error and returns "".
357+
name: "single line exceeding scanner buffer limit returns empty string",
358+
content: strings.Repeat("x", bufio.MaxScanTokenSize+1),
359+
expected: "",
360+
},
351361
}
352362

353363
for _, tt := range tests {
354364
t.Run(tt.name, func(t *testing.T) {
365+
t.Parallel()
355366
result := extractContainerIDFromContent(tt.content)
356367
assert.Equal(t, tt.expected, result)
357368
})
@@ -423,6 +434,8 @@ func containsAny(s string, substrings []string) bool {
423434
}
424435

425436
func TestContainsAny_Helper(t *testing.T) {
437+
t.Parallel()
438+
426439
// Test the helper function itself
427440
tests := []struct {
428441
name string
@@ -482,6 +495,7 @@ func TestContainsAny_Helper(t *testing.T) {
482495

483496
for _, tt := range tests {
484497
t.Run(tt.name, func(t *testing.T) {
498+
t.Parallel()
485499
result := containsAny(tt.input, tt.substrings)
486500
assert.Equal(t, tt.want, result)
487501
})

0 commit comments

Comments
 (0)