-
Notifications
You must be signed in to change notification settings - Fork 824
feat: scheme-aware shell completion for scan targets #3437
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
Open
ChrisJr404
wants to merge
1
commit into
anchore:main
Choose a base branch
from
ChrisJr404:feat-completion-scheme-prefixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,158 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSchemePrefixCompletions(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| toComplete string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "empty input returns all documented scheme prefixes", | ||
| toComplete: "", | ||
| want: targetSchemePrefixes, | ||
| }, | ||
| { | ||
| name: "single letter narrows to matching prefixes", | ||
| toComplete: "o", | ||
| want: []string{"oci-archive:", "oci-dir:"}, | ||
| }, | ||
| { | ||
| name: "partial scheme returns just the matches", | ||
| toComplete: "oci-", | ||
| want: []string{"oci-archive:", "oci-dir:"}, | ||
| }, | ||
| { | ||
| name: "exact partial returns the single match", | ||
| toComplete: "sbom", | ||
| want: []string{"sbom:"}, | ||
| }, | ||
| { | ||
| name: "unrelated input returns no scheme matches", | ||
| toComplete: "xyz", | ||
| want: []string{}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := schemePrefixCompletions(tt.toComplete) | ||
| assert.Equal(t, tt.want, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTargetSchemePrefixesCoverage(t *testing.T) { | ||
| // every scheme documented in the root command long-help text should be in the completion list, | ||
| // otherwise users typing one of those prefixes won't see it offered | ||
| expected := []string{ | ||
| "docker:", | ||
| "podman:", | ||
| "docker-archive:", | ||
| "oci-archive:", | ||
| "oci-dir:", | ||
| "singularity:", | ||
| "registry:", | ||
| "dir:", | ||
| "file:", | ||
| "sbom:", | ||
| "purl:", | ||
| "cpes:", | ||
| } | ||
| for _, p := range expected { | ||
| assert.Containsf(t, targetSchemePrefixes, p, "scheme %q should be offered as a completion", p) | ||
| } | ||
| for _, p := range targetSchemePrefixes { | ||
| assert.Truef(t, strings.HasSuffix(p, ":"), "scheme prefix %q must end in ':' so users can keep typing", p) | ||
| } | ||
| } | ||
|
|
||
| func TestHasImageScheme(t *testing.T) { | ||
| tests := []struct { | ||
| input string | ||
| wantScheme string | ||
| wantOK bool | ||
| }{ | ||
| {"docker:alpine", "docker:", true}, | ||
| {"docker:", "docker:", true}, | ||
| {"podman:fedora:39", "podman:", true}, | ||
| {"registry:gcr.io/foo", "", false}, | ||
| {"dir:/tmp", "", false}, | ||
| {"alpine:3.18", "", false}, | ||
| {"", "", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.input, func(t *testing.T) { | ||
| scheme, ok := hasImageScheme(tt.input) | ||
| assert.Equal(t, tt.wantOK, ok) | ||
| assert.Equal(t, tt.wantScheme, scheme) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestHasAnyTargetScheme(t *testing.T) { | ||
| tests := []struct { | ||
| input string | ||
| want bool | ||
| }{ | ||
| {"docker:alpine", true}, | ||
| {"dir:/tmp/proj", true}, | ||
| {"sbom:./sbom.json", true}, | ||
| {"registry:gcr.io/foo", true}, | ||
| {"oci-archive:/tmp/img.tar", true}, | ||
| {"alpine:3.18", false}, | ||
| {"./local/path", false}, | ||
| {"", false}, | ||
| {"unknown-scheme:foo", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.input, func(t *testing.T) { | ||
| assert.Equal(t, tt.want, hasAnyTargetScheme(tt.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestDockerImageValidArgsFunction_OffersSchemesOnEmptyInput(t *testing.T) { | ||
| // when the user has typed nothing, the function should return every documented scheme prefix. | ||
| // We don't care whether a docker daemon is reachable in CI; we only assert the scheme prefixes | ||
| // are present in the suggestions. The directive should request NoSpace so that "dir:" can be | ||
| // followed by a path without the shell adding a separator. | ||
| cmd := &cobra.Command{Use: "grype"} | ||
| got, directive := dockerImageValidArgsFunction(cmd, nil, "") | ||
| for _, p := range targetSchemePrefixes { | ||
| assert.Containsf(t, got, p, "expected scheme prefix %q in completions", p) | ||
| } | ||
| assert.NotZero(t, directive&cobra.ShellCompDirectiveNoSpace, "expected NoSpace directive bit so user can keep typing after the colon") | ||
| } | ||
|
|
||
| func TestDockerImageValidArgsFunction_FileSchemeFallsThroughToShell(t *testing.T) { | ||
| // when a non-image scheme is in play, defer to the shell for path completion. We should return | ||
| // no suggestions and the Default directive so the shell offers filename completion. | ||
| cmd := &cobra.Command{Use: "grype"} | ||
| got, directive := dockerImageValidArgsFunction(cmd, nil, "dir:/tmp/") | ||
| assert.Empty(t, got) | ||
| assert.Equal(t, cobra.ShellCompDirectiveDefault, directive) | ||
| } | ||
|
|
||
| func TestDockerImageValidArgsFunction_PartialSchemeNarrows(t *testing.T) { | ||
| // when the user has typed a partial scheme that doesn't fully match any prefix, only matching | ||
| // prefixes should be offered. | ||
| cmd := &cobra.Command{Use: "grype"} | ||
| got, _ := dockerImageValidArgsFunction(cmd, nil, "oci-") | ||
| require.NotEmpty(t, got) | ||
| assert.Contains(t, got, "oci-archive:") | ||
| assert.Contains(t, got, "oci-dir:") | ||
| for _, c := range got { | ||
| // since we matched on "oci-", non-matching schemes should not be returned (docker-image | ||
| // fallback is also limited by the same prefix on the daemon side, so we don't expect | ||
| // arbitrary images either) | ||
| assert.Truef(t, strings.HasPrefix(c, "oci-"), "completion %q should start with the partial 'oci-' the user typed", c) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR. I think we will need to experiment with it to see if it "feels good" for real world use, but I would note that we have a
--fromflag which is also intended to replace the scheme. I'm not sure if autocompleting the scheme is ideal. We would also want to make sure the behavior is the same with--fromspecified.