Skip to content

Commit

Permalink
test: update tests to new workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Phillips <[email protected]>
  • Loading branch information
spiffcs committed Dec 21, 2023
1 parent ea14035 commit e09e755
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ tasks:
sh: "go list ./... | grep -v {{ .OWNER }}/{{ .PROJECT }}/test | tr '\n' ' '"

# unit test coverage threshold (in % coverage)
COVERAGE_THRESHOLD: 50
COVERAGE_THRESHOLD: 20
cmds:
- cmd: "mkdir -p {{ .TMP_DIR }}"
silent: true
Expand Down
10 changes: 5 additions & 5 deletions grant/evalutation/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func Test_NewResults(t *testing.T) {
name string
ec EvaluationConfig
fixtures []string
wantPass bool
isFailed bool
}{
{
name: "NewResults returns results from a group of cases that cannot pass the default config",
Expand All @@ -20,15 +20,15 @@ func Test_NewResults(t *testing.T) {
"../../fixtures/multiple",
"../../fixtures/licenses/MIT",
},
wantPass: false,
isFailed: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cases := grant.NewCases(&tc.ec.Policy, tc.fixtures...)
cases := grant.NewCases(tc.ec.Policy, tc.fixtures...)
results := NewResults(tc.ec, cases...)
if tc.wantPass != results.Pass() {
t.Errorf("NewResults() = %v, want %v", results.Pass(), tc.wantPass)
if tc.isFailed != results.IsFailed() {
t.Errorf("results.IsFailed() = %v, want %v", results.IsFailed(), tc.isFailed)
}
})
}
Expand Down
22 changes: 14 additions & 8 deletions grant/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ type Policy struct {
MatchNonSPDX bool
}

var DefaultDenyAll = Rule{
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant by default will deny all licenses",
}

// DefaultPolicy returns a policy that denies all licenses
func DefaultPolicy() Policy {
return Policy{
Rules: []Rule{
{
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant by default will deny all licenses",
},
},
Rules: []Rule{DefaultDenyAll},
}
}

// NewPolicy builds a policy from lists of allow, deny, and ignore glob patterns
// It lower cases all patterns to make matching against the spdx license set case-insensitive
func NewPolicy(matchNonSPDX bool, rules ...Rule) (p Policy, err error) {
if len(rules) == 0 {
return Policy{
Rules: Rules{DefaultDenyAll},
MatchNonSPDX: matchNonSPDX,
}, nil
}
return Policy{
Rules: rules,
MatchNonSPDX: matchNonSPDX,
Expand Down
66 changes: 17 additions & 49 deletions grant/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/gobwas/glob"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func Test_DefaultPolicy(t *testing.T) {
Expand All @@ -17,16 +16,17 @@ func Test_DefaultPolicy(t *testing.T) {
{
name: "DefaultPolicy() returns the expected default policy",
want: Policy{
AllowLicenses: make([]glob.Glob, 0),
DenyLicenses: []glob.Glob{
glob.MustCompile("*"),
Rules: []Rule{
{
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant by default will deny all licenses",
},
},
IgnoreLicenses: make([]glob.Glob, 0),
denyAll: true,
},
compareOptions: []cmp.Option{
cmpopts.IgnoreFields(Policy{}, "denyAll", "allowAll"),
MatchNonSPDX: false,
},
compareOptions: []cmp.Option{},
},
}

Expand All @@ -36,65 +36,33 @@ func Test_DefaultPolicy(t *testing.T) {
if diff := cmp.Diff(tc.want, got, tc.compareOptions...); diff != "" {
t.Errorf("DefaultPolicy() mismatch (-want +got):\n%s", diff)
}
if got.denyAll != true {
t.Errorf("DefaultPolicy() denyAll = %v, want %v", got.denyAll, true)
}
})
}
}

func Test_NewPolicy(t *testing.T) {
tests := []struct {
name string
allowLicenses []string
denyLicenses []string
ignoreLicenses []string
want Policy
rules []Rule
matchNonSPDX bool
compareOptions []cmp.Option
wantErr bool
}{
{
name: "NewPolicy() returns the expected policy",
allowLicenses: []string{"MIT", "Apache-2.0"},
denyLicenses: []string{"GPL-3.0"},
ignoreLicenses: make([]string, 0),
name: "NewPolicy() returns the expected policy with no rules",
want: Policy{
AllowLicenses: []glob.Glob{
glob.MustCompile("mit"),
glob.MustCompile("apache-2.0"),
},
DenyLicenses: []glob.Glob{
glob.MustCompile("gpl-3.0"),
},
IgnoreLicenses: make([]glob.Glob, 0),
},
compareOptions: []cmp.Option{
cmpopts.IgnoreFields(Policy{}, "denyAll", "allowAll"),
},
wantErr: false,
},
{
name: "NewPolicy() returns the expected policy when allow and deny licenses are empty",
allowLicenses: []string{},
denyLicenses: []string{},
ignoreLicenses: []string{},
want: Policy{
AllowLicenses: make([]glob.Glob, 0),
DenyLicenses: []glob.Glob{
glob.MustCompile("*"),
},
IgnoreLicenses: make([]glob.Glob, 0),
},
compareOptions: []cmp.Option{
cmpopts.IgnoreFields(Policy{}, "denyAll", "allowAll"),
Rules: Rules{DefaultDenyAll},
MatchNonSPDX: false,
},
wantErr: false,
compareOptions: []cmp.Option{},
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := NewPolicy(tc.allowLicenses, tc.denyLicenses, tc.ignoreLicenses)
got, err := NewPolicy(tc.matchNonSPDX, tc.rules...)
if (err != nil) != tc.wantErr {
t.Errorf("NewPolicy() error = %v, wantErr %v", err, tc.wantErr)
return
Expand Down

0 comments on commit e09e755

Please sign in to comment.