-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(logger): attribute parallel logs to the correct test under go test -json #1873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
james00012
merged 4 commits into
gruntwork-io:main
from
james00012:fix/json-log-attribution-1871
Jul 24, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
985e837
fix(logger): attribute parallel logs to the correct test under go tes…
james00012 6c6132a
fix(logger/parser): de-interleave parallel logs emitted through t.Log
james00012 2f5fa82
chore: retrigger checks
james00012 4c4dbd8
Merge branch 'main' into fix/json-log-attribution-1871
james00012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package parser_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/gruntwork-io/terratest/modules/core/v2/logger/parser" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIsIndentedTerratestLogLine(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| in string | ||
| out bool | ||
| }{ | ||
| { | ||
| name: "IndentedTerratestLine", | ||
| in: " apply_test.go:42: TestFoo 2026-07-18T13:36:46-04:00 logger.go:81: applying", | ||
| out: true, | ||
| }, | ||
| { | ||
| name: "IndentedSubtestLine", | ||
| in: " apply_test.go:42: TestFoo/Sub1 2026-07-18T13:36:46-04:00 logger.go:81: applying", | ||
| out: true, | ||
| }, | ||
| { | ||
| name: "UnindentedTerratestLine", | ||
| in: "TestFoo 2026-07-18T13:36:46-04:00 logger.go:81: applying", | ||
| out: false, | ||
| }, | ||
| { | ||
| name: "PlainTLogLine", | ||
| in: " apply_test.go:42: some plain message", | ||
| out: false, | ||
| }, | ||
| { | ||
| name: "IndentedResultLine", | ||
| in: " --- PASS: TestFoo (0.02s)", | ||
| out: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| testCase := testCase | ||
| t.Run(testCase.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| assert.Equal(t, testCase.out, parser.IsIndentedTerratestLogLine(testCase.in)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetTestNameFromIndentedTerratestLogLine(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| assert.Equal(t, "TestFoo", parser.GetTestNameFromIndentedTerratestLogLine( | ||
| " apply_test.go:42: TestFoo 2026-07-18T13:36:46-04:00 logger.go:81: applying")) | ||
| assert.Equal(t, "TestFoo/Sub1", parser.GetTestNameFromIndentedTerratestLogLine( | ||
| " apply_test.go:42: TestFoo/Sub1 2026-07-18T13:36:46-04:00 logger.go:81: applying")) | ||
| } | ||
|
|
||
| // TestSpawnParsersDeinterleavesTLogOutput verifies that the parser de-interleaves parallel-test output that terratest | ||
| // now emits through t.Log (indented and decorated by the testing framework). Both tests resume up front, so the | ||
| // `=== CONT` status lines cannot distinguish them; correct attribution relies on the test name embedded in each line. | ||
| func TestSpawnParsersDeinterleavesTLogOutput(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Interleaved `go test -v` output (non-JSON) as produced after logging is routed through t.Log. | ||
| sample := strings.Join([]string{ | ||
| "=== RUN TestParA", | ||
| "=== PAUSE TestParA", | ||
| "=== RUN TestParB", | ||
| "=== PAUSE TestParB", | ||
| "=== CONT TestParA", | ||
| "=== CONT TestParB", | ||
| " a_test.go:10: TestParA 2026-07-18T13:36:46-04:00 logger.go:81: MARKA payload 0", | ||
| " b_test.go:20: TestParB 2026-07-18T13:36:46-04:00 logger.go:81: MARKB payload 0", | ||
| " b_test.go:20: TestParB 2026-07-18T13:36:46-04:00 logger.go:81: MARKB payload 1", | ||
| " a_test.go:10: TestParA 2026-07-18T13:36:46-04:00 logger.go:81: MARKA payload 1", | ||
| " a_test.go:10: TestParA 2026-07-18T13:36:46-04:00 logger.go:81: MARKA payload 2", | ||
| " b_test.go:20: TestParB 2026-07-18T13:36:46-04:00 logger.go:81: MARKB payload 2", | ||
| "--- PASS: TestParA (0.02s)", | ||
| "--- PASS: TestParB (0.02s)", | ||
| "PASS", | ||
| "ok \tpkg\t0.10s", | ||
| "", | ||
| }, "\n") | ||
|
|
||
| out := t.TempDir() | ||
| parser.SpawnParsers(logrus.New(), strings.NewReader(sample), out) | ||
|
|
||
| readLog := func(test string) string { | ||
| b, err := os.ReadFile(filepath.Join(out, test+".log")) | ||
| require.NoError(t, err, "expected a log file for %s", test) | ||
|
|
||
| return string(b) | ||
| } | ||
|
|
||
| a := readLog("TestParA") | ||
| assert.Equal(t, 3, strings.Count(a, "MARKA"), "TestParA.log should contain all of its own lines") | ||
| assert.NotContains(t, a, "MARKB", "TestParA.log should not contain TestParB output") | ||
|
|
||
| b := readLog("TestParB") | ||
| assert.Equal(t, 3, strings.Count(b, "MARKB"), "TestParB.log should contain all of its own lines") | ||
| assert.NotContains(t, b, "MARKA", "TestParB.log should not contain TestParA output") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Add a bounds check to prevent potential panics.
Because this function is exported, it can be called by other packages with arbitrary strings. If the input string doesn't match the regex,
FindStringSubmatchreturnsnil, and indexingm[1]will cause a runtime panic.Even though it's called safely within this file's switch block, adding a quick bounds check makes the function robust for any future use. (Note: The other exported helpers in this file share this vulnerability and could also benefit from similar checks.)
🛡️ Proposed fix
func GetTestNameFromIndentedTerratestLogLine(text string) string { m := regexIndentedTerratestLog.FindStringSubmatch(text) - return m[1] + if len(m) > 1 { + return m[1] + } + return "" }📝 Committable suggestion
🤖 Prompt for AI Agents