-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(guided remediation): add
--upgrade-config
flag (#1191)
closes #1177 - Adds `--upgrade-config` flag for configuring allowed upgrades on a per-package basis. - Hide & deprecate previous `--disallow-major-upgrades` and `--disallow-package-upgrades` flags. - Update docs & example script to use new flag.
- Loading branch information
1 parent
2bc1b28
commit 61979fe
Showing
19 changed files
with
545 additions
and
110 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
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,138 @@ | ||
package fix | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/internal/remediation/upgrade" | ||
"github.com/google/osv-scanner/pkg/reporter" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func parseFlags(t *testing.T, flags []string, arguments []string) (*cli.Context, error) { | ||
// This is a bit hacky: make a mock App with only the flags we care about. | ||
// Then use app.Run() to parse the flags into the cli.Context, which is returned. | ||
t.Helper() | ||
appFlags := make([]cli.Flag, 0, len(flags)) | ||
for _, f := range Command(nil, nil, nil).Flags { | ||
if slices.ContainsFunc(f.Names(), func(s string) bool { return slices.Contains(flags, s) }) { | ||
appFlags = append(appFlags, f) | ||
} | ||
} | ||
var parsedContext *cli.Context | ||
app := cli.App{ | ||
Flags: appFlags, | ||
Action: func(ctx *cli.Context) error { | ||
t.Helper() | ||
parsedContext = ctx | ||
|
||
return nil | ||
}, | ||
} | ||
err := app.Run(append([]string{""}, arguments...)) | ||
|
||
return parsedContext, err | ||
} | ||
|
||
func TestParseUpgradeConfig(t *testing.T) { | ||
t.Parallel() | ||
flags := []string{"upgrade-config", "disallow-major-upgrades", "disallow-package-upgrades"} | ||
|
||
tests := []struct { | ||
name string | ||
args []string | ||
want map[string]upgrade.Level | ||
}{ | ||
{ | ||
name: "default behaviour", | ||
args: []string{}, | ||
want: map[string]upgrade.Level{ | ||
"foo": upgrade.Major, | ||
"bar": upgrade.Major, | ||
}, | ||
}, | ||
{ | ||
name: "general level config", | ||
args: []string{"--upgrade-config=minor"}, | ||
want: map[string]upgrade.Level{ | ||
"foo": upgrade.Minor, | ||
"bar": upgrade.Minor, | ||
}, | ||
}, | ||
{ | ||
name: "all levels", | ||
args: []string{ | ||
"--upgrade-config", "major:major", | ||
"--upgrade-config", "minor:minor", | ||
"--upgrade-config", "patch:patch", | ||
"--upgrade-config", "none:none", | ||
}, | ||
want: map[string]upgrade.Level{ | ||
"major": upgrade.Major, | ||
"minor": upgrade.Minor, | ||
"patch": upgrade.Patch, | ||
"none": upgrade.None, | ||
"other": upgrade.Major, | ||
}, | ||
}, | ||
{ | ||
name: "package takes precedence over general", | ||
args: []string{ | ||
"--upgrade-config", "pkg1:minor", | ||
"--upgrade-config", "none", | ||
"--upgrade-config", "pkg2:major", | ||
}, | ||
want: map[string]upgrade.Level{ | ||
"pkg1": upgrade.Minor, | ||
"pkg2": upgrade.Major, | ||
"pkg3": upgrade.None, | ||
}, | ||
}, | ||
{ | ||
name: "package names with colons", | ||
args: []string{ | ||
"--upgrade-config=none:patch:minor:major", | ||
"--upgrade-config=none:patch:minor", | ||
"--upgrade-config=none:patch", | ||
"--upgrade-config=none", | ||
}, | ||
want: map[string]upgrade.Level{ | ||
"none:patch:minor": upgrade.Major, | ||
"none:patch": upgrade.Minor, | ||
"none": upgrade.Patch, | ||
"other": upgrade.None, | ||
}, | ||
}, | ||
{ | ||
name: "deprecated flag", | ||
args: []string{ | ||
"--disallow-major-upgrades", | ||
"--disallow-package-upgrades=pkg1,pkg2", | ||
"--upgrade-config=pkg3:patch", | ||
}, | ||
want: map[string]upgrade.Level{ | ||
"pkg1": upgrade.None, | ||
"pkg2": upgrade.None, | ||
"pkg3": upgrade.Patch, | ||
"pkg4": upgrade.Minor, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
ctx, err := parseFlags(t, flags, tt.args) | ||
if err != nil { | ||
t.Fatalf("error parsing flags: %v", err) | ||
} | ||
config := parseUpgradeConfig(ctx, &reporter.VoidReporter{}) | ||
for pkg, want := range tt.want { | ||
if got := config.Get(pkg); got != want { | ||
t.Errorf("Config.Get(%s) got = %v, want %v", pkg, got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.