pull/32627/merge
cui fliter 1 day ago committed by GitHub
commit a346360ea4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -339,16 +339,25 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell
cobra.CompDebugln(fmt.Sprintf("Completions after repos: %v", completions), settings.Debug)
// Now handle completions for url prefixes
for _, url := range []string{"oci://\tChart OCI prefix", "https://\tChart URL prefix", "http://\tChart URL prefix", "file://\tChart local URL prefix"} {
if strings.HasPrefix(toComplete, url) {
urlPrefixes := []struct {
value string
desc string
}{
{value: "oci://", desc: "Chart OCI prefix"},
{value: "https://", desc: "Chart URL prefix"},
{value: "http://", desc: "Chart URL prefix"},
{value: "file://", desc: "Chart local URL prefix"},
}
for _, prefix := range urlPrefixes {
if strings.HasPrefix(toComplete, prefix.value) {
// The user already put in the full url prefix; we don't have
// anything to add, but make sure the shell does not default
// to file completion since we could be returning an empty array.
noFile = true
noSpace = true
} else if strings.HasPrefix(url, toComplete) {
} else if strings.HasPrefix(prefix.value, toComplete) {
// We are completing a url prefix
completions = append(completions, url)
completions = append(completions, fmt.Sprintf("%s\t%s", prefix.value, prefix.desc))
noSpace = true
}
}

@ -17,7 +17,11 @@ limitations under the License.
package cmd
import (
"path/filepath"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestSearchRepositoriesCmd(t *testing.T) {
@ -106,3 +110,29 @@ func TestSearchRepoOutputCompletion(t *testing.T) {
func TestSearchRepoFileCompletion(t *testing.T) {
checkFileCompletion(t, "search repo", true) // File completion may be useful when inputting a keyword
}
func TestCompListChartsURLPrefix(t *testing.T) {
originalRepositoryConfig := settings.RepositoryConfig
originalRepositoryCache := settings.RepositoryCache
settings.RepositoryConfig = filepath.Join(t.TempDir(), "repositories.yaml")
settings.RepositoryCache = t.TempDir()
t.Cleanup(func() {
settings.RepositoryConfig = originalRepositoryConfig
settings.RepositoryCache = originalRepositoryCache
})
tests := []struct {
name string
toComplete string
}{
{name: "OCI URL", toComplete: "oci://registry.example/chart"},
{name: "HTTPS URL", toComplete: "https://example.com/chart.tgz"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, directive := compListCharts(tt.toComplete, true)
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp|cobra.ShellCompDirectiveNoSpace, directive)
})
}
}

Loading…
Cancel
Save