Skip to content

Commit

Permalink
unit tests, makefile & readme update (#3)
Browse files Browse the repository at this point in the history
* Adding command line flags (still need to cleanup some of the code)

* adds error message for missing pipe

* Adds unit test for the go side of the code

* adds go test in dockerfile

* some code refactoring and adds more test

* adds support for running go test in the CI docker file.

* adds a default to the groupSize flag

* adds test for groupSize

* some minor test updates

* more refactorings and updated test

* updated tests and added more tests

* adds shorthand flag for the title flag

* removed shortand tests

* moved the stdin checker into the main processing block

* updates to documentation and test

* added minor code-refactoring and fixing some broken test

* adds a short hand for the --groupSize flag

* adds AST parsing code to retrieve the filenames associated with test function names

* added initial code to map test names to their associated go test files.

* Adds the test file with line and col position information

* code cleanup

* added embedded assets into the binary

* updated dockerfile to fix broken build

* removed unnecessary generated code

* added verbose flag

* add total time at the end of process

* removed unneeded code

* updated readme

* adds a build script for mac, linux and windows builds

* updates release build file to create the destination folders before performing builds.

* added a little more clarity to the -groupSize usage description

* updates template to support change the html window title in addition to the title on the page

* updated readme and removed unneeded code

* nomenclature update

* added the embedded_assets.go file

* updates to the default test report template

* added the generate_embedded_go_code bash script

* moved some files around. preparing for commit to master

* moved assets to the root project folder and added test duration to the test report

* tweaked the test report template

* changes default groupSize from 10 to 20.

* small teak to the test report ui

* updated broken test

* added an ellipsis to test names that are to long

* updates the test report ui to show 'n/a' if a test was not found in the package

* Adds test execution date to test report html

* fixes date formatted problem in test execution date

* deleted jeykll file

* adds a unit test for the TestGenerateReport function

* test updates

* updated readme

* updates to readme file

* moved some things around in the readme file

* removed unneeded image in readme

* removing old files

* replacing shell scripts with makefile

* updated Dockerfile to use Makefile for builds

* updates to the readme file

* Removed support for 32bit version of windows binary

* changed default make command
  • Loading branch information
vakenbolt authored Jul 11, 2020
1 parent 91e9f1a commit 2b7d8b3
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 661 deletions.
9 changes: 1 addition & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,5 @@ RUN npm install
RUN npm fund
RUN npm run test

# WORKDIR /home/dockeruser/embed_assets
# RUN go build
# RUN ./embed_assets


# WORKDIR /home/dockeruser

RUN ./generate_embedded_go_code.sh
RUN make genbuild
RUN go test -v
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
genbuild: gencode
go build

gencode:
(cd embed_assets/;set -e;go build;./embed_assets)

buildall: genbuild
echo "Building..."

mkdir -p release_builds/linux-amd64/
mkdir -p release_builds/darwin-amd64/
mkdir -p release_builds/windows-amd64/

echo "Linux 64bit"
GOOS=linux GOARCH=amd64 go build -o release_builds/linux-amd64/

echo "Darwin (MacOS) 64bit"
GOOS=darwin GOARCH=amd64 go build -o release_builds/darwin-amd64/

echo "Windows 64bit"
GOOS=windows GOARCH=amd64 go build -o release_builds/windows-amd64/

echo "...Done!"
86 changes: 63 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# go-test-report
<p align="center">
<br/>
<img src="https://user-images.githubusercontent.com/1223459/87218719-d185e300-c31a-11ea-897b-0db31b956ff1.png" width="700px">
<br/>
<br/>
</p>



[![license: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://shields.io/)
[![version: 0.9](https://img.shields.io/badge/version-0.9-default.svg)](https://shields.io/)
[![version: 0.9](https://img.shields.io/badge/platforms-macos%20|%20windows%20|%20linux-orange.svg)](https://shields.io/)
[![version: 0.9](https://img.shields.io/badge/platforms-macos%20|%20linux%20|%20windows-orange.svg)](https://shields.io/)

go-test-report captures `go test` output and parses it into a _single_ self-contained HTML file.

Expand All @@ -25,11 +32,48 @@ $ go test -json | go-test-report
The aforementioned command outputs an HTML file in the same location.

```shell
go-test-report.html
test_report.html
```

>Everything needed to display the HTML file correctly is located inside of the file, providing an easy way to store and host the test results.
#### Understanding HTML test report

The HTML file generated by `go-test-report` is designed to provide an easy way to navigate test results. At the top of the page are general stats related to the test. Below is the title of the test followed by a section called _"Test Groups"_. Tests are organized into these groups which are represented by color-coded squares. A <span style="color: green">green</span> square indicates that all test in that group passed.

<p align="center">
<br/>
<img src="https://user-images.githubusercontent.com/1223459/87217645-2290d980-c311-11ea-9967-957c4ee8ec61.png" width="700px" style="border: 1px #cccccc solid; padding: 8px">
<br/>
<br/>
</p>

A <span style="color: red">red</span> square indicates that at least one test in that group failed.

<p align="center">
<br/>
<img src="https://user-images.githubusercontent.com/1223459/87218926-c9c73e00-c31c-11ea-9834-057d92de98bd.png" width="216px" style="border: 1px #cccccc solid; padding: 8px">
<br/>
<br/>
</p>

To view the tests in a particular test group, click on any of the test group indicators. A selected group indicator will be colored in black. The number of tests allocated per test group can be set with the `groupSize` command-line flag.

<p align="center">
<br/>
<img src="https://user-images.githubusercontent.com/1223459/87218203-325eec80-c316-11ea-80c9-fea015cd5343.png" width="700px" style="border: 1px #cccccc solid; padding: 8px">
<br/>
<br/>
</p>

To view the output of a related test, click on the title of a test on the list. If you want to expand _all_ of the test on the list, simultaneously press `shift` and the test group indicator.

<p align="center">
<br/>
<img src="https://user-images.githubusercontent.com/1223459/87218282-d9438880-c316-11ea-9a81-54d4cd5b6d85.png" width="700px" style="border: 1px #cccccc solid; padding: 8px">
</p>


## Configuration
Additional configuration options are available via command-line flags.

Expand All @@ -43,7 +87,7 @@ Available Commands:
version Prints the version number of go-test-report
Flags:
-g, --groupSize int the number of tests per test group indicator (default 10)
-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")
Expand All @@ -60,7 +104,7 @@ $ go test -json | go-test-report -o my-test-report.html
```


To change the default title shown in the `go-test-report.html` file.
To change the default title shown in the `test_report.html` file.

```bash
$ go test -json | go-test-report -t "My Test Report"
Expand All @@ -79,39 +123,35 @@ $ go test -json | go-test-report -g 32x16
```

## Building from source
Before running `go build`, build the "embed_assets" program _inside_ of the `embed_assets` folder. To do so, change the working directory to `%PROJECT_HOME%/embed_assets` and run the following commands:

```bash
$ go build
$ go ./embed_assets
```
To build `go-test-report` from source.

> This will generate an `embedded_assets.go` file in the root directory of the project. This file contains the embedded templates used by `go-test-report`.
```bash
$ make genbuild
```

Once the _"embedded_assets.go"_ have been generated, the main binary can be built.
Because `go-test-report` embeds the HTML and Javascript code necessary to generate the report output file, a command to generate the embedded go code is needed.

```bash
$ go build
$ make gencode
```
> Alternatively, `make genbuild` can be used which automatically runs `gencode` _before_ `genbuild`
To build release binaries (not supported on windows):
To build release binaries,

```bash
$ ./build_release.sh
$ make buildall
```
> Creates a folder in the root project folder called `release_builds` that contains builds for the following platforms:
>
> - `darwin/amd64` (MacOS)
> - `linux/amd64`
> - `windows/amd64`
> - `windows/i386`
## F.A.Q.

**What platforms does `go-test-report` support?**
MacOS (64bit), Linux (64bit) and Windoows (32 & 64bit)
## Contribute & Support

**I thought this was a _go_ project, why the need for `npm`**
- Add a GitHub Star
- Suggest [new features, ideas and optimizations](https://github.com/vakenbolt/go-test-report/issues)
- [Report issues](https://github.com/vakenbolt/go-test-report/issues)

**How do I contribute to the project**

22 changes: 0 additions & 22 deletions build_release.sh

This file was deleted.

3 changes: 0 additions & 3 deletions generate_embedded_go_code.sh

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect
golang.org/x/text v0.3.2
gopkg.in/ini.v1 v1.57.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
61 changes: 32 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"html/template"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -111,15 +112,31 @@ func newRootCommand() (*cobra.Command, *TemplateData, *cmdFlags) {
rootCmd := &cobra.Command{
Use: "go-test-report",
Long: "Captures go test output via stdin and parses it into a single self-contained html file.",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (e error) {
startTime := time.Now()
if err := parseSizeFlag(tmplData, flags); err != nil {
return err
}
tmplData.numOfTestsPerGroup = flags.groupSize
tmplData.ReportTitle = flags.titleFlag
tmplData.OutputFilename = flags.outputFlag
if err := generateTestReport(flags, tmplData, cmd); err != nil {
if err := checkIfStdinIsPiped(); err != nil {
return err
}
stdin := os.Stdin
stdinScanner := bufio.NewScanner(stdin)
testReportHTMLTemplateFile, _ := os.Create(tmplData.OutputFilename)
reportFileWriter := bufio.NewWriter(testReportHTMLTemplateFile)
defer func() {
_ = stdin.Close()
if err := reportFileWriter.Flush(); err != nil {
e = err
}
if err := testReportHTMLTemplateFile.Close(); err != nil {
e = err
}
}()
if err := generateReport(stdinScanner, flags, tmplData, reportFileWriter, cmd); err != nil {
return errors.New(err.Error() + "\n")
}
elapsedTime := time.Since(startTime)
Expand Down Expand Up @@ -171,23 +188,12 @@ func newRootCommand() (*cobra.Command, *TemplateData, *cmdFlags) {
return rootCmd, tmplData, flags
}

func generateTestReport(flags *cmdFlags, tmplData *TemplateData, cmd *cobra.Command) error {
stdin := os.Stdin
if err := checkIfStdinIsPiped(); err != nil {
return err
}

func generateReport(stdinScanner *bufio.Scanner, flags *cmdFlags, tmplData *TemplateData, reportFileWriter *bufio.Writer, cmd *cobra.Command) (e error) {
var err error
var allTests = map[string]*TestStatus{}
var allPackageNames = map[string]*types.Nil{}

// read from stdin and parse "go test" results
defer func() {
if err = stdin.Close(); err != nil {
panic(err)
}
}()
stdinScanner := bufio.NewScanner(stdin)
startTestTime := time.Now()
for stdinScanner.Scan() {
stdinScanner.Text()
Expand Down Expand Up @@ -229,7 +235,7 @@ func generateTestReport(flags *cmdFlags, tmplData *TemplateData, cmd *cobra.Comm
// used to the location of test functions in test go files by package and test function name.
testFileDetailByPackage, err := getPackageDetails(allPackageNames)
if err != nil {
return nil
return err
}

// read the html template from the generated embedded asset go file
Expand All @@ -241,17 +247,6 @@ func generateTestReport(flags *cmdFlags, tmplData *TemplateData, cmd *cobra.Comm
if tpl, err := tpl.Parse(string(testReportHtmlTemplateStr)); err != nil {
return err
} else {
testReportHTMLTemplateFile, _ := os.Create(tmplData.OutputFilename)
w := bufio.NewWriter(testReportHTMLTemplateFile)
defer func() {
if err := w.Flush(); err != nil {
panic(err)
}
if err := testReportHTMLTemplateFile.Close(); err != nil {
panic(err)
}
}()

// read Javascript code from the generated embedded asset go file
testReportJsCodeStr, err := hex.DecodeString(testReportJsCode)
if err != nil {
Expand All @@ -264,7 +259,14 @@ func generateTestReport(flags *cmdFlags, tmplData *TemplateData, cmd *cobra.Comm
tgCounter := 0
tgId := 0

for _, status := range allTests {
// sort the allTests map by test name (this will produce a consistent order when iterating through the map)
var testNames []string
for test := range allTests {
testNames = append(testNames, test)
}
sort.Strings(testNames)
for _, testName := range testNames {
status := allTests[testName]
if len(tmplData.TestResults) == tgId {
tmplData.TestResults = append(tmplData.TestResults, &TestGroupData{})
}
Expand Down Expand Up @@ -292,8 +294,9 @@ func generateTestReport(flags *cmdFlags, tmplData *TemplateData, cmd *cobra.Comm
td := time.Now()
tmplData.TestExecutionDate = fmt.Sprintf("%s %d, %d %02d:%02d:%02d",
td.Month(), td.Day(), td.Year(), td.Hour(), td.Minute(), td.Second())

err = tpl.Execute(w, tmplData)
if err := tpl.Execute(reportFileWriter, tmplData); err != nil {
return err
}
}
return nil
}
Expand Down
Loading

0 comments on commit 2b7d8b3

Please sign in to comment.