Skip to content

Commit

Permalink
Add duration to output. Made json output key names consistent with go…
Browse files Browse the repository at this point in the history
…ss.json
  • Loading branch information
aelsabbahy committed Oct 29, 2015
1 parent 3b7405c commit c9f8212
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
4 changes: 3 additions & 1 deletion cmd/goss/goss.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"time"

"github.com/aelsabbahy/goss"
"github.com/aelsabbahy/goss/outputs"
Expand All @@ -13,6 +14,7 @@ import (
var version string

func main() {
startTime := time.Now()
app := cli.NewApp()
app.EnableBashCompletion = true
app.Version = version
Expand Down Expand Up @@ -49,7 +51,7 @@ func main() {
},
},
Action: func(c *cli.Context) {
goss.Run(c.GlobalString("gossfile"), c)
goss.Run(c.GlobalString("gossfile"), c, startTime)
},
},
{
Expand Down
8 changes: 5 additions & 3 deletions outputs/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package outputs

import (
"fmt"
"time"

"github.com/aelsabbahy/goss/resource"
"github.com/fatih/color"
)

type Documentation struct{}

func (r Documentation) Output(results <-chan []resource.TestResult) (hasFail bool) {
func (r Documentation) Output(results <-chan []resource.TestResult, startTime time.Time) (exitCode int) {
testCount := 0
var failed []resource.TestResult
for resultGroup := range results {
Expand All @@ -35,12 +36,13 @@ func (r Documentation) Output(results <-chan []resource.TestResult) (hasFail boo
fmt.Print("\n")
}

fmt.Printf("Total Duration: %s\n", time.Now().Sub(startTime))
if len(failed) > 0 {
color.Red("Count: %d failed: %d\n", testCount, len(failed))
return true
return 1
}
color.Green("Count: %d failed: %d\n", testCount, len(failed))
return false
return 0
}

func init() {
Expand Down
12 changes: 7 additions & 5 deletions outputs/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package outputs
import (
"encoding/json"
"fmt"
"time"

"github.com/aelsabbahy/goss/resource"
)

type Json struct{}

func (r Json) Output(results <-chan []resource.TestResult) (hasFail bool) {
func (r Json) Output(results <-chan []resource.TestResult, startTime time.Time) (exitCode int) {
testCount := 0
failed := 0
var resultsOut []resource.TestResult
Expand All @@ -24,8 +25,9 @@ func (r Json) Output(results <-chan []resource.TestResult) (hasFail bool) {
}

summary := make(map[string]interface{})
summary["test_count"] = testCount
summary["failed_count"] = failed
summary["test-count"] = testCount
summary["failed-count"] = failed
summary["total-duration"] = time.Now().Sub(startTime)

out := make(map[string]interface{})
out["results"] = resultsOut
Expand All @@ -35,10 +37,10 @@ func (r Json) Output(results <-chan []resource.TestResult) (hasFail bool) {
fmt.Println(string(j))

if failed > 0 {
return true
return 1
}

return false
return 0
}

func init() {
Expand Down
3 changes: 2 additions & 1 deletion outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/aelsabbahy/goss/resource"
"github.com/fatih/color"
)

type Outputer interface {
Output(<-chan []resource.TestResult) bool
Output(<-chan []resource.TestResult, time.Time) int
}

var green = color.New(color.FgGreen).SprintfFunc()
Expand Down
8 changes: 5 additions & 3 deletions outputs/rspecish.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package outputs

import (
"fmt"
"time"

"github.com/aelsabbahy/goss/resource"
"github.com/fatih/color"
)

type Rspecish struct{}

func (r Rspecish) Output(results <-chan []resource.TestResult) (hasFail bool) {
func (r Rspecish) Output(results <-chan []resource.TestResult, startTime time.Time) (exitCode int) {
testCount := 0
var failed []resource.TestResult
for resultGroup := range results {
Expand All @@ -33,12 +34,13 @@ func (r Rspecish) Output(results <-chan []resource.TestResult) (hasFail bool) {
fmt.Print("\n")
}

fmt.Printf("Total Duration: %s\n", time.Now().Sub(startTime))
if len(failed) > 0 {
color.Red("Count: %d failed: %d\n", testCount, len(failed))
return true
return 1
}
color.Green("Count: %d failed: %d\n", testCount, len(failed))
return false
return 0
}

func init() {
Expand Down
18 changes: 9 additions & 9 deletions resource/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const (
)

type TestResult struct {
Successful bool
Title string
ResourceType string
TestType int
Property string
Err error
Expected []string
Found []string
Duration time.Duration
Successful bool `json:"successful"`
Title string `json:"title"`
ResourceType string `json:"resource-type"`
TestType int `json:"test-type"`
Property string `json:"property"`
Err error `json:"err"`
Expected []string `json:"expected"`
Found []string `json:"found"`
Duration time.Duration `json:"duration"`
}

func ValidateValues(res IDer, property string, expectedValues []string, method func() ([]string, error)) TestResult {
Expand Down
8 changes: 4 additions & 4 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"runtime"
"sync"
"time"

"github.com/aelsabbahy/goss/outputs"
"github.com/aelsabbahy/goss/resource"
Expand All @@ -15,7 +16,7 @@ import (
"github.com/fatih/color"
)

func Run(specFile string, c *cli.Context) {
func Run(specFile string, c *cli.Context, startTime time.Time) {
sys := system.New(c)

// handle stdin
Expand Down Expand Up @@ -82,9 +83,8 @@ func Run(specFile string, c *cli.Context) {

outputer := outputs.GetOutputer(c.String("format"))

if hasFail := outputer.Output(out); hasFail {
os.Exit(1)
}
exitCode := outputer.Output(out, startTime)
os.Exit(exitCode)

}

Expand Down

0 comments on commit c9f8212

Please sign in to comment.