Skip to content
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

Add flag to group tests by package #63

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Available Commands:
version Prints the version number of go-test-report

Flags:
-p, --groupPackage group tests by package instead of by number
-g, --groupSize int the number of tests per test group indicator (default 20)
-h, --help help for go-test-report
-o, --output string the HTML output file (default "test_report.html")
Expand Down Expand Up @@ -121,6 +122,12 @@ 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.

82 changes: 69 additions & 13 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 @@ -179,6 +187,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 @@ -194,6 +207,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 @@ -218,6 +236,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 @@ -237,9 +256,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 Down Expand Up @@ -375,10 +396,23 @@ 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)
}
Expand Down Expand Up @@ -410,17 +444,34 @@ func generateReport(tmplData *templateData, allTests map[string]*testStatus, tes
tmplData.NumOfTestFailed = 0
tmplData.NumOfTestSkipped = 0
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 @@ -430,6 +481,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 @@ -442,10 +496,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