Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update code static analysis; update tests with new changes #27

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 10
cmds:
- cmd: "mkdir -p {{ .TMP_DIR }}"
silent: true
Expand Down
1 change: 0 additions & 1 deletion cmd/grant/cli/internal/check/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func (r *Report) Render() error {
return r.renderCheckTree()
case JSON:
return r.renderJSON()
return errors.New("json format not yet supported")
}
return errors.Join(r.errors...)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

[Test_postUIEventWriter_write/no_events/stdout - 1]

---

[Test_postUIEventWriter_write/no_events/stderr - 1]

---

[Test_postUIEventWriter_write/all_events/stdout - 1]


<my --
-
-
report 1!!>
<report 2>

---

[Test_postUIEventWriter_write/all_events/stderr - 1]


<my notification 1!!
...still notifying>


<notification 2>
<notification 3>

---

[Test_postUIEventWriter_write/quiet_only_shows_report/stdout - 1]
<report 1>

---

[Test_postUIEventWriter_write/quiet_only_shows_report/stderr - 1]

---
1 change: 0 additions & 1 deletion cmd/grant/cli/internal/ui/post_ui_event_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func Test_postUIEventWriter_write(t *testing.T) {

tests := []struct {
name string
quiet bool
Expand Down
2 changes: 1 addition & 1 deletion cmd/grant/cli/tui/handle_task_started.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func (m *Handler) handleTaskStarted(e partybus.Event) ([]tea.Model, tea.Cmd) {
cmd, prog, err := event.ParseTaskStarted(e)
if err != nil {
//log.Warnf("unable to parse event: %+v", err)
// log.Warnf("unable to parse event: %+v", err)
return nil, nil
}

Expand Down
3 changes: 2 additions & 1 deletion grant/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ type Case struct {
func NewCases(p Policy, userInputs ...string) []Case {
cases := make([]Case, 0)
ch, err := NewCaseHandler()
defer ch.Close()
if err != nil {
log.Errorf("unable to create case handler: %+v", err)
return cases
}
defer ch.Close()
for _, userInput := range userInputs {
c, err := ch.determineRequestCase(userInput)
if err != nil {
Expand Down Expand Up @@ -161,6 +161,7 @@ func (ch *CaseHandler) handleFile(path string) (c Case, err error) {

sb, _, _, err := format.NewDecoderCollection(format.Decoders()...).Decode(bytes)
if err != nil {
log.Debugf("unable to determine SBOM or licenses for %s: %+v", path, err)
// we want to log the error, but we don't want to return yet
}
if sb != nil {
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