Skip to content

Commit

Permalink
Merge pull request #1 from DarkDrim/group-package
Browse files Browse the repository at this point in the history
Support grouping by package name
  • Loading branch information
DarkDrim authored Jan 21, 2024
2 parents e2c594b + 2224d14 commit a543d9e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ The default number of tests in a _test group_ can be changed using the `-g` or `
$ go test -json | go-test-report -g 8
```

To group the tests by their package instead of by count, use the `-p` or `--groupPackage` flag.

```bash
$ go test -json | go-test-report -p
```


Use the `-s` or `--size` flag to change the default size of the _group size indicator_. For example, the following command will set both the width and height of the size of the indicator to 48 pixels.
Expand Down
2 changes: 1 addition & 1 deletion embedded_assets.go

Large diffs are not rendered by default.

88 changes: 76 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type (
TestName string
Package string
ElapsedTime float64
Order int
Output []string
Passed bool
Skipped bool
Expand All @@ -57,23 +58,28 @@ type (
ReportTitle string
JsCode template.JS
numOfTestsPerGroup int
groupTestsByPackage bool
sortTestsByName bool
OutputFilename string
TestExecutionDate string
}

testGroupData struct {
FailureIndicator string
SkippedIndicator string
Title string
TestResults []*testStatus
}

cmdFlags struct {
titleFlag string
sizeFlag string
groupSize int
listFlag string
outputFlag string
verbose bool
titleFlag string
sizeFlag string
groupSize int
groupPackage bool
nosortFlag bool
listFlag string
outputFlag string
verbose bool
}

goListJSONModule struct {
Expand Down Expand Up @@ -124,6 +130,8 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
return err
}
tmplData.numOfTestsPerGroup = flags.groupSize
tmplData.groupTestsByPackage = flags.groupPackage
tmplData.sortTestsByName = !flags.nosortFlag
tmplData.ReportTitle = flags.titleFlag
tmplData.OutputFilename = flags.outputFlag
if err := checkIfStdinIsPiped(); err != nil {
Expand Down Expand Up @@ -182,6 +190,11 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
},
}
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().BoolVarP(&flags.nosortFlag,
"no-sort",
"",
false,
"don't sort the packages by name in the list")
rootCmd.PersistentFlags().StringVarP(&flags.titleFlag,
"title",
"t",
Expand All @@ -197,6 +210,11 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
"g",
20,
"the number of tests per test group indicator")
rootCmd.PersistentFlags().BoolVarP(&flags.groupPackage,
"groupPackage",
"p",
false,
"group tests by package instead of by count")
rootCmd.PersistentFlags().StringVarP(&flags.listFlag,
"list",
"l",
Expand All @@ -221,6 +239,7 @@ func readTestDataFromStdIn(stdinScanner *bufio.Scanner, flags *cmdFlags, cmd *co
allPackageNames = map[string]*types.Nil{}

// read from stdin and parse "go test" results
order := 0
for stdinScanner.Scan() {
lineInput := stdinScanner.Bytes()
if flags.verbose {
Expand All @@ -240,9 +259,11 @@ func readTestDataFromStdIn(stdinScanner *bufio.Scanner, flags *cmdFlags, cmd *co
status = &testStatus{
TestName: goTestOutputRow.TestName,
Package: goTestOutputRow.Package,
Order: order,
Output: []string{},
}
allTests[key] = status
order += 1
} else {
status = allTests[key]
}
Expand All @@ -267,6 +288,7 @@ func readTestDataFromStdIn(stdinScanner *bufio.Scanner, flags *cmdFlags, cmd *co

func getAllDetails(listFile string) (testFileDetailsByPackage, error) {
testFileDetailByPackage := testFileDetailsByPackage{}
// #nosec
f, err := os.Open(listFile)
defer func() {
_ = f.Close()
Expand Down Expand Up @@ -380,16 +402,34 @@ func getFileDetails(goListJSON *goListJSON) (testFileDetailsByTest, error) {

type testRef struct {
key string
pkg string
name string
ord int
}

type byPackage []testRef
type byName []testRef

func (t byPackage) Len() int {
return len(t)
}

func (t byPackage) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

func (t byPackage) Less(i, j int) bool {
return t[i].pkg < t[j].pkg
}

func (t byName) Len() int {
return len(t)
}

func (t byName) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}

func (t byName) Less(i, j int) bool {
return t[i].name < t[j].name
}
Expand All @@ -414,18 +454,37 @@ func generateReport(tmplData *templateData, allTests map[string]*testStatus, tes
tmplData.NumOfTestPassed = 0
tmplData.NumOfTestFailed = 0
tmplData.NumOfTestSkipped = 0
// #nosec
tmplData.JsCode = template.JS(testReportJsCodeStr)
tgPackage := ""
tgCounter := 0
tgID := 0

// sort the allTests map by test name (this will produce a consistent order when iterating through the map)
var tests []testRef
for test, status := range allTests {
tests = append(tests, testRef{test, status.TestName})
tests = append(tests, testRef{test, status.Package, status.TestName, status.Order})
}
// sort the allTests map by input order
sort.Slice(tests, func(i, j int) bool {
return tests[i].ord < tests[j].ord
})
if tmplData.groupTestsByPackage {
sort.Stable(byPackage(tests))
if tmplData.sortTestsByName {
sort.Stable(byName(tests))
}
} else if tmplData.sortTestsByName {
sort.Sort(byName(tests))
}
sort.Sort(byName(tests))
for _, test := range tests {
status := allTests[test.key]
if tmplData.groupTestsByPackage {
if tgPackage != "" && status.Package != tgPackage {
tgID++
}
tgPackage = status.Package
}
if len(tmplData.TestResults) == tgID {
tmplData.TestResults = append(tmplData.TestResults, &testGroupData{})
}
Expand All @@ -435,6 +494,9 @@ func generateReport(tmplData *templateData, allTests map[string]*testStatus, tes
status.TestFileName = testFileInfo.FileName
status.TestFunctionDetail = testFileInfo.TestFunctionFilePos
}
if tmplData.groupTestsByPackage {
tmplData.TestResults[tgID].Title = tgPackage
}
tmplData.TestResults[tgID].TestResults = append(tmplData.TestResults[tgID].TestResults, status)
if !status.Passed {
if !status.Skipped {
Expand All @@ -447,10 +509,12 @@ func generateReport(tmplData *templateData, allTests map[string]*testStatus, tes
} else {
tmplData.NumOfTestPassed++
}
tgCounter++
if tgCounter == tmplData.numOfTestsPerGroup {
tgCounter = 0
tgID++
if !tmplData.groupTestsByPackage {
tgCounter++
if tgCounter == tmplData.numOfTestsPerGroup {
tgCounter = 0
tgID++
}
}
}
tmplData.NumOfTests = tmplData.NumOfTestPassed + tmplData.NumOfTestFailed + tmplData.NumOfTestSkipped
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func TestGenerateReport(t *testing.T) {
TestResultGroupIndicatorWidth: "20px",
TestResultGroupIndicatorHeight: "16px",
ReportTitle: "test-title",
sortTestsByName: true,
numOfTestsPerGroup: 2,
OutputFilename: "test-output-report.html",
}
Expand Down
2 changes: 1 addition & 1 deletion test_report.html.template
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
<div class="cardContainer">
<div id="testResults">
{{range $k, $v := .TestResults}}
<div class="testResultGroup {{.FailureIndicator}} {{.SkippedIndicator}}" id="{{$k}}"></div>
<div class="testResultGroup {{.FailureIndicator}} {{.SkippedIndicator}}" id="{{$k}}"{{if $v.Title}} title="{{$v.Title}}"{{end}}></div>
{{end}}
</div>
</div>
Expand Down

0 comments on commit a543d9e

Please sign in to comment.