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 optional parameters for running with gotestsum #62

Open
wants to merge 1 commit 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
go-test-report
go-test-report.exe
test_report.json
test_report.html
embed_assets/embed_assets
release_builds/
Expand Down
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,21 @@ Usage:
go-test-report [command]

Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
version Prints the version number of go-test-report

Flags:
-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")
-s, --size string the size (in pixels) of the clickable indicator for test result groups (default "24")
-t, --title string the title text shown in the test report (default "go-test-report")
-v, --verbose while processing, show the complete output from go test
-e, --elapsed string the test elapsed time from the original test run (in "0.000s" format)
-d, --executionDate string the test execution date of the original test run (in RFC 3339 format)
-g, --groupSize int the number of tests per test group indicator (default 20)
-h, --help help for go-test-report
-i, --input string the JSON input file
-l, --list string the JSON module list
-o, --output string the HTML output file (default "test_report.html")
-s, --size string the size (in pixels) of the clickable indicator for test result groups (default "24")
-t, --title string the title text shown in the test report (default "go-test-report")
-v, --verbose while processing, show the complete output from go test

Use "go-test-report [command] --help" for more information about a command.
```
Expand All @@ -114,6 +119,27 @@ To change the default title shown in the `test_report.html` file.
$ go test -json | go-test-report -t "My Test Report"
```

The default duration will be the time it takes to read the JSON data.
This can be changed by using the `-e` or `--elapsed` flag explicitly.

To report a test duration, from a previously recorded test run:

```bash
$ gotestsum --jsonfile test_report.json
✓ . (6ms)

DONE 18 tests in 0.398s
$ go-test-report -e 0.398s -i test_report.json
```

The default execution time will be when the test report is generated.
To change the test date/time, use the `-d` or `--executionDate` flag.

```
$ date --rfc-3339=seconds
```

When using an input file, the file modification time is used.

The default number of tests in a _test group_ can be changed using the `-g` or `--groupSize` flag. For example, the following command will change the default number of tests in a group to 8.

Expand Down
72 changes: 60 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ type (
}

cmdFlags struct {
titleFlag string
sizeFlag string
groupSize int
listFlag string
outputFlag string
verbose bool
titleFlag string
elapsedFlag string
executionDate string
sizeFlag string
groupSize int
listFlag string
inputFlag string
outputFlag string
verbose bool
}

goListJSONModule struct {
Expand Down Expand Up @@ -126,10 +129,19 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
tmplData.numOfTestsPerGroup = flags.groupSize
tmplData.ReportTitle = flags.titleFlag
tmplData.OutputFilename = flags.outputFlag
if err := checkIfStdinIsPiped(); err != nil {
return err
var stdin *os.File
if flags.inputFlag != "" {
file, err := os.Open(flags.inputFlag)
if err != nil {
return err
}
stdin = file
} else {
if err := checkIfStdinIsPiped(); err != nil {
return err
}
stdin = os.Stdin
}
stdin := os.Stdin
stdinScanner := bufio.NewScanner(stdin)
testReportHTMLTemplateFile, _ := os.Create(tmplData.OutputFilename)
reportFileWriter := bufio.NewWriter(testReportHTMLTemplateFile)
Expand All @@ -148,6 +160,27 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
return errors.New(err.Error() + "\n")
}
elapsedTestTime := time.Since(startTestTime)
if flags.elapsedFlag != "" {
elapsedTestTime, err = time.ParseDuration(flags.elapsedFlag)
if err != nil {
return err
}
}
testExecutionTime := time.Now()
if flags.inputFlag != "" {
st, err := os.Stat(flags.inputFlag)
if err != nil {
return err
}
testExecutionTime = st.ModTime()
}
if flags.executionDate != "" {
testExecutionTime, err = time.Parse(time.RFC3339, flags.executionDate)
if err != nil {
return err
}

}
// used to the location of test functions in test go files by package and test function name.
var testFileDetailByPackage testFileDetailsByPackage
if flags.listFlag != "" {
Expand All @@ -158,7 +191,7 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
if err != nil {
return err
}
err = generateReport(tmplData, allTests, testFileDetailByPackage, elapsedTestTime, reportFileWriter)
err = generateReport(tmplData, allTests, testFileDetailByPackage, elapsedTestTime, testExecutionTime, reportFileWriter)
elapsedTime := time.Since(startTime)
elapsedTimeMsg := []byte(fmt.Sprintf("[go-test-report] finished in %s\n", elapsedTime))
if _, err := cmd.OutOrStdout().Write(elapsedTimeMsg); err != nil {
Expand All @@ -184,6 +217,16 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
"t",
"go-test-report",
"the title text shown in the test report")
rootCmd.PersistentFlags().StringVarP(&flags.elapsedFlag,
"elapsed",
"e",
os.Getenv("GOTESTSUM_ELAPSED"),
"the test elapsed time from the original test run (in \"0.000s\" format)")
rootCmd.PersistentFlags().StringVarP(&flags.executionDate,
"executionDate",
"d",
"",
"the test execution date of the original test run (in RFC 3339 format)")
rootCmd.PersistentFlags().StringVarP(&flags.sizeFlag,
"size",
"s",
Expand All @@ -199,6 +242,11 @@ func initRootCommand() (*cobra.Command, *templateData, *cmdFlags) {
"l",
"",
"the JSON module list")
rootCmd.PersistentFlags().StringVarP(&flags.inputFlag,
"input",
"i",
os.Getenv("GOTESTSUM_JSONFILE"),
"the JSON input file")
rootCmd.PersistentFlags().StringVarP(&flags.outputFlag,
"output",
"o",
Expand Down Expand Up @@ -389,7 +437,7 @@ func (t byName) Less(i, j int) bool {
return t[i].name < t[j].name
}

func generateReport(tmplData *templateData, allTests map[string]*testStatus, testFileDetailByPackage testFileDetailsByPackage, elapsedTestTime time.Duration, reportFileWriter *bufio.Writer) error {
func generateReport(tmplData *templateData, allTests map[string]*testStatus, testFileDetailByPackage testFileDetailsByPackage, elapsedTestTime time.Duration, testExecutionTime time.Time, reportFileWriter *bufio.Writer) error {
// read the html template from the generated embedded asset go file
tpl := template.New("test_report.html.template")
testReportHTMLTemplateStr, err := hex.DecodeString(testReportHTMLTemplate)
Expand Down Expand Up @@ -450,7 +498,7 @@ func generateReport(tmplData *templateData, allTests map[string]*testStatus, tes
}
tmplData.NumOfTests = tmplData.NumOfTestPassed + tmplData.NumOfTestFailed + tmplData.NumOfTestSkipped
tmplData.TestDuration = elapsedTestTime.Round(time.Millisecond)
td := time.Now()
td := testExecutionTime
tmplData.TestExecutionDate = fmt.Sprintf("%s %d, %d %02d:%02d:%02d",
td.Month(), td.Day(), td.Year(), td.Hour(), td.Minute(), td.Second())
if err := tpl.Execute(reportFileWriter, tmplData); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ func TestGenerateReport(t *testing.T) {
},
}
elapsedTestTime := 3 * time.Second
testExecutionTime := time.Now()
writer := bufio.NewWriter(&bytes.Buffer{})
err := generateReport(tmplData, allTests, testFileDetailsByPackage, elapsedTestTime, writer)
err := generateReport(tmplData, allTests, testFileDetailsByPackage, elapsedTestTime, testExecutionTime, writer)
assertions.Nil(err)
assertions.Equal(2, tmplData.NumOfTestPassed)
assertions.Equal(1, tmplData.NumOfTestFailed)
Expand Down