Skip to content

Commit 172ae16

Browse files
Merge pull request #14 from joergmis/filenames
reporter: enrich tests with filenames
2 parents e96e54d + 949b5e5 commit 172ae16

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

reporter/reporter.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"io/fs"
78
"os"
9+
"path/filepath"
10+
"regexp"
811
"strings"
912
"time"
1013

@@ -91,8 +94,50 @@ func ParseTestResults(r io.Reader, verbose bool, env *ctrf.Environment) (*ctrf.R
9194
}
9295

9396
}
97+
98+
enrichReportWithFilenames(report)
99+
94100
return report, nil
95101
}
102+
103+
func generateTestMap() map[string][]string {
104+
tests := map[string][]string{}
105+
106+
r := regexp.MustCompile(`Test.\w+`)
107+
if err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
108+
if !strings.HasSuffix(path, "_test.go") || err != nil {
109+
return nil
110+
}
111+
112+
data, err := os.ReadFile(path)
113+
if err != nil {
114+
return nil
115+
}
116+
117+
tests[path] = r.FindAllString(string(data), -1)
118+
119+
return nil
120+
}); err != nil {
121+
return tests
122+
}
123+
124+
return tests
125+
}
126+
127+
func enrichReportWithFilenames(report *ctrf.Report) {
128+
tests := generateTestMap()
129+
130+
for i, testResult := range report.Results.Tests {
131+
for file, names := range tests {
132+
for _, name := range names {
133+
if strings.Contains(testResult.Name, name) {
134+
report.Results.Tests[i].Filepath = file
135+
}
136+
}
137+
}
138+
}
139+
}
140+
96141
func getMessagesForTest(testEvents []TestEvent, index int, packageName, testName string) string {
97142
var messages []string
98143
for i := index; i >= 0; i-- {

reporter/reporter_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package reporter_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/ctrf-io/go-ctrf-json-reporter/ctrf"
8+
"github.com/ctrf-io/go-ctrf-json-reporter/reporter"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func Test_Enrich_Reporter(t *testing.T) {
13+
expected := &ctrf.Report{Results: &ctrf.Results{Tests: []*ctrf.TestResult{
14+
{
15+
Name: "Test_Enrich_Reporter",
16+
Status: "passed",
17+
Suite: "github.com/ctrf-io/go-ctrf-json-reporter/reporter",
18+
Filepath: "reporter_test.go",
19+
},
20+
}}}
21+
input := `{"Time":"2025-03-02T01:08:01.832222033+01:00","Action":"start","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter"}
22+
{"Time":"2025-03-02T01:08:01.832309292+01:00","Action":"run","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter"}
23+
{"Time":"2025-03-02T01:08:01.832321979+01:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter","Output":"=== RUN Test_Enrich_Reporter\n"}
24+
{"Time":"2025-03-02T01:08:01.832333869+01:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter","Output":"--- PASS: Test_Enrich_Reporter (0.00s)\n"}
25+
{"Time":"2025-03-02T01:08:01.832339962+01:00","Action":"pass","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Test":"Test_Enrich_Reporter","Elapsed":0}
26+
{"Time":"2025-03-02T01:08:01.832347177+01:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Output":"PASS\n"}
27+
{"Time":"2025-03-02T01:08:01.83235318+01:00","Action":"output","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Output":"ok \tgithub.com/ctrf-io/go-ctrf-json-reporter/reporter\t(cached)\n"}
28+
{"Time":"2025-03-02T01:08:01.832359242+01:00","Action":"pass","Package":"github.com/ctrf-io/go-ctrf-json-reporter/reporter","Elapsed":0}`
29+
30+
actual, err := reporter.ParseTestResults(bytes.NewBufferString(input), false, &ctrf.Environment{})
31+
32+
assert.Nil(t, err)
33+
assert.Equal(t, expected.Results.Tests, actual.Results.Tests)
34+
}

0 commit comments

Comments
 (0)