Skip to content

Commit c5daced

Browse files
committed
Changed the result
1 parent 7bfb304 commit c5daced

File tree

2 files changed

+82
-42
lines changed

2 files changed

+82
-42
lines changed

integration/client_crudl_test.go

+25-16
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,29 @@ type MobileClientJson struct {
1717
Spec MobileClientSpec
1818
}
1919

20-
func ValidMobileClientJson(name string, clientType string) func(output []byte, err error) (bool, []string) {
21-
return func(output []byte, err error) (bool, []string) {
20+
func ValidMobileClientJson(name string, clientType string) func(output []byte, err error) ValidationResult {
21+
return func(output []byte, err error) ValidationResult {
2222
var parsed MobileClientJson
23-
errJson := json.Unmarshal([]byte(output), &parsed)
24-
if errJson != nil {
25-
return false, []string{fmt.Sprintf("%s", err)}
23+
if err := json.Unmarshal([]byte(output), &parsed); err != nil {
24+
return FailureValidation(output, err)
2625
}
2726
if parsed.Spec.ClientType != clientType {
28-
return false, []string{fmt.Sprintf("Expected the ClientType to be %s, but got %s", clientType, parsed.Spec.ClientType)}
27+
return ValidationResult{
28+
Success: false,
29+
Message: []string{fmt.Sprintf("Expected the ClientType to be %s, but got %s", clientType, parsed.Spec.ClientType)},
30+
Error: err,
31+
Output: output,
32+
}
2933
}
3034
if parsed.Spec.Name != name {
31-
return false, []string{fmt.Sprintf("Expected the Name to be %s, but got %s", name, parsed.Spec.Name)}
35+
return ValidationResult{
36+
Success: false,
37+
Message: []string{fmt.Sprintf("Expected the Name to be %s, but got %s", name, parsed.Spec.Name)},
38+
Error: err,
39+
Output: output,
40+
}
3241
}
33-
return true, []string{}
42+
return SuccessValidation(output, err)
3443
}
3544
}
3645

@@ -52,14 +61,14 @@ func TestClientJson(t *testing.T) {
5261
notExists := All(IsErr, ValidRegex(fmt.Sprintf(".*\"%s\" not found.*", expectedId)))
5362
exists := All(NoErr, ValidMobileClientJson(name, clientType))
5463

55-
m.Args("get", "client", expectedId).Should(notExists).Run(t)
56-
o.Args("get", "mobileclient", expectedId).Should(notExists).Run(t)
57-
m.Args("create", "client", name, clientType).Should(exists).Run(t)
58-
m.Args("get", "client", expectedId).Should(exists).Run(t)
59-
o.Args("get", "mobileclient", expectedId).Should(exists).Run(t)
60-
m.Args("delete", "client", expectedId).Should(NoErr).Run(t)
61-
m.Args("get", "client", expectedId).Should(notExists).Run(t)
62-
o.Args("get", "mobileclient", expectedId).Should(notExists).Run(t)
64+
m.Args("get", "client", expectedId).Should(notExists).Run().Test(t)
65+
o.Args("get", "mobileclient", expectedId).Should(notExists).Run().Test(t)
66+
m.Args("create", "client", name, clientType).Should(exists).Run().Test(t)
67+
m.Args("get", "client", expectedId).Should(exists).Run(t).Run().Test(t)
68+
o.Args("get", "mobileclient", expectedId).Should(exists).Run().Test(t)
69+
m.Args("delete", "client", expectedId).Should(NoErr).Run().Test(t)
70+
m.Args("get", "client", expectedId).Should(notExists).Run().Test(t)
71+
o.Args("get", "mobileclient", expectedId).Should(notExists).Run().Test(t)
6372
})
6473
}
6574
}

integration/validatedCommandUtils.go

+57-26
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,79 @@ import (
77
"testing"
88
)
99

10-
type ValidationFunction = func(output []byte, err error) (bool, []string)
10+
type ValidationResult struct {
11+
Error error
12+
Output []byte
13+
Message []string
14+
Success bool
15+
}
16+
17+
func (v ValidationResult) Test(t *testing.T) (output []byte, err error) {
18+
t.Log(fmt.Sprintf("%s\n", output))
19+
if !v.Success {
20+
t.Fatal(v.Message)
21+
}
22+
return v.Output, v.Error
23+
}
24+
25+
type ValidationFunction = func(output []byte, err error) ValidationResult
26+
27+
func SuccessValidation(output []byte, err error) ValidationResult {
28+
return ValidationResult{nil, output, []string{}, true}
29+
}
1130

12-
func EmptyValidation(output []byte, err error) (bool, []string) {
13-
return true, []string{}
31+
func FailureValidation(output []byte, err error) ValidationResult {
32+
return ValidationResult{err, output, []string{fmt.Sprintf("%s", err)}, false}
1433
}
1534

16-
func NoErr(output []byte, err error) (bool, []string) {
17-
return err == nil, []string{fmt.Sprintf("%s", err)}
35+
func NoErr(output []byte, err error) ValidationResult {
36+
if err == nil {
37+
return SuccessValidation(output, err)
38+
}
39+
return FailureValidation(output, err)
1840
}
1941

20-
func IsErr(output []byte, err error) (bool, []string) {
21-
return err != nil, []string{fmt.Sprintf("%s", err)}
42+
func IsErr(output []byte, err error) ValidationResult {
43+
if err != nil {
44+
return SuccessValidation(output, err)
45+
}
46+
return ValidationResult{nil, output, []string{"Expected error to occur"}, false}
2247
}
2348

24-
func ValidRegex(pattern string) func(output []byte, err error) (bool, []string) {
25-
return func(output []byte, err error) (bool, []string) {
49+
func ValidRegex(pattern string) func(output []byte, err error) ValidationResult {
50+
return func(output []byte, err error) ValidationResult {
2651
matched, errMatch := regexp.MatchString(pattern, fmt.Sprintf("%s", output))
2752
if errMatch != nil {
28-
return false, []string{fmt.Sprintf("Error in regexp %s when trying to match %s", errMatch, pattern)}
53+
return ValidationResult{
54+
Success: false,
55+
Message: []string{fmt.Sprintf("Error in regexp %s when trying to match %s", errMatch, pattern)},
56+
Error: err,
57+
Output: output,
58+
}
59+
2960
}
3061
if !matched {
31-
return false, []string{fmt.Sprintf("Expected combined output matching %s", pattern)}
62+
return ValidationResult{
63+
Success: false,
64+
Message: []string{fmt.Sprintf("Expected combined output matching %s", pattern)},
65+
Error: nil,
66+
Output: output,
67+
}
68+
3269
}
33-
return true, []string{}
70+
return SuccessValidation(output, err)
3471
}
3572
}
3673

3774
func All(vs ...ValidationFunction) ValidationFunction {
38-
return func(output []byte, err error) (bool, []string) {
75+
return func(output []byte, err error) ValidationResult {
3976
for _, v := range vs {
40-
r, o := v(output, err)
41-
if !r {
42-
return r, o
77+
r := v(output, err)
78+
if !r.Success {
79+
return r
4380
}
4481
}
45-
return true, []string{}
82+
return ValidationResult{nil, output, []string{}, true}
4683
}
4784
}
4885

@@ -60,18 +97,12 @@ func (c CmdDesc) Should(validator ValidationFunction) CmdDesc {
6097
return CmdDesc{c.executable, c.Arg, All(c.Validator, validator)}
6198
}
6299

63-
func (c CmdDesc) Run(t *testing.T) ([]byte, error) {
64-
t.Log(c.Arg)
100+
func (c CmdDesc) Run() ValidationResult {
65101
cmd := exec.Command(c.executable, c.Arg...)
66102
output, err := cmd.CombinedOutput()
67-
t.Log(fmt.Sprintf("%s\n", output))
68-
v, errs := c.Validator(output, err)
69-
if !v {
70-
t.Fatal(errs)
71-
}
72-
return output, err
103+
return c.Validator(output, err)
73104
}
74105

75106
func ValidatedCmd(executable string, arg ...string) CmdDesc {
76-
return CmdDesc{executable, arg, EmptyValidation}
107+
return CmdDesc{executable, arg, SuccessValidation}
77108
}

0 commit comments

Comments
 (0)