Skip to content

Commit 489f2c6

Browse files
committed
Add tests for Value
If Violations are set, this is preferred. Otherwise attempt to parse value and maintain old behavior Signed-off-by: Charlie Egan <[email protected]>
1 parent d116daf commit 489f2c6

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

pkg/results/results.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,32 @@ type Result struct {
2525

2626
// IsSuccessState returns true if Value is boolean and it is true.
2727
func (r *Result) IsSuccessState() bool {
28+
if r.Violations != nil {
29+
return len(r.Violations) == 0
30+
}
31+
2832
success, ok := r.Value.(bool)
29-
return len(r.Violations) == 0 || (ok && success)
33+
if ok {
34+
log.Println("Using a boolean for `Value` is deprecated")
35+
return success
36+
} else {
37+
return false
38+
}
3039
}
3140

3241
// IsFailureState returns true if Value is boolean and it is false.
3342
func (r *Result) IsFailureState() bool {
43+
if r.Violations != nil {
44+
return len(r.Violations) != 0
45+
}
46+
3447
success, ok := r.Value.(bool)
35-
return len(r.Violations) != 0 || (ok && !success)
48+
if ok {
49+
log.Println("Using a boolean for `Value` is deprecated")
50+
return !success
51+
} else {
52+
return false
53+
}
3654
}
3755

3856
// ResultCollection is a collection of Result
@@ -120,6 +138,7 @@ func NewResultCollectionFromRegoResultSet(rs *rego.ResultSet) (*ResultCollection
120138
}
121139
rc = append(rc, &Result{
122140
ID: k,
141+
Value: violations,
123142
Violations: violations,
124143
Package: pkg,
125144
})

pkg/results/results_test.go

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ func TestIsSuccessState(t *testing.T) {
2929
}
3030
}
3131

32+
func TestIsSuccessStateWithLegacyValueResult(t *testing.T) {
33+
testCases := []struct {
34+
result *Result
35+
want bool
36+
}{
37+
{&Result{ID: "1", Value: ""}, false},
38+
{&Result{ID: "2", Value: "aaa"}, false},
39+
{&Result{ID: "3", Value: []interface{}{}}, false},
40+
{&Result{ID: "4", Value: []string{"aaa"}}, false},
41+
{&Result{ID: "5", Value: true}, true},
42+
{&Result{ID: "6", Value: false}, false},
43+
}
44+
45+
for idx, tc := range testCases {
46+
t.Run(string(idx), func(t *testing.T) {
47+
if got, want := tc.result.IsSuccessState(), tc.want; got != want {
48+
t.Fatalf("got!=want: got=%v, want=%v", got, want)
49+
}
50+
})
51+
}
52+
}
53+
3254
func TestIsFailureState(t *testing.T) {
3355
testCases := []struct {
3456
result *Result
@@ -48,6 +70,28 @@ func TestIsFailureState(t *testing.T) {
4870
}
4971
}
5072

73+
func TestIsFailureStateWithLegacyValueResult(t *testing.T) {
74+
testCases := []struct {
75+
result *Result
76+
want bool
77+
}{
78+
{&Result{ID: "1", Value: ""}, false},
79+
{&Result{ID: "2", Value: "aaa"}, false},
80+
{&Result{ID: "3", Value: []interface{}{}}, false},
81+
{&Result{ID: "4", Value: []string{"aaa"}}, false},
82+
{&Result{ID: "5", Value: true}, false},
83+
{&Result{ID: "6", Value: false}, true},
84+
}
85+
86+
for idx, tc := range testCases {
87+
t.Run(string(idx), func(t *testing.T) {
88+
if got, want := tc.result.IsFailureState(), tc.want; got != want {
89+
t.Fatalf("got!=want: got=%v, want=%v", got, want)
90+
}
91+
})
92+
}
93+
}
94+
5195
func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
5296
err1 := "ResultSet does not contain 1 exact element"
5397
err2 := "'expressions' does not contain exactly 1 element"
@@ -104,12 +148,12 @@ func TestNewResultCollectionFromRegoResultSet(t *testing.T) {
104148
}
105149

106150
expectedResults := []*Result{
107-
&Result{ID: "_1_4_3", Violations: []string{}, Package: "package.name"},
108-
&Result{ID: "_1_4_4", Violations: []string{"violation"}, Package: "package.name"},
109-
&Result{ID: "_1_4_5", Violations: []string{}, Package: "package.name"},
110-
&Result{ID: "_1_4_6", Violations: []string{}, Package: "package.name"},
111-
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Violations: []string{}, Package: "package.name"},
112-
&Result{ID: "node_pools_without_cloud_platform_scope", Violations: []string{"violation"}, Package: "package.name"},
151+
&Result{ID: "_1_4_3", Value: []string{}, Violations: []string{}, Package: "package.name"},
152+
&Result{ID: "_1_4_4", Value: []string{"violation"}, Violations: []string{"violation"}, Package: "package.name"},
153+
&Result{ID: "_1_4_5", Value: []string{}, Violations: []string{}, Package: "package.name"},
154+
&Result{ID: "_1_4_6", Value: []string{}, Violations: []string{}, Package: "package.name"},
155+
&Result{ID: "node_pools_with_legacy_endpoints_enabled", Value: []string{}, Violations: []string{}, Package: "package.name"},
156+
&Result{ID: "node_pools_without_cloud_platform_scope", Value: []string{"violation"}, Violations: []string{"violation"}, Package: "package.name"},
113157
}
114158

115159
rc, err := NewResultCollectionFromRegoResultSet(regoResultSet)

0 commit comments

Comments
 (0)