github.com/google/osv-scalibr@v0.4.1/guidedremediation/upgrade/upgrade_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Copyright 2025 Google LLC 16 // 17 // Licensed under the Apache License, Version 2.0 (the "License"); 18 // you may not use this file except in compliance with the License. 19 // You may obtain a copy of the License at 20 // 21 // http://www.apache.org/licenses/LICENSE-2.0 22 // 23 // Unless required by applicable law or agreed to in writing, software 24 // distributed under the License is distributed on an "AS IS" BASIS, 25 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 26 // See the License for the specific language governing permissions and 27 // limitations under the License. 28 29 package upgrade_test 30 31 import ( 32 "slices" 33 "testing" 34 35 "deps.dev/util/semver" 36 "github.com/google/osv-scalibr/guidedremediation/upgrade" 37 ) 38 39 func TestLevelAllows(t *testing.T) { 40 // Check every combination of Level + Diff 41 allDiffs := [...]semver.Diff{ 42 semver.Same, 43 semver.DiffOther, 44 semver.DiffMajor, 45 semver.DiffMinor, 46 semver.DiffPatch, 47 semver.DiffPrerelease, 48 semver.DiffBuild, 49 } 50 51 levelDisallowed := map[upgrade.Level][]semver.Diff{ 52 upgrade.Major: {}, 53 upgrade.Minor: {semver.DiffMajor}, 54 upgrade.Patch: {semver.DiffMajor, semver.DiffMinor}, 55 upgrade.None: allDiffs[1:], // everything but semver.Same 56 } 57 58 for level, disallowed := range levelDisallowed { 59 for _, diff := range allDiffs { 60 want := !slices.Contains(disallowed, diff) 61 got := level.Allows(diff) 62 if want != got { 63 t.Errorf("(Level: %v, Diff: %v) Allows() = %v, want %v", level, diff, got, want) 64 } 65 } 66 } 67 } 68 69 func configSetExpect(t *testing.T, config upgrade.Config, pkg string, level upgrade.Level, want bool) { 70 t.Helper() 71 got := config.Set(pkg, level) 72 if got != want { 73 t.Errorf("Set(%v, %v) got %v, want %v", pkg, level, got, want) 74 } 75 } 76 77 func configSetDefaultExpect(t *testing.T, config upgrade.Config, level upgrade.Level, want bool) { 78 t.Helper() 79 got := config.SetDefault(level) 80 if got != want { 81 t.Errorf("SetDefault(%v) got %v, want %v", level, got, want) 82 } 83 } 84 85 func configGetExpect(t *testing.T, config upgrade.Config, pkg string, want upgrade.Level) { 86 t.Helper() 87 if got := config.Get(pkg); got != want { 88 t.Errorf("Get(%v) got %v, want %v", pkg, got, want) 89 } 90 } 91 92 func TestConfig(t *testing.T) { 93 config := upgrade.NewConfig() 94 95 // Default everything to allow major 96 configGetExpect(t, config, "foo", upgrade.Major) 97 configGetExpect(t, config, "bar", upgrade.Major) 98 99 // Set specific package 100 configSetExpect(t, config, "foo", upgrade.Minor, false) 101 configGetExpect(t, config, "foo", upgrade.Minor) 102 configGetExpect(t, config, "bar", upgrade.Major) 103 104 // Set package again 105 configSetExpect(t, config, "foo", upgrade.None, true) 106 configGetExpect(t, config, "foo", upgrade.None) 107 configGetExpect(t, config, "bar", upgrade.Major) 108 109 // Set default 110 configSetDefaultExpect(t, config, upgrade.Patch, false) 111 configGetExpect(t, config, "foo", upgrade.None) 112 configGetExpect(t, config, "bar", upgrade.Patch) 113 114 // Set default again 115 configSetDefaultExpect(t, config, upgrade.Major, true) 116 configGetExpect(t, config, "foo", upgrade.None) 117 configGetExpect(t, config, "bar", upgrade.Major) 118 119 // Set other package 120 configSetExpect(t, config, "bar", upgrade.Minor, false) 121 configGetExpect(t, config, "foo", upgrade.None) 122 configGetExpect(t, config, "bar", upgrade.Minor) 123 configGetExpect(t, config, "baz", upgrade.Major) 124 }