Skip to content

Commit eb3964d

Browse files
committed
add outputs
1 parent 80f1c54 commit eb3964d

9 files changed

Lines changed: 136 additions & 28 deletions

File tree

cmd/commander/commander.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"github.com/commander-cli/commander/pkg/output"
56
"io/ioutil"
67
"log"
78
"os"
@@ -75,8 +76,8 @@ commander test commander.yaml --filter="^filter1$"
7576
Flags: []cli.Flag{
7677
cli.BoolFlag{
7778
Name: "no-color",
78-
EnvVar: "COMMANDER_NO_COLOR",
7979
Usage: "Activate or deactivate colored output",
80+
EnvVar: "COMMANDER_NO_COLOR",
8081
},
8182
cli.BoolFlag{
8283
Name: "verbose",
@@ -95,6 +96,11 @@ commander test commander.yaml --filter="^filter1$"
9596
Name: "filter",
9697
Usage: `Filter tests by a given regex pattern. Tests are filtered by its title.`,
9798
},
99+
cli.StringFlag{
100+
Name: "format",
101+
Usage: `Use a different test output format. Available are: cli, tap`,
102+
Value: output.CLI,
103+
},
98104
},
99105
Action: func(c *cli.Context) error {
100106
return app.TestCommand(c.Args().First(), app.NewTestContextFromCli(c))

pkg/app/app.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type TestCommandContext struct {
1919
Workdir string
2020
Concurrent int
2121
Filters []string
22+
Format string
2223
}
2324

2425
//NewTestContextFromCli is a constructor which creates the context
@@ -30,5 +31,6 @@ func NewTestContextFromCli(c *cli.Context) TestCommandContext {
3031
Workdir: c.String("workdir"),
3132
Concurrent: c.Int("concurrent"),
3233
Filters: c.StringSlice("filter"),
34+
Format: c.String("format"),
3335
}
3436
}

pkg/app/test_command.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/commander-cli/commander/pkg/suite"
1616
)
1717

18-
var out output.OutputWriter
18+
var out output.Output
1919

2020
// TestCommand executes the test argument
2121
// testPath is the path to the test suite config, it can be a dir or file
@@ -33,14 +33,16 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
3333
log.SetOutput(os.Stdout)
3434
}
3535

36-
out = output.NewCliOutput(!ctx.NoColor)
36+
out, err := output.NewOutput(ctx.Format, !ctx.NoColor)
37+
if err != nil {
38+
return err
39+
}
3740

3841
if testPath == "" {
3942
testPath = CommanderFile
4043
}
4144

4245
var result runtime.Result
43-
var err error
4446
switch {
4547
case ctx.Dir:
4648
fmt.Println("Starting test against directory: " + testPath + "...")
@@ -64,7 +66,9 @@ func TestCommand(testPath string, ctx TestCommandContext) error {
6466
return fmt.Errorf(err.Error())
6567
}
6668

67-
if !out.PrintSummary(result) && !ctx.Verbose {
69+
70+
out.PrintSummary(result)
71+
if result.Failed != 0 && !ctx.Verbose {
6872
return fmt.Errorf("Test suite failed, use --verbose for more detailed output")
6973
}
7074

pkg/output/cli.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ import (
1111
"github.com/logrusorgru/aurora"
1212
)
1313

14-
// OutputWriter represents the output
15-
type OutputWriter struct {
14+
var _ Output = (*CLIOutputWriter)(nil)
15+
16+
// CLIOutputWriter represents the output
17+
type CLIOutputWriter struct {
1618
out io.Writer
1719
au aurora.Aurora
1820
template cliTemplate
1921
}
2022

21-
// NewCliOutput creates a new OutputWriter with a stdout writer
22-
func NewCliOutput(color bool) OutputWriter {
23+
// NewCliOutput creates a new C̄LIOutputWriter with a stdout writer
24+
func NewCliOutput(color bool) Output {
2325
au := aurora.NewAurora(color)
2426
if run.GOOS == "windows" {
2527
au = aurora.NewAurora(false)
2628
}
2729

2830
t := newCliTemplate()
2931

30-
return OutputWriter{
32+
return CLIOutputWriter{
3133
out: os.Stdout,
3234
au: au,
3335
template: t,
@@ -48,7 +50,7 @@ type TestResult struct {
4850
}
4951

5052
// GetEventHandler create a new runtime.EventHandler
51-
func (w *OutputWriter) GetEventHandler() *runtime.EventHandler {
53+
func (w CLIOutputWriter) GetEventHandler() *runtime.EventHandler {
5254
handler := runtime.EventHandler{}
5355
handler.TestFinished = func(testResult runtime.TestResult) {
5456
tr := convertTestResult(testResult)
@@ -64,7 +66,7 @@ func (w *OutputWriter) GetEventHandler() *runtime.EventHandler {
6466
}
6567

6668
// PrintSummary prints summary
67-
func (w *OutputWriter) PrintSummary(result runtime.Result) bool {
69+
func (w CLIOutputWriter) PrintSummary(result runtime.Result) {
6870
if result.Failed > 0 {
6971
w.printFailures(result.TestResults)
7072
}
@@ -77,24 +79,22 @@ func (w *OutputWriter) PrintSummary(result runtime.Result) bool {
7779
} else {
7880
w.fprintf(w.au.Green(summary))
7981
}
80-
81-
return result.Failed == 0
8282
}
8383

8484
// printResult prints the simple output form of a TestReault
85-
func (w *OutputWriter) printResult(r TestResult) {
85+
func (w CLIOutputWriter) printResult(r TestResult) {
8686
if !r.Success {
8787
w.fprintf(w.au.Red(w.template.testResult(r)))
8888
return
8989
}
9090
w.fprintf(w.template.testResult(r))
9191
}
9292

93-
func (w *OutputWriter) printSkip(r TestResult) {
93+
func (w CLIOutputWriter) printSkip(r TestResult) {
9494
w.fprintf(fmt.Sprintf("- [%s] %s, was skipped", r.Node, r.Title))
9595
}
9696

97-
func (w *OutputWriter) printFailures(results []runtime.TestResult) {
97+
func (w CLIOutputWriter) printFailures(results []runtime.TestResult) {
9898
w.fprintf("")
9999
w.fprintf(w.au.Bold("Results"))
100100
w.fprintf(w.au.Bold(""))
@@ -118,7 +118,7 @@ func (w *OutputWriter) printFailures(results []runtime.TestResult) {
118118
}
119119
}
120120

121-
func (w *OutputWriter) fprintf(a ...interface{}) {
121+
func (w CLIOutputWriter) fprintf(a ...interface{}) {
122122
if _, err := fmt.Fprintln(w.out, a...); err != nil {
123123
log.Fatal(err)
124124
}

pkg/output/cli_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func Test_NewCliOutput(t *testing.T) {
1313
got := NewCliOutput(true)
14-
assert.IsType(t, OutputWriter{}, got)
14+
assert.IsType(t, CLIOutputWriter{}, got)
1515
}
1616

1717
func Test_GetEventHandler(t *testing.T) {
@@ -22,8 +22,9 @@ func Test_GetEventHandler(t *testing.T) {
2222

2323
func Test_EventHandlerTestFinished(t *testing.T) {
2424
var buf bytes.Buffer
25-
writer := NewCliOutput(true)
26-
writer.out = &buf
25+
writer := CLIOutputWriter{
26+
out: &buf,
27+
}
2728
eh := writer.GetEventHandler()
2829

2930
testResults := createFakeTestResults()
@@ -39,8 +40,9 @@ func Test_EventHandlerTestFinished(t *testing.T) {
3940

4041
func Test_EventHandlerTestSkipped(t *testing.T) {
4142
var buf bytes.Buffer
42-
writer := NewCliOutput(true)
43-
writer.out = &buf
43+
writer := CLIOutputWriter{
44+
out: &buf,
45+
}
4446
eh := writer.GetEventHandler()
4547

4648
testResults := createFakeTestResults()
@@ -62,11 +64,11 @@ func Test_PrintSummary(t *testing.T) {
6264
}
6365

6466
var buf bytes.Buffer
65-
writer := NewCliOutput(true)
66-
writer.out = &buf
67+
writer := CLIOutputWriter{
68+
out: &buf,
69+
}
6770

68-
outResult := writer.PrintSummary(r)
69-
assert.False(t, outResult)
71+
writer.PrintSummary(r)
7072

7173
output := buf.String()
7274
assert.Contains(t, output, "✗ [192.168.0.1] 'Failed test', on property 'Stdout'")

pkg/output/output.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package output
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"github.com/commander-cli/commander/pkg/runtime"
7+
)
8+
9+
const (
10+
TAP = "tap"
11+
CLI = "cli"
12+
)
13+
14+
type Output interface {
15+
GetEventHandler() *runtime.EventHandler
16+
PrintSummary(result runtime.Result)
17+
}
18+
19+
// NewOutput creates a new output
20+
func NewOutput(format string, color bool) (Output, error) {
21+
switch format {
22+
case TAP:
23+
return NewTAPOutputWriter(), nil
24+
case CLI:
25+
return NewCliOutput(color), nil
26+
}
27+
return nil, errors.New(fmt.Sprintf("Invalid format type %s", format))
28+
}

pkg/output/output_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package output
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestNewOutput(t *testing.T) {
9+
var o Output
10+
o, err := NewOutput(CLI, false)
11+
assert.NoError(t, err)
12+
assert.Implements(t, (*Output)(nil), o)
13+
}

pkg/output/tap.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package output
2+
3+
import (
4+
"fmt"
5+
"github.com/commander-cli/commander/pkg/runtime"
6+
"io"
7+
"log"
8+
"os"
9+
)
10+
11+
var _ Output = (*CLIOutputWriter)(nil)
12+
13+
// TAPOutputWriter writes TAP results
14+
type TAPOutputWriter struct {
15+
out io.Writer
16+
}
17+
18+
// NewTAPOutputWriter represents the output, defaults to stdout
19+
func NewTAPOutputWriter() Output {
20+
return TAPOutputWriter{
21+
out: os.Stdout,
22+
}
23+
}
24+
25+
func (w TAPOutputWriter) GetEventHandler() *runtime.EventHandler {
26+
return nil
27+
}
28+
29+
func (w TAPOutputWriter) PrintSummary(result runtime.Result) {
30+
counter := 0
31+
for _, r := range result.TestResults {
32+
if r.Skipped {
33+
// skipped tests are not specified in the TAP specification
34+
continue
35+
}
36+
37+
counter++
38+
if r.FailedProperty != "" {
39+
w.fprintf("%d ok - %s", counter+1, r.TestCase.Title)
40+
}
41+
if r.FailedProperty != "" {
42+
w.fprintf("%d not ok - %s", counter+1, r.TestCase.Title)
43+
}
44+
}
45+
46+
w.fprintf("1..%d", counter)
47+
}
48+
49+
func (w TAPOutputWriter) fprintf(msg string , a ...interface{}) {
50+
if _, err := fmt.Fprintln(w.out, fmt.Sprintf(msg, a...)); err != nil {
51+
log.Fatal(err)
52+
}
53+
}

pkg/runtime/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type TestResult struct {
123123
Skipped bool
124124
}
125125

126-
// Result respresents the aggregation of all TestResults/summary of a runtime
126+
// Result represents the aggregation of all TestResults/summary of a runtime
127127
type Result struct {
128128
TestResults []TestResult
129129
Duration time.Duration

0 commit comments

Comments
 (0)