diff --git a/Taskfile.yaml b/Taskfile.yaml index 75fd492..58c7340 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -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 diff --git a/grant/evalutation/result_test.go b/grant/evalutation/result_test.go index 0ef8d15..327db4b 100644 --- a/grant/evalutation/result_test.go +++ b/grant/evalutation/result_test.go @@ -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", @@ -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) } }) } diff --git a/grant/policy.go b/grant/policy.go index efadeea..3a371ce 100644 --- a/grant/policy.go +++ b/grant/policy.go @@ -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, diff --git a/grant/policy_test.go b/grant/policy_test.go index d19e82a..3e8c112 100644 --- a/grant/policy_test.go +++ b/grant/policy_test.go @@ -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) { @@ -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{}, }, } @@ -36,9 +36,6 @@ 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) - } }) } } @@ -46,55 +43,26 @@ func Test_DefaultPolicy(t *testing.T) { 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