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

fix(options): --verbose option now works as intended (closes #11) #12

Merged
merged 1 commit into from
May 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ dist/**
test-report

# test data (generated when parser_test.go is run)
testdata/*.json
**/testdata/*.json
# any test/sample report (md) in the root
test-report.md
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ Options:

-o, --output <filename> the output filename (default "test-report.md")

-s, --summary produce a summary report only (no details of failed tests)

-t, --title <string> the title text shown in the test report (default "Test Report")

-h, --help help for test-report

-v, --verbose while processing, show the complete output from go test
-v, --verbose while processing, show the (JSON) output from go test
```

The name of the output file can be changed by using the `-o` or `--output` option.
Expand Down Expand Up @@ -168,7 +170,7 @@ The report details section might appear similar to:

## Background

This tool was created to satisfy a desire to incorporate a test report into github
This tool was created to satisfy a desire to incorporate a test report into Github
action job summaries, for which the HTML produced by existing tools was not suitable.

Markdown seemed to offer a better fit for that use case, and so this tool was born.
Expand Down
11 changes: 11 additions & 0 deletions internal/coalesce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package internal

func coalesce[T comparable](values ...T) T {
z := *new(T)
for _, v := range values {
if v != z {
return v
}
}
return z
}
48 changes: 48 additions & 0 deletions internal/coalesce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package internal

import (
"testing"

"github.com/blugnu/test"
)

func TestCoalesce(t *testing.T) {
// ARRANGE
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
{scenario: "(z,nz)",
exec: func(t *testing.T) {
// ACT
result := coalesce(0, 1)

// ASSERT
test.That(t, result).Equals(1)
},
},
{scenario: "(nz,nz)",
exec: func(t *testing.T) {
// ACT
result := coalesce(1, 2)

// ASSERT
test.That(t, result).Equals(1)
},
},
{scenario: "no args",
exec: func(t *testing.T) {
// ACT
result := coalesce[int]()

// ASSERT
test.That(t, result).Equals(0)
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
tc.exec(t)
})
}
}
55 changes: 20 additions & 35 deletions internal/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ type Options struct{}
// parse is a method that parses the command line arguments and returns the
// appropriate command to run (if any).
func (o *Options) Parse() (interface{ Run(*Options) int }, error) {
var (
of string = "test-report.md"
rm reportMode = rmFailedTests
rt string = "Test Report"

opts = struct {
h, help bool
f, full bool
o, output string
s, summary bool
t, title string
}{}
)

opts := struct {
h, help bool
f, full bool
o, output string
s, summary bool
t, title string
v, verbose bool
}{}
if len(os.Args) > 1 {
if os.Args[1] == "version" {
return showVersion{}, nil
Expand All @@ -47,30 +41,21 @@ func (o *Options) Parse() (interface{ Run(*Options) int }, error) {
flags.BoolVar(&opts.summary, "summary", false, "")
flags.StringVar(&opts.t, "t", "", "report title")
flags.StringVar(&opts.title, "title", "", "")
flags.BoolVar(&opts.v, "v", false, "verbose output")
flags.BoolVar(&opts.verbose, "verbose", false, "")
if err := ParseFlags(flags, os.Args[1:]); err != nil {
return nil, err
}
}
of := coalesce(opts.o, opts.output, "test-report.md")
rt := coalesce(opts.t, opts.title, "Test Report")

switch {
case opts.o != "":
of = opts.o
case opts.output != "":
of = opts.output
}

switch {
case opts.t != "":
rt = opts.t
case opts.title != "":
rt = opts.title
}

switch {
case opts.f || opts.full:
rm = rmAllTests
case opts.s || opts.summary:
rm = rmSummaryOnly
}
rm := rmFailedTests
if opts.f || opts.full {
rm = rmAllTests
}
if opts.s || opts.summary {
rm = rmSummaryOnly
}

switch {
Expand All @@ -82,7 +67,7 @@ func (o *Options) Parse() (interface{ Run(*Options) int }, error) {
filename: of,
title: rt,
mode: rm,
parser: &parser{},
parser: &parser{verbose: opts.v || opts.verbose},
}, nil
}
}
16 changes: 16 additions & 0 deletions internal/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ func TestOpts(t *testing.T) {
parser: &parser{},
},
},
{args: []string{"-v"},
result: generateReport{
filename: "test-report.md",
title: "Test Report",
mode: rmFailedTests,
parser: &parser{verbose: true},
},
},
{args: []string{"--verbose"},
result: generateReport{
filename: "test-report.md",
title: "Test Report",
mode: rmFailedTests,
parser: &parser{verbose: true},
},
},
}
for _, tc := range testcases {
t.Run(fmt.Sprintf("%s", tc.args), func(t *testing.T) {
Expand Down
18 changes: 12 additions & 6 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"fmt"
"io"
"math"
"os"
"regexp"
"strings"
"time"
)

type line struct {
Time string `json:"Time"`
Time string `json:"Time,omitempty"`
Action string `json:"Action"`
Package string `json:"Package"`
Package string `json:"Package,omitempty"`
Test *string `json:"Test,omitempty"`
Elapsed *float64 `json:"Elapsed,omitempty"`
Output *string `json:"Output,omitempty"`
Expand All @@ -24,9 +25,10 @@ func (line line) elapsedDur() time.Duration {
}

type parser struct {
pkgs map[string]*packageinfo
tests map[string]map[string]*testinfo
srcref *regexp.Regexp
pkgs map[string]*packageinfo
tests map[string]map[string]*testinfo
srcref *regexp.Regexp
verbose bool
}

func (p *parser) parse(r io.Reader, rpt *testrun) error {
Expand All @@ -35,6 +37,10 @@ func (p *parser) parse(r io.Reader, rpt *testrun) error {
p.srcref, _ = regexp.Compile(`(.*\.go:[0-9]*): (.*)\n`)

*rpt = testrun{}
echo := func([]byte) (int, error) { return 0, nil }
if p.verbose {
echo = os.Stdout.Write
}

decoder := json.NewDecoder(r)
for {
Expand All @@ -44,7 +50,7 @@ func (p *parser) parse(r io.Reader, rpt *testrun) error {
}

s, _ := json.Marshal(l)
fmt.Println(string(s))
_, _ = echo(s)

if l.Test == nil && l.Elapsed != nil {
rpt.elapsed = l.elapsedDur()
Expand Down
30 changes: 30 additions & 0 deletions internal/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ func TestParse(t *testing.T) {
test.That(t, report.numSkipped).Equals(0, "tests skipped")
},
},
{scenario: "verbose==true",
exec: func(t *testing.T) {
report := &testrun{}
input := bytes.NewReader([]byte(`{"Action":"output"}`))
p := parser{verbose: true}

// ACT
stdout, _ := test.CaptureOutput(t, func() {
_ = p.parse(input, report)
})

// ASSERT
stdout.Equals([]string{`{"Action":"output"}`})
},
},
{scenario: "verbose==false",
exec: func(t *testing.T) {
report := &testrun{}
input := bytes.NewReader([]byte(`{"Action":"output"}`))
p := parser{verbose: false}

// ACT
stdout, _ := test.CaptureOutput(t, func() {
_ = p.parse(input, report)
})

// ASSERT
stdout.IsEmpty()
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion internal/testdata/no-code.json

This file was deleted.

3 changes: 0 additions & 3 deletions internal/testdata/no-test-files.json

This file was deleted.

54 changes: 0 additions & 54 deletions internal/testdata/packages.json

This file was deleted.