Skip to content

Commit

Permalink
Basic upgrade config & level tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkedar committed Aug 20, 2024
1 parent dc48e83 commit 57c5f7f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/remediation/upgrade/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package upgrade_test

import (
"testing"

"github.com/google/osv-scanner/internal/remediation/upgrade"
)

func configSetExpect(t *testing.T, config upgrade.Config, pkg string, level upgrade.Level, want bool) {
t.Helper()
got := config.Set(pkg, level)
if got != want {
t.Errorf("Set(%v, %v) got %v, want %v", pkg, level, got, want)
}
}

func configSetDefaultExpect(t *testing.T, config upgrade.Config, level upgrade.Level, want bool) {
t.Helper()
got := config.SetDefault(level)
if got != want {
t.Errorf("SetDefault(%v) got %v, want %v", level, got, want)
}
}

func configGetExpect(t *testing.T, config upgrade.Config, pkg string, want upgrade.Level) {
t.Helper()
if got := config.Get(pkg); got != want {
t.Errorf("Get(%v) got %v, want %v", pkg, got, want)
}
}

func TestConfig(t *testing.T) {
t.Parallel()
config := upgrade.NewConfig()

// Default everything to allow major
configGetExpect(t, config, "foo", upgrade.Major)
configGetExpect(t, config, "bar", upgrade.Major)

// Set specific package
configSetExpect(t, config, "foo", upgrade.Minor, false)
configGetExpect(t, config, "foo", upgrade.Minor)
configGetExpect(t, config, "bar", upgrade.Major)

// Set package again
configSetExpect(t, config, "foo", upgrade.None, true)
configGetExpect(t, config, "foo", upgrade.None)
configGetExpect(t, config, "bar", upgrade.Major)

// Set default
configSetDefaultExpect(t, config, upgrade.Patch, false)
configGetExpect(t, config, "foo", upgrade.None)
configGetExpect(t, config, "bar", upgrade.Patch)

// Set default again
configSetDefaultExpect(t, config, upgrade.Major, true)
configGetExpect(t, config, "foo", upgrade.None)
configGetExpect(t, config, "bar", upgrade.Major)

// Set other package
configSetExpect(t, config, "bar", upgrade.Minor, false)
configGetExpect(t, config, "foo", upgrade.None)
configGetExpect(t, config, "bar", upgrade.Minor)
configGetExpect(t, config, "baz", upgrade.Major)
}
40 changes: 40 additions & 0 deletions internal/remediation/upgrade/level_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package upgrade_test

import (
"slices"
"testing"

"deps.dev/util/semver"
"github.com/google/osv-scanner/internal/remediation/upgrade"
)

func TestLevelAllows(t *testing.T) {
t.Parallel()
// Check every combination of Level + Diff
allDiffs := [...]semver.Diff{
semver.Same,
semver.DiffOther,
semver.DiffMajor,
semver.DiffMinor,
semver.DiffPatch,
semver.DiffPrerelease,
semver.DiffBuild,
}

levelDisallowed := map[upgrade.Level][]semver.Diff{
upgrade.Major: {},
upgrade.Minor: {semver.DiffMajor},
upgrade.Patch: {semver.DiffMajor, semver.DiffMinor},
upgrade.None: allDiffs[1:], // everything but semver.Same
}

for level, disallowed := range levelDisallowed {
for _, diff := range allDiffs {
want := !slices.Contains(disallowed, diff)
got := level.Allows(diff)
if want != got {
t.Errorf("(Level: %v, Diff: %v) Allows() = %v, want %v", level, diff, got, want)
}
}
}
}

0 comments on commit 57c5f7f

Please sign in to comment.