forked from google/osv-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc48e83
commit 57c5f7f
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |