Skip to content

Commit

Permalink
Merge pull request #5 from aelsabbahy/feature/cleanup_output
Browse files Browse the repository at this point in the history
Feature/cleanup output
  • Loading branch information
aelsabbahy committed Oct 26, 2015
2 parents 5f0b979 + e202f7e commit bd89865
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 53 deletions.
10 changes: 6 additions & 4 deletions outputs/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (r Documentation) Output(results <-chan []resource.TestResult) (hasFail boo
var failed []resource.TestResult
for resultGroup := range results {
for _, testResult := range resultGroup {
if testResult.Result {
if testResult.Successful {
fmt.Println(humanizeResult(testResult))
testCount++
} else {
Expand All @@ -26,18 +26,20 @@ func (r Documentation) Output(results <-chan []resource.TestResult) (hasFail boo
fmt.Println("")
}

fmt.Print("\n")
if len(failed) > 0 {
color.Red("\nFailures:")
color.Red("Failures:")
for _, testResult := range failed {
fmt.Println(humanizeResult(testResult))
}
fmt.Print("\n")
}

if len(failed) > 0 {
color.Red("\nCount: %d failed: %d\n", testCount, len(failed))
color.Red("Count: %d failed: %d\n", testCount, len(failed))
return true
}
color.Green("\nCount: %d failed: %d\n", testCount, len(failed))
color.Green("Count: %d failed: %d\n", testCount, len(failed))
return false
}

Expand Down
46 changes: 46 additions & 0 deletions outputs/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package outputs

import (
"encoding/json"
"fmt"

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

type Json struct{}

func (r Json) Output(results <-chan []resource.TestResult) (hasFail bool) {
testCount := 0
failed := 0
var resultsOut []resource.TestResult
for resultGroup := range results {
for _, testResult := range resultGroup {
if !testResult.Successful {
failed++
}
resultsOut = append(resultsOut, testResult)
testCount++
}
}

summary := make(map[string]interface{})
summary["test_count"] = testCount
summary["failed_count"] = failed

out := make(map[string]interface{})
out["results"] = resultsOut
out["summary"] = summary

j, _ := json.MarshalIndent(out, "", " ")
fmt.Println(string(j))

if failed > 0 {
return true
}

return false
}

func init() {
RegisterOutputer("json", &Json{})
}
30 changes: 24 additions & 6 deletions outputs/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ var red = color.New(color.FgRed).SprintfFunc()

func humanizeResult(r resource.TestResult) string {
if r.Err != nil {
return fmt.Sprintf("%s: %s: Error: %s", r.Title, r.Property, r.Err)
return red("%s: %s: Error: %s", r.Title, r.Property, r.Err)
}

switch r.TestType {
case resource.Value:
if r.Result {
if r.Successful {
return green("%s: %s: %s: matches expectation: %s", r.ResourceType, r.Title, r.Property, r.Expected)
} else {
return red("%s: %s: %s: doesn't match, expect: %s found: %s", r.ResourceType, r.Title, r.Property, r.Expected, r.Found)
}
case resource.Values:
if r.Result {
if r.Successful {
return green("%s: %s: %s: all expectations found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
return red("%s: %s: %s: expectations not found [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
case resource.Contains:
if r.Result {
if r.Successful {
return green("%s: %s: %s: all patterns found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
} else {
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(r.Expected, ", "))
return red("%s: %s: %s: patterns not found: [%s]", r.ResourceType, r.Title, r.Property, strings.Join(subtractSlice(r.Expected, r.Found), ", "))
}
default:
return red("Unexpected type %d", r.TestType)
Expand Down Expand Up @@ -85,3 +85,21 @@ func GetOutputer(name string) Outputer {
}
return outputers[name]
}

func subtractSlice(x, y []string) []string {
m := make(map[string]bool)

for _, y := range y {
m[y] = true
}

var ret []string
for _, x := range x {
if m[x] {
continue
}
ret = append(ret, x)
}

return ret
}
13 changes: 7 additions & 6 deletions outputs/rspecish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ func (r Rspecish) Output(results <-chan []resource.TestResult) (hasFail bool) {
var failed []resource.TestResult
for resultGroup := range results {
for _, testResult := range resultGroup {
if testResult.Result {
if testResult.Successful {
fmt.Printf(green("."))
testCount++
} else {
fmt.Printf(red("F"))
failed = append(failed, testResult)
testCount++
}
testCount++
}
}

fmt.Print("\n\n")
if len(failed) > 0 {
color.Red("\n\nFailures:")
color.Red("Failures:")
for _, testResult := range failed {
fmt.Println(humanizeResult(testResult))
}
fmt.Print("\n")
}

if len(failed) > 0 {
color.Red("\n\nCount: %d failed: %d\n", testCount, len(failed))
color.Red("Count: %d failed: %d\n", testCount, len(failed))
return true
}
color.Green("\n\nCount: %d failed: %d\n", testCount, len(failed))
color.Green("Count: %d failed: %d\n", testCount, len(failed))
return false
}

Expand Down
48 changes: 23 additions & 25 deletions resource/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
)

type TestResult struct {
Result bool
Successful bool
Title string
ResourceType string
TestType int
Expand All @@ -36,7 +36,7 @@ func ValidateValues(res IDer, property string, expectedValues []string, method f
foundValues, err := method()
if err != nil {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Values,
Title: title,
Expand All @@ -59,18 +59,18 @@ func ValidateValues(res IDer, property string, expectedValues []string, method f

if len(bad) > 0 {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Values,
Title: title,
Property: property,
Expected: bad,
Expected: expectedValues,
Found: foundValues,
Duration: time.Now().Sub(startTime),
}
}
return TestResult{
Result: true,
Successful: true,
ResourceType: typs,
TestType: Values,
Title: title,
Expand All @@ -90,7 +90,7 @@ func ValidateValue(res IDer, property string, expectedValue interface{}, method
foundValue, err := method()
if err != nil {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Value,
Title: title,
Expand All @@ -102,7 +102,7 @@ func ValidateValue(res IDer, property string, expectedValue interface{}, method

if expectedValue == foundValue {
return TestResult{
Result: true,
Successful: true,
ResourceType: typs,
TestType: Value,
Title: title,
Expand All @@ -114,7 +114,7 @@ func ValidateValue(res IDer, property string, expectedValue interface{}, method
}

return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Value,
Title: title,
Expand Down Expand Up @@ -229,7 +229,7 @@ func ValidateContains(res IDer, property string, expectedValues []string, method
fh, err := method()
if err != nil {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Contains,
Title: title,
Expand All @@ -247,7 +247,10 @@ func ValidateContains(res IDer, property string, expectedValues []string, method
i := 0
for _, pat := range notfound {
if pat.Match(line) {
found = append(found, pat)
// Found it, but wasn't supposed to, don't mark it as found, but remove it from search
if !pat.Inverse() {
found = append(found, pat)
}
continue
}
notfound[i] = pat
Expand All @@ -260,7 +263,7 @@ func ValidateContains(res IDer, property string, expectedValues []string, method
}
if err := scanner.Err(); err != nil {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Contains,
Title: title,
Expand All @@ -270,39 +273,34 @@ func ValidateContains(res IDer, property string, expectedValues []string, method
}
}

var bad []patternMatcher
for _, pat := range notfound {
// pat.Pattern() == "" is for empty io.Reader
// Didn't find it, but we didn't want to.. so we mark it as found
// Empty pattern should match even if input to scanner is empty
if pat.Inverse() || pat.Pattern() == "" {
continue
found = append(found, pat)
}
bad = append(bad, pat)
}

for _, pat := range found {
if pat.Inverse() {
bad = append(bad, pat)
}
}

if len(bad) > 0 {
if len(expectedValues) != len(found) {
return TestResult{
Result: false,
Successful: false,
ResourceType: typs,
TestType: Contains,
Title: title,
Property: property,
Expected: patternsToSlice(bad),
Expected: expectedValues,
Found: patternsToSlice(found),
Duration: time.Now().Sub(startTime),
}
}
return TestResult{
Result: true,
Successful: true,
ResourceType: typs,
TestType: Contains,
Title: title,
Property: property,
Expected: expectedValues,
Found: patternsToSlice(found),
Duration: time.Now().Sub(startTime),
}

Expand Down
24 changes: 12 additions & 12 deletions resource/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestValidateValue(t *testing.T) {
return c.in2, nil
}
got := ValidateValue(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Result, c.want)
if got.Successful != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Successful, c.want)
}
}
}
Expand All @@ -44,8 +44,8 @@ func TestValidateValueErr(t *testing.T) {
return c.in2, fmt.Errorf("some err")
}
got := ValidateValue(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != false {
t.Errorf("%+v: got %v, want %v", c, got.Result, false)
if got.Successful != false {
t.Errorf("%+v: got %v, want %v", c, got.Successful, false)
}
}
}
Expand Down Expand Up @@ -75,8 +75,8 @@ func TestValidateValues(t *testing.T) {
return c.in2, nil
}
got := ValidateValues(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Result, c.want)
if got.Successful != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Successful, c.want)
}
}
}
Expand All @@ -87,8 +87,8 @@ func TestValidateValuesErr(t *testing.T) {
return c.in2, fmt.Errorf("some err")
}
got := ValidateValues(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != false {
t.Errorf("%+v: got %v, want %v", c, got.Result, false)
if got.Successful != false {
t.Errorf("%+v: got %v, want %v", c, got.Successful, false)
}
}
}
Expand All @@ -115,8 +115,8 @@ func TestValidateContains(t *testing.T) {
return reader, nil
}
got := ValidateContains(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Result, c.want)
if got.Successful != c.want {
t.Errorf("%+v: got %v, want %v", c, got.Successful, c.want)
}
}
}
Expand All @@ -128,8 +128,8 @@ func TestValidateContainsErr(t *testing.T) {
return reader, fmt.Errorf("some err")
}
got := ValidateContains(&FakeIDer{""}, "", c.in, inFunc)
if got.Result != false {
t.Errorf("%+v: got %v, want %v", c, got.Result, false)
if got.Successful != false {
t.Errorf("%+v: got %v, want %v", c, got.Successful, false)
}
}
}

0 comments on commit bd89865

Please sign in to comment.