Skip to content

Commit

Permalink
Fix linter errors
Browse files Browse the repository at this point in the history
Make Test.Cases a slice of pointers to avoid copying.

Signed-off-by: Will Beason <willbeason@google.com>
  • Loading branch information
Will Beason committed Sep 23, 2021
1 parent a6e97d9 commit 98d1b6b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
16 changes: 8 additions & 8 deletions pkg/gktest/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ func TestFilter_MatchesTest(t *testing.T) {
{
name: "filter matches case",
filter: "foo",
test: Test{Name: "bar", Cases: []Case{{Name: "foo"}}},
test: Test{Name: "bar", Cases: []*Case{{Name: "foo"}}},
want: true,
},
{
name: "filter matches case submatch",
filter: "foo",
test: Test{Name: "bar", Cases: []Case{{Name: "foo-bar"}}},
test: Test{Name: "bar", Cases: []*Case{{Name: "foo-bar"}}},
want: true,
},
{
Expand All @@ -119,37 +119,37 @@ func TestFilter_MatchesTest(t *testing.T) {
{
name: "test name mismatch",
filter: "foo//",
test: Test{Name: "bar", Cases: []Case{{Name: "foo"}}},
test: Test{Name: "bar", Cases: []*Case{{Name: "foo"}}},
want: false,
},
{
name: "test and case match",
filter: "bar//qux",
test: Test{Name: "bar", Cases: []Case{{Name: "qux"}}},
test: Test{Name: "bar", Cases: []*Case{{Name: "qux"}}},
want: true,
},
{
name: "test and case submatch",
filter: "bar//qux",
test: Test{Name: "foo-bar", Cases: []Case{{Name: "qux-corge"}}},
test: Test{Name: "foo-bar", Cases: []*Case{{Name: "qux-corge"}}},
want: true,
},
{
name: "test mismatch",
filter: "bar-bar//qux",
test: Test{Name: "bar-foo", Cases: []Case{{Name: "qux-corge"}}},
test: Test{Name: "bar-foo", Cases: []*Case{{Name: "qux-corge"}}},
want: false,
},
{
name: "case mismatch",
filter: "bar//qux-qux",
test: Test{Name: "foo", Cases: []Case{{Name: "corge-qux"}}},
test: Test{Name: "foo", Cases: []*Case{{Name: "corge-qux"}}},
want: false,
},
{
name: "test match case mismatch",
filter: "bar-bar//qux-qux",
test: Test{Name: "bar", Cases: []Case{{Name: "foo"}}},
test: Test{Name: "bar", Cases: []*Case{{Name: "foo"}}},
want: false,
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gktest/read_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ tests:
Tests: []Test{{
Template: "template.yaml",
Constraint: "constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "allow.yaml",
}, {
Object: "deny.yaml",
Expand Down Expand Up @@ -318,15 +318,15 @@ tests:
Tests: []Test{{
Template: "template.yaml",
Constraint: "constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "allow.yaml",
}, {
Object: "deny.yaml",
Assertions: []Assertion{{
Violations: intStrFromStr("yes"),
}},
}, {
Object: "referential.yaml",
Object: "referential.yaml",
Inventory: []string{"inventory.yaml"},
}},
}},
Expand Down
11 changes: 5 additions & 6 deletions pkg/gktest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (r *Runner) runCases(ctx context.Context, suiteDir string, filter Filter, t
return results, nil
}

func (r *Runner) skipCase(c Case) CaseResult {
return CaseResult{Name: c.Name, Skipped: true}
func (r *Runner) skipCase(tc *Case) CaseResult {
return CaseResult{Name: tc.Name, Skipped: true}
}

func (r *Runner) makeTestClient(ctx context.Context, suiteDir string, t Test) (Client, error) {
Expand Down Expand Up @@ -179,7 +179,7 @@ func (r *Runner) addTemplate(ctx context.Context, suiteDir, templatePath string,
}

// RunCase executes a Case and returns the result of the run.
func (r *Runner) runCase(ctx context.Context, newClient func() (Client, error), suiteDir string, tc Case) CaseResult {
func (r *Runner) runCase(ctx context.Context, newClient func() (Client, error), suiteDir string, tc *Case) CaseResult {
start := time.Now()

err := r.checkCase(ctx, newClient, suiteDir, tc)
Expand All @@ -191,7 +191,7 @@ func (r *Runner) runCase(ctx context.Context, newClient func() (Client, error),
}
}

func (r *Runner) checkCase(ctx context.Context, newClient func() (Client, error), suiteDir string, tc Case) (err error) {
func (r *Runner) checkCase(ctx context.Context, newClient func() (Client, error), suiteDir string, tc *Case) (err error) {
if tc.Object == "" {
return fmt.Errorf("%w: must define object", ErrInvalidCase)
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func (r *Runner) checkCase(ctx context.Context, newClient func() (Client, error)
return nil
}

func (r *Runner) runReview(ctx context.Context, newClient func() (Client, error), suiteDir string, tc Case) (*types.Responses, error) {
func (r *Runner) runReview(ctx context.Context, newClient func() (Client, error), suiteDir string, tc *Case) (*types.Responses, error) {
c, err := newClient()
if err != nil {
return nil, err
Expand Down Expand Up @@ -283,7 +283,6 @@ func readObjects(f fs.FS, path string) ([]*unstructured.Unstructured, error) {
}

nObjs := len(objs)

if nObjs == 0 {
return nil, fmt.Errorf("%w: path %q defines no YAML objects", ErrNoObjects, path)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gktest/runner_integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestRunner_Run_Integer(t *testing.T) {
Tests: []Test{{
Template: "template.yaml",
Constraint: "constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "allow.yaml",
}, {
Object: "disallow.yaml",
Expand Down
38 changes: 19 additions & 19 deletions pkg/gktest/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
}},
}, {
Template: "deny-template.yaml",
Constraint: "deny-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
Assertions: []Assertion{{
Violations: intStrFromStr("yes"),
Expand Down Expand Up @@ -513,7 +513,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
}},
}},
Expand Down Expand Up @@ -543,8 +543,8 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Object: "object.yaml",
Cases: []*Case{{
Object: "object.yaml",
Inventory: []string{"inventory.yaml"},
}},
}},
Expand Down Expand Up @@ -577,7 +577,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
}},
}},
Expand Down Expand Up @@ -607,7 +607,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
}},
}},
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
Inventory: []string{"inventory.yaml"},
}},
Expand Down Expand Up @@ -776,7 +776,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
}},
}},
Expand All @@ -803,7 +803,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Object: "object.yaml",
Assertions: []Assertion{{
Violations: intStrFromStr("yes"),
Expand Down Expand Up @@ -833,7 +833,7 @@ func TestRunner_Run(t *testing.T) {
Tests: []Test{{
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{}},
Cases: []*Case{{}},
}},
},
f: fstest.MapFS{
Expand All @@ -858,7 +858,7 @@ func TestRunner_Run(t *testing.T) {
Name: "allow",
Template: "allow-template.yaml",
Constraint: "allow-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Name: "allowed-1",
Object: "object.yaml",
}, {
Expand All @@ -869,7 +869,7 @@ func TestRunner_Run(t *testing.T) {
Name: "deny",
Template: "deny-template.yaml",
Constraint: "deny-constraint.yaml",
Cases: []Case{{
Cases: []*Case{{
Name: "denied",
Object: "object.yaml",
Assertions: []Assertion{{
Expand Down Expand Up @@ -915,13 +915,13 @@ func TestRunner_Run(t *testing.T) {
Name: "referential constraint",
Template: "template.yaml",
Constraint: "constraint.yaml",
Cases: []Case{{
Name: "allow",
Object: "allow.yaml",
Cases: []*Case{{
Name: "allow",
Object: "allow.yaml",
Inventory: []string{"inventory.yaml"},
}, {
Name: "deny",
Object: "deny.yaml",
Name: "deny",
Object: "deny.yaml",
Inventory: []string{"inventory.yaml"},
Assertions: []Assertion{{
Violations: intStrFromStr("yes"),
Expand Down Expand Up @@ -1330,7 +1330,7 @@ func TestRunner_RunCase(t *testing.T) {
Tests: []Test{{
Template: templateFile,
Constraint: constraintFile,
Cases: []Case{{
Cases: []*Case{{
Object: objectFile,
Assertions: tc.assertions,
}},
Expand Down
2 changes: 1 addition & 1 deletion pkg/gktest/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Test struct {
Constraint string `json:"constraint"`

// Cases are the test cases to run on the instantiated Constraint.
Cases []Case `json:"cases,omitempty"`
Cases []*Case `json:"cases,omitempty"`
}

// Case runs Constraint against a YAML object.
Expand Down

0 comments on commit 98d1b6b

Please sign in to comment.