Skip to content
Myr

completions_test.go

This file

Path

completions_test.go

4,129 lines · 116.3 KiB · Go

Last changed by

fix: prevent completions from mutating os.Args via append side effect (#2356)Varun Chawlabefore adoption746ef07

History of this file

Take it

1// Copyright 2013-2023 The Cobra Authors2//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 at6//7//      http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.1415package cobra1617import (18	"bytes"19	"context"20	"fmt"21	"os"22	"strings"23	"sync"24	"testing"25)2627func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {28	if len(args) != 0 {29		return nil, ShellCompDirectiveNoFileComp30	}3132	var completions []string33	for _, comp := range []string{"one\tThe first", "two\tThe second"} {34		if strings.HasPrefix(comp, toComplete) {35			completions = append(completions, comp)36		}37	}38	return completions, ShellCompDirectiveDefault39}4041func validArgsFunc2(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {42	if len(args) != 0 {43		return nil, ShellCompDirectiveNoFileComp44	}4546	var completions []string47	for _, comp := range []string{"three\tThe third", "four\tThe fourth"} {48		if strings.HasPrefix(comp, toComplete) {49			completions = append(completions, comp)50		}51	}52	return completions, ShellCompDirectiveDefault53}5455func TestCmdNameCompletionInGo(t *testing.T) {56	rootCmd := &Command{57		Use: "root",58		Run: emptyRun,59	}60	childCmd1 := &Command{61		Use:   "firstChild",62		Short: "First command",63		Run:   emptyRun,64	}65	childCmd2 := &Command{66		Use: "secondChild",67		Run: emptyRun,68	}69	hiddenCmd := &Command{70		Use:    "testHidden",71		Hidden: true, // Not completed72		Run:    emptyRun,73	}74	deprecatedCmd := &Command{75		Use:        "testDeprecated",76		Deprecated: "deprecated", // Not completed77		Run:        emptyRun,78	}79	aliasedCmd := &Command{80		Use:     "aliased",81		Short:   "A command with aliases",82		Aliases: []string{"testAlias", "testSynonym"}, // Not completed83		Run:     emptyRun,84	}8586	rootCmd.AddCommand(childCmd1, childCmd2, hiddenCmd, deprecatedCmd, aliasedCmd)8788	// Test that sub-command names are completed89	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")90	if err != nil {91		t.Errorf("Unexpected error: %v", err)92	}9394	expected := strings.Join([]string{95		"aliased",96		"completion",97		"firstChild",98		"help",99		"secondChild",100		":4",101		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")102103	if output != expected {104		t.Errorf("expected: %q, got: %q", expected, output)105	}106107	// Test that sub-command names are completed with prefix108	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "s")109	if err != nil {110		t.Errorf("Unexpected error: %v", err)111	}112113	expected = strings.Join([]string{114		"secondChild",115		":4",116		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")117118	if output != expected {119		t.Errorf("expected: %q, got: %q", expected, output)120	}121122	// Test that even with no valid sub-command matches, hidden, deprecated and123	// aliases are not completed124	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "test")125	if err != nil {126		t.Errorf("Unexpected error: %v", err)127	}128129	expected = strings.Join([]string{130		":4",131		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")132133	if output != expected {134		t.Errorf("expected: %q, got: %q", expected, output)135	}136137	// Test that sub-command names are completed with description138	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "")139	if err != nil {140		t.Errorf("Unexpected error: %v", err)141	}142143	expected = strings.Join([]string{144		"aliased\tA command with aliases",145		"completion\tGenerate the autocompletion script for the specified shell",146		"firstChild\tFirst command",147		"help\tHelp about any command",148		"secondChild",149		":4",150		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")151152	if output != expected {153		t.Errorf("expected: %q, got: %q", expected, output)154	}155}156157func TestNoCmdNameCompletionInGo(t *testing.T) {158	rootCmd := &Command{159		Use: "root",160		Run: emptyRun,161	}162	rootCmd.Flags().String("localroot", "", "local root flag")163164	childCmd1 := &Command{165		Use:   "childCmd1",166		Short: "First command",167		Args:  MinimumNArgs(0),168		Run:   emptyRun,169	}170	rootCmd.AddCommand(childCmd1)171	childCmd1.PersistentFlags().StringP("persistent", "p", "", "persistent flag")172	persistentFlag := childCmd1.PersistentFlags().Lookup("persistent")173	childCmd1.Flags().StringP("nonPersistent", "n", "", "non-persistent flag")174	nonPersistentFlag := childCmd1.Flags().Lookup("nonPersistent")175176	childCmd2 := &Command{177		Use: "childCmd2",178		Run: emptyRun,179	}180	childCmd1.AddCommand(childCmd2)181182	// Test that sub-command names are not completed if there is an argument already183	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "arg1", "")184	if err != nil {185		t.Errorf("Unexpected error: %v", err)186	}187188	expected := strings.Join([]string{189		":0",190		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")191192	if output != expected {193		t.Errorf("expected: %q, got: %q", expected, output)194	}195196	// Test that sub-command names are not completed if a local non-persistent flag is present197	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--nonPersistent", "value", "")198	if err != nil {199		t.Errorf("Unexpected error: %v", err)200	}201	// Reset the flag for the next command202	nonPersistentFlag.Changed = false203204	expected = strings.Join([]string{205		":0",206		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")207208	if output != expected {209		t.Errorf("expected: %q, got: %q", expected, output)210	}211212	// Test that sub-command names are completed if a local non-persistent flag is present and TraverseChildren is set to true213	// set TraverseChildren to true on the root cmd214	rootCmd.TraverseChildren = true215216	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "")217	if err != nil {218		t.Errorf("Unexpected error: %v", err)219	}220	// Reset TraverseChildren for next command221	rootCmd.TraverseChildren = false222223	expected = strings.Join([]string{224		"childCmd1",225		"completion",226		"help",227		":4",228		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")229230	if output != expected {231		t.Errorf("expected: %q, got: %q", expected, output)232	}233234	// Test that sub-command names from a child cmd are completed if a local non-persistent flag is present235	// and TraverseChildren is set to true on the root cmd236	rootCmd.TraverseChildren = true237238	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "--nonPersistent", "value", "")239	if err != nil {240		t.Errorf("Unexpected error: %v", err)241	}242	// Reset TraverseChildren for next command243	rootCmd.TraverseChildren = false244	// Reset the flag for the next command245	nonPersistentFlag.Changed = false246247	expected = strings.Join([]string{248		"childCmd2",249		":4",250		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")251252	if output != expected {253		t.Errorf("expected: %q, got: %q", expected, output)254	}255256	// Test that we don't use Traverse when we shouldn't.257	// This command should not return a completion since the command line is invalid without TraverseChildren.258	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--localroot", "value", "childCmd1", "")259	if err != nil {260		t.Errorf("Unexpected error: %v", err)261	}262263	expected = strings.Join([]string{264		":0",265		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")266267	if output != expected {268		t.Errorf("expected: %q, got: %q", expected, output)269	}270271	// Test that sub-command names are not completed if a local non-persistent short flag is present272	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-n", "value", "")273	if err != nil {274		t.Errorf("Unexpected error: %v", err)275	}276	// Reset the flag for the next command277	nonPersistentFlag.Changed = false278279	expected = strings.Join([]string{280		":0",281		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")282283	if output != expected {284		t.Errorf("expected: %q, got: %q", expected, output)285	}286287	// Test that sub-command names are completed with a persistent flag288	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "--persistent", "value", "")289	if err != nil {290		t.Errorf("Unexpected error: %v", err)291	}292	// Reset the flag for the next command293	persistentFlag.Changed = false294295	expected = strings.Join([]string{296		"childCmd2",297		":4",298		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")299300	if output != expected {301		t.Errorf("expected: %q, got: %q", expected, output)302	}303304	// Test that sub-command names are completed with a persistent short flag305	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd1", "-p", "value", "")306	if err != nil {307		t.Errorf("Unexpected error: %v", err)308	}309	// Reset the flag for the next command310	persistentFlag.Changed = false311312	expected = strings.Join([]string{313		"childCmd2",314		":4",315		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")316317	if output != expected {318		t.Errorf("expected: %q, got: %q", expected, output)319	}320}321322func TestValidArgsCompletionInGo(t *testing.T) {323	rootCmd := &Command{324		Use:       "root",325		ValidArgs: []string{"one", "two", "three"},326		Args:      MinimumNArgs(1),327	}328329	// Test that validArgs are completed330	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")331	if err != nil {332		t.Errorf("Unexpected error: %v", err)333	}334335	expected := strings.Join([]string{336		"one",337		"two",338		"three",339		":4",340		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")341342	if output != expected {343		t.Errorf("expected: %q, got: %q", expected, output)344	}345346	// Test that validArgs are completed with prefix347	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "o")348	if err != nil {349		t.Errorf("Unexpected error: %v", err)350	}351352	expected = strings.Join([]string{353		"one",354		":4",355		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")356357	if output != expected {358		t.Errorf("expected: %q, got: %q", expected, output)359	}360361	// Test that validArgs don't repeat362	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "one", "")363	if err != nil {364		t.Errorf("Unexpected error: %v", err)365	}366367	expected = strings.Join([]string{368		":0",369		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")370371	if output != expected {372		t.Errorf("expected: %q, got: %q", expected, output)373	}374}375376func TestValidArgsAndCmdCompletionInGo(t *testing.T) {377	rootCmd := &Command{378		Use:       "root",379		ValidArgs: []string{"one", "two"},380		Run:       emptyRun,381	}382383	childCmd := &Command{384		Use: "thechild",385		Run: emptyRun,386	}387388	rootCmd.AddCommand(childCmd)389390	// Test that both sub-commands and validArgs are completed391	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")392	if err != nil {393		t.Errorf("Unexpected error: %v", err)394	}395396	expected := strings.Join([]string{397		"completion",398		"help",399		"thechild",400		"one",401		"two",402		":4",403		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")404405	if output != expected {406		t.Errorf("expected: %q, got: %q", expected, output)407	}408409	// Test that both sub-commands and validArgs are completed with prefix410	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")411	if err != nil {412		t.Errorf("Unexpected error: %v", err)413	}414415	expected = strings.Join([]string{416		"thechild",417		"two",418		":4",419		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")420421	if output != expected {422		t.Errorf("expected: %q, got: %q", expected, output)423	}424}425426func TestValidArgsFuncAndCmdCompletionInGo(t *testing.T) {427	rootCmd := &Command{428		Use:               "root",429		ValidArgsFunction: validArgsFunc,430		Run:               emptyRun,431	}432433	childCmd := &Command{434		Use:   "thechild",435		Short: "The child command",436		Run:   emptyRun,437	}438439	rootCmd.AddCommand(childCmd)440441	// Test that both sub-commands and validArgsFunction are completed442	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")443	if err != nil {444		t.Errorf("Unexpected error: %v", err)445	}446447	expected := strings.Join([]string{448		"completion",449		"help",450		"thechild",451		"one",452		"two",453		":0",454		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")455456	if output != expected {457		t.Errorf("expected: %q, got: %q", expected, output)458	}459460	// Test that both sub-commands and validArgs are completed with prefix461	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")462	if err != nil {463		t.Errorf("Unexpected error: %v", err)464	}465466	expected = strings.Join([]string{467		"thechild",468		"two",469		":0",470		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")471472	if output != expected {473		t.Errorf("expected: %q, got: %q", expected, output)474	}475476	// Test that both sub-commands and validArgs are completed with description477	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "t")478	if err != nil {479		t.Errorf("Unexpected error: %v", err)480	}481482	expected = strings.Join([]string{483		"thechild\tThe child command",484		"two\tThe second",485		":0",486		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")487488	if output != expected {489		t.Errorf("expected: %q, got: %q", expected, output)490	}491}492493func TestFlagNameCompletionInGo(t *testing.T) {494	rootCmd := &Command{495		Use: "root",496		Run: emptyRun,497	}498	childCmd := &Command{499		Use:     "childCmd",500		Version: "1.2.3",501		Run:     emptyRun,502	}503	rootCmd.AddCommand(childCmd)504505	rootCmd.Flags().IntP("first", "f", -1, "first flag")506	rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")507	childCmd.Flags().String("subFlag", "", "sub flag")508509	// Test that flag names are not shown if the user has not given the '-' prefix510	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")511	if err != nil {512		t.Errorf("Unexpected error: %v", err)513	}514515	expected := strings.Join([]string{516		"childCmd",517		"completion",518		"help",519		":4",520		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")521522	if output != expected {523		t.Errorf("expected: %q, got: %q", expected, output)524	}525526	// Test that flag names are completed527	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")528	if err != nil {529		t.Errorf("Unexpected error: %v", err)530	}531532	expected = strings.Join([]string{533		"--first",534		"-f",535		"--help",536		"-h",537		"--second",538		"-s",539		":4",540		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")541542	if output != expected {543		t.Errorf("expected: %q, got: %q", expected, output)544	}545546	// Test that flag names are completed when a prefix is given547	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--f")548	if err != nil {549		t.Errorf("Unexpected error: %v", err)550	}551552	expected = strings.Join([]string{553		"--first",554		":4",555		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")556557	if output != expected {558		t.Errorf("expected: %q, got: %q", expected, output)559	}560561	// Test that flag names are completed in a sub-cmd562	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-")563	if err != nil {564		t.Errorf("Unexpected error: %v", err)565	}566567	expected = strings.Join([]string{568		"--second",569		"-s",570		"--help",571		"-h",572		"--subFlag",573		"--version",574		"-v",575		":4",576		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")577578	if output != expected {579		t.Errorf("expected: %q, got: %q", expected, output)580	}581}582583func TestFlagNameCompletionInGoWithDesc(t *testing.T) {584	rootCmd := &Command{585		Use: "root",586		Run: emptyRun,587	}588	childCmd := &Command{589		Use:     "childCmd",590		Short:   "first command",591		Version: "1.2.3",592		Run:     emptyRun,593	}594	rootCmd.AddCommand(childCmd)595596	rootCmd.Flags().IntP("first", "f", -1, "first flag\nlonger description for flag")597	rootCmd.PersistentFlags().BoolP("second", "s", false, "second flag")598	childCmd.Flags().String("subFlag", "", "sub flag")599600	// Test that flag names are not shown if the user has not given the '-' prefix601	output, err := executeCommand(rootCmd, ShellCompRequestCmd, "")602	if err != nil {603		t.Errorf("Unexpected error: %v", err)604	}605606	expected := strings.Join([]string{607		"childCmd\tfirst command",608		"completion\tGenerate the autocompletion script for the specified shell",609		"help\tHelp about any command",610		":4",611		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")612613	if output != expected {614		t.Errorf("expected: %q, got: %q", expected, output)615	}616617	// Test that flag names are completed618	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "-")619	if err != nil {620		t.Errorf("Unexpected error: %v", err)621	}622623	expected = strings.Join([]string{624		"--first\tfirst flag",625		"-f\tfirst flag",626		"--help\thelp for root",627		"-h\thelp for root",628		"--second\tsecond flag",629		"-s\tsecond flag",630		":4",631		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")632633	if output != expected {634		t.Errorf("expected: %q, got: %q", expected, output)635	}636637	// Test that flag names are completed when a prefix is given638	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--f")639	if err != nil {640		t.Errorf("Unexpected error: %v", err)641	}642643	expected = strings.Join([]string{644		"--first\tfirst flag",645		":4",646		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")647648	if output != expected {649		t.Errorf("expected: %q, got: %q", expected, output)650	}651652	// Test that flag names are completed in a sub-cmd653	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "childCmd", "-")654	if err != nil {655		t.Errorf("Unexpected error: %v", err)656	}657658	expected = strings.Join([]string{659		"--second\tsecond flag",660		"-s\tsecond flag",661		"--help\thelp for childCmd",662		"-h\thelp for childCmd",663		"--subFlag\tsub flag",664		"--version\tversion for childCmd",665		"-v\tversion for childCmd",666		":4",667		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")668669	if output != expected {670		t.Errorf("expected: %q, got: %q", expected, output)671	}672}673674// customMultiString is a custom Value type that accepts multiple values,675// but does not include "Slice" or "Array" in its "Type" string.676type customMultiString []string677678var _ SliceValue = (*customMultiString)(nil)679680func (s *customMultiString) String() string {681	return fmt.Sprintf("%v", *s)682}683684func (s *customMultiString) Set(v string) error {685	*s = append(*s, v)686	return nil687}688689func (s *customMultiString) Type() string {690	return "multi string"691}692693func (s *customMultiString) GetSlice() []string {694	return *s695}696697func TestFlagNameCompletionRepeat(t *testing.T) {698	rootCmd := &Command{699		Use: "root",700		Run: emptyRun,701	}702	childCmd := &Command{703		Use:   "childCmd",704		Short: "first command",705		Run:   emptyRun,706	}707	rootCmd.AddCommand(childCmd)708709	rootCmd.Flags().IntP("first", "f", -1, "first flag")710	firstFlag := rootCmd.Flags().Lookup("first")711	rootCmd.Flags().BoolP("second", "s", false, "second flag")712	secondFlag := rootCmd.Flags().Lookup("second")713	rootCmd.Flags().StringArrayP("array", "a", nil, "array flag")714	arrayFlag := rootCmd.Flags().Lookup("array")715	rootCmd.Flags().IntSliceP("slice", "l", nil, "slice flag")716	sliceFlag := rootCmd.Flags().Lookup("slice")717	rootCmd.Flags().BoolSliceP("bslice", "b", nil, "bool slice flag")718	bsliceFlag := rootCmd.Flags().Lookup("bslice")719	rootCmd.Flags().VarP(&customMultiString{}, "multi", "m", "multi string flag")720	multiFlag := rootCmd.Flags().Lookup("multi")721722	// Test that flag names are not repeated unless they are an array or slice723	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--")724	if err != nil {725		t.Errorf("Unexpected error: %v", err)726	}727	// Reset the flag for the next command728	firstFlag.Changed = false729730	expected := strings.Join([]string{731		"--array",732		"--bslice",733		"--help",734		"--multi",735		"--second",736		"--slice",737		":4",738		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")739740	if output != expected {741		t.Errorf("expected: %q, got: %q", expected, output)742	}743744	// Test that flag names are not repeated unless they are an array or slice745	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--first", "1", "--second=false", "--")746	if err != nil {747		t.Errorf("Unexpected error: %v", err)748	}749	// Reset the flag for the next command750	firstFlag.Changed = false751	secondFlag.Changed = false752753	expected = strings.Join([]string{754		"--array",755		"--bslice",756		"--help",757		"--multi",758		"--slice",759		":4",760		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")761762	if output != expected {763		t.Errorf("expected: %q, got: %q", expected, output)764	}765766	// Test that flag names are not repeated unless they are an array or slice767	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--slice", "1", "--slice=2", "--array", "val", "--bslice", "true", "--multi", "val", "--")768	if err != nil {769		t.Errorf("Unexpected error: %v", err)770	}771	// Reset the flag for the next command772	sliceFlag.Changed = false773	arrayFlag.Changed = false774	bsliceFlag.Changed = false775	multiFlag.Changed = false776777	expected = strings.Join([]string{778		"--array",779		"--bslice",780		"--first",781		"--help",782		"--multi",783		"--second",784		"--slice",785		":4",786		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")787788	if output != expected {789		t.Errorf("expected: %q, got: %q", expected, output)790	}791792	// Test that flag names are not repeated unless they are an array or slice, using shortname793	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a", "val", "-")794	if err != nil {795		t.Errorf("Unexpected error: %v", err)796	}797	// Reset the flag for the next command798	sliceFlag.Changed = false799	arrayFlag.Changed = false800	multiFlag.Changed = false801802	expected = strings.Join([]string{803		"--array",804		"-a",805		"--bslice",806		"-b",807		"--first",808		"-f",809		"--help",810		"-h",811		"--multi",812		"-m",813		"--second",814		"-s",815		"--slice",816		"-l",817		":4",818		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")819820	if output != expected {821		t.Errorf("expected: %q, got: %q", expected, output)822	}823824	// Test that flag names are not repeated unless they are an array or slice, using shortname with prefix825	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-l", "1", "-l=2", "-a", "val", "-a")826	if err != nil {827		t.Errorf("Unexpected error: %v", err)828	}829	// Reset the flag for the next command830	sliceFlag.Changed = false831	arrayFlag.Changed = false832	multiFlag.Changed = false833834	expected = strings.Join([]string{835		"-a",836		":4",837		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")838839	if output != expected {840		t.Errorf("expected: %q, got: %q", expected, output)841	}842}843844func TestRequiredFlagNameCompletionInGo(t *testing.T) {845	rootCmd := &Command{846		Use:       "root",847		ValidArgs: []string{"realArg"},848		Run:       emptyRun,849	}850	childCmd := &Command{851		Use: "childCmd",852		ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {853			return []string{"subArg"}, ShellCompDirectiveNoFileComp854		},855		Run: emptyRun,856	}857	rootCmd.AddCommand(childCmd)858859	rootCmd.Flags().IntP("requiredFlag", "r", -1, "required flag")860	assertNoErr(t, rootCmd.MarkFlagRequired("requiredFlag"))861	requiredFlag := rootCmd.Flags().Lookup("requiredFlag")862863	rootCmd.PersistentFlags().IntP("requiredPersistent", "p", -1, "required persistent")864	assertNoErr(t, rootCmd.MarkPersistentFlagRequired("requiredPersistent"))865	requiredPersistent := rootCmd.PersistentFlags().Lookup("requiredPersistent")866867	rootCmd.Flags().StringP("release", "R", "", "Release name")868869	childCmd.Flags().BoolP("subRequired", "s", false, "sub required flag")870	assertNoErr(t, childCmd.MarkFlagRequired("subRequired"))871	childCmd.Flags().BoolP("subNotRequired", "n", false, "sub not required flag")872873	// Test that a required flag is suggested even without the - prefix874	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")875	if err != nil {876		t.Errorf("Unexpected error: %v", err)877	}878879	expected := strings.Join([]string{880		"childCmd",881		"completion",882		"help",883		"--requiredFlag",884		"-r",885		"--requiredPersistent",886		"-p",887		"realArg",888		":4",889		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")890891	if output != expected {892		t.Errorf("expected: %q, got: %q", expected, output)893	}894895	// Test that a required flag is suggested without other flags when using the '-' prefix896	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-")897	if err != nil {898		t.Errorf("Unexpected error: %v", err)899	}900901	expected = strings.Join([]string{902		"--requiredFlag",903		"-r",904		"--requiredPersistent",905		"-p",906		":4",907		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")908909	if output != expected {910		t.Errorf("expected: %q, got: %q", expected, output)911	}912913	// Test that if no required flag matches, the normal flags are suggested914	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--relea")915	if err != nil {916		t.Errorf("Unexpected error: %v", err)917	}918919	expected = strings.Join([]string{920		"--release",921		":4",922		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")923924	if output != expected {925		t.Errorf("expected: %q, got: %q", expected, output)926	}927928	// Test required flags for sub-commands929	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "")930	if err != nil {931		t.Errorf("Unexpected error: %v", err)932	}933934	expected = strings.Join([]string{935		"--requiredPersistent",936		"-p",937		"--subRequired",938		"-s",939		"subArg",940		":4",941		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")942943	if output != expected {944		t.Errorf("expected: %q, got: %q", expected, output)945	}946947	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "-")948	if err != nil {949		t.Errorf("Unexpected error: %v", err)950	}951952	expected = strings.Join([]string{953		"--requiredPersistent",954		"-p",955		"--subRequired",956		"-s",957		":4",958		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")959960	if output != expected {961		t.Errorf("expected: %q, got: %q", expected, output)962	}963964	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "childCmd", "--subNot")965	if err != nil {966		t.Errorf("Unexpected error: %v", err)967	}968969	expected = strings.Join([]string{970		"--subNotRequired",971		":4",972		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")973974	if output != expected {975		t.Errorf("expected: %q, got: %q", expected, output)976	}977978	// Test that when a required flag is present, it is not suggested anymore979	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "")980	if err != nil {981		t.Errorf("Unexpected error: %v", err)982	}983	// Reset the flag for the next command984	requiredFlag.Changed = false985986	expected = strings.Join([]string{987		"--requiredPersistent",988		"-p",989		"realArg",990		":4",991		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")992993	if output != expected {994		t.Errorf("expected: %q, got: %q", expected, output)995	}996997	// Test that when a persistent required flag is present, it is not suggested anymore998	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredPersistent", "1", "")999	if err != nil {1000		t.Errorf("Unexpected error: %v", err)1001	}1002	// Reset the flag for the next command1003	requiredPersistent.Changed = false10041005	expected = strings.Join([]string{1006		"childCmd",1007		"completion",1008		"help",1009		"--requiredFlag",1010		"-r",1011		"realArg",1012		":4",1013		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")10141015	if output != expected {1016		t.Errorf("expected: %q, got: %q", expected, output)1017	}10181019	// Test that when all required flags are present, normal completion is done1020	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--requiredFlag", "1", "--requiredPersistent", "1", "")1021	if err != nil {1022		t.Errorf("Unexpected error: %v", err)1023	}1024	// Reset the flags for the next command1025	requiredFlag.Changed = false1026	requiredPersistent.Changed = false10271028	expected = strings.Join([]string{1029		"realArg",1030		":4",1031		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")10321033	if output != expected {1034		t.Errorf("expected: %q, got: %q", expected, output)1035	}1036}10371038func TestFlagFileExtFilterCompletionInGo(t *testing.T) {1039	rootCmd := &Command{1040		Use: "root",1041		Run: emptyRun,1042	}10431044	// No extensions.  Should be ignored.1045	rootCmd.Flags().StringP("file", "f", "", "file flag")1046	assertNoErr(t, rootCmd.MarkFlagFilename("file"))10471048	// Single extension1049	rootCmd.Flags().StringP("log", "l", "", "log flag")1050	assertNoErr(t, rootCmd.MarkFlagFilename("log", "log"))10511052	// Multiple extensions1053	rootCmd.Flags().StringP("yaml", "y", "", "yaml flag")1054	assertNoErr(t, rootCmd.MarkFlagFilename("yaml", "yaml", "yml"))10551056	// Directly using annotation1057	rootCmd.Flags().StringP("text", "t", "", "text flag")1058	assertNoErr(t, rootCmd.Flags().SetAnnotation("text", BashCompFilenameExt, []string{"txt"}))10591060	// Test that the completion logic returns the proper info for the completion1061	// script to handle the file filtering1062	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--file", "")1063	if err != nil {1064		t.Errorf("Unexpected error: %v", err)1065	}10661067	expected := strings.Join([]string{1068		":0",1069		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")10701071	if output != expected {1072		t.Errorf("expected: %q, got: %q", expected, output)1073	}10741075	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--log", "")1076	if err != nil {1077		t.Errorf("Unexpected error: %v", err)1078	}10791080	expected = strings.Join([]string{1081		"log",1082		":8",1083		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")10841085	if output != expected {1086		t.Errorf("expected: %q, got: %q", expected, output)1087	}10881089	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml", "")1090	if err != nil {1091		t.Errorf("Unexpected error: %v", err)1092	}10931094	expected = strings.Join([]string{1095		"yaml", "yml",1096		":8",1097		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")10981099	if output != expected {1100		t.Errorf("expected: %q, got: %q", expected, output)1101	}11021103	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--yaml=")1104	if err != nil {1105		t.Errorf("Unexpected error: %v", err)1106	}11071108	expected = strings.Join([]string{1109		"yaml", "yml",1110		":8",1111		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")11121113	if output != expected {1114		t.Errorf("expected: %q, got: %q", expected, output)1115	}11161117	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y", "")1118	if err != nil {1119		t.Errorf("Unexpected error: %v", err)1120	}11211122	expected = strings.Join([]string{1123		"yaml", "yml",1124		":8",1125		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")11261127	if output != expected {1128		t.Errorf("expected: %q, got: %q", expected, output)1129	}11301131	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-y=")1132	if err != nil {1133		t.Errorf("Unexpected error: %v", err)1134	}11351136	expected = strings.Join([]string{1137		"yaml", "yml",1138		":8",1139		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")11401141	if output != expected {1142		t.Errorf("expected: %q, got: %q", expected, output)1143	}11441145	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--text", "")1146	if err != nil {1147		t.Errorf("Unexpected error: %v", err)1148	}11491150	expected = strings.Join([]string{1151		"txt",1152		":8",1153		"Completion ended with directive: ShellCompDirectiveFilterFileExt", ""}, "\n")11541155	if output != expected {1156		t.Errorf("expected: %q, got: %q", expected, output)1157	}1158}11591160func TestFlagDirFilterCompletionInGo(t *testing.T) {1161	rootCmd := &Command{1162		Use: "root",1163		Run: emptyRun,1164	}11651166	// Filter directories1167	rootCmd.Flags().StringP("dir", "d", "", "dir flag")1168	assertNoErr(t, rootCmd.MarkFlagDirname("dir"))11691170	// Filter directories within a directory1171	rootCmd.Flags().StringP("subdir", "s", "", "subdir")1172	assertNoErr(t, rootCmd.Flags().SetAnnotation("subdir", BashCompSubdirsInDir, []string{"themes"}))11731174	// Multiple directory specification get ignored1175	rootCmd.Flags().StringP("manydir", "m", "", "manydir")1176	assertNoErr(t, rootCmd.Flags().SetAnnotation("manydir", BashCompSubdirsInDir, []string{"themes", "colors"}))11771178	// Test that the completion logic returns the proper info for the completion1179	// script to handle the directory filtering1180	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--dir", "")1181	if err != nil {1182		t.Errorf("Unexpected error: %v", err)1183	}11841185	expected := strings.Join([]string{1186		":16",1187		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")11881189	if output != expected {1190		t.Errorf("expected: %q, got: %q", expected, output)1191	}11921193	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-d", "")1194	if err != nil {1195		t.Errorf("Unexpected error: %v", err)1196	}11971198	expected = strings.Join([]string{1199		":16",1200		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12011202	if output != expected {1203		t.Errorf("expected: %q, got: %q", expected, output)1204	}12051206	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir", "")1207	if err != nil {1208		t.Errorf("Unexpected error: %v", err)1209	}12101211	expected = strings.Join([]string{1212		"themes",1213		":16",1214		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12151216	if output != expected {1217		t.Errorf("expected: %q, got: %q", expected, output)1218	}12191220	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--subdir=")1221	if err != nil {1222		t.Errorf("Unexpected error: %v", err)1223	}12241225	expected = strings.Join([]string{1226		"themes",1227		":16",1228		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12291230	if output != expected {1231		t.Errorf("expected: %q, got: %q", expected, output)1232	}12331234	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s", "")1235	if err != nil {1236		t.Errorf("Unexpected error: %v", err)1237	}12381239	expected = strings.Join([]string{1240		"themes",1241		":16",1242		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12431244	if output != expected {1245		t.Errorf("expected: %q, got: %q", expected, output)1246	}12471248	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "-s=")1249	if err != nil {1250		t.Errorf("Unexpected error: %v", err)1251	}12521253	expected = strings.Join([]string{1254		"themes",1255		":16",1256		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12571258	if output != expected {1259		t.Errorf("expected: %q, got: %q", expected, output)1260	}12611262	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--manydir", "")1263	if err != nil {1264		t.Errorf("Unexpected error: %v", err)1265	}12661267	expected = strings.Join([]string{1268		":16",1269		"Completion ended with directive: ShellCompDirectiveFilterDirs", ""}, "\n")12701271	if output != expected {1272		t.Errorf("expected: %q, got: %q", expected, output)1273	}1274}12751276func TestValidArgsFuncCmdContext(t *testing.T) {1277	validArgsFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1278		ctx := cmd.Context()12791280		if ctx == nil {1281			t.Error("Received nil context in completion func")1282		} else if ctx.Value("testKey") != "123" {1283			t.Error("Received invalid context")1284		}12851286		return nil, ShellCompDirectiveDefault1287	}12881289	rootCmd := &Command{1290		Use: "root",1291		Run: emptyRun,1292	}1293	childCmd := &Command{1294		Use:               "childCmd",1295		ValidArgsFunction: validArgsFunc,1296		Run:               emptyRun,1297	}1298	rootCmd.AddCommand(childCmd)12991300	//nolint:staticcheck // We can safely use a basic type as key in tests.1301	ctx := context.WithValue(context.Background(), "testKey", "123")13021303	// Test completing an empty string on the childCmd1304	_, output, err := executeCommandWithContextC(ctx, rootCmd, ShellCompNoDescRequestCmd, "childCmd", "")1305	if err != nil {1306		t.Errorf("Unexpected error: %v", err)1307	}13081309	expected := strings.Join([]string{1310		":0",1311		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")13121313	if output != expected {1314		t.Errorf("expected: %q, got: %q", expected, output)1315	}1316}13171318func TestValidArgsFuncSingleCmd(t *testing.T) {1319	rootCmd := &Command{1320		Use:               "root",1321		ValidArgsFunction: validArgsFunc,1322		Run:               emptyRun,1323	}13241325	// Test completing an empty string1326	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")1327	if err != nil {1328		t.Errorf("Unexpected error: %v", err)1329	}13301331	expected := strings.Join([]string{1332		"one",1333		"two",1334		":0",1335		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")13361337	if output != expected {1338		t.Errorf("expected: %q, got: %q", expected, output)1339	}13401341	// Check completing with a prefix1342	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "t")1343	if err != nil {1344		t.Errorf("Unexpected error: %v", err)1345	}13461347	expected = strings.Join([]string{1348		"two",1349		":0",1350		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")13511352	if output != expected {1353		t.Errorf("expected: %q, got: %q", expected, output)1354	}1355}13561357func TestValidArgsFuncSingleCmdInvalidArg(t *testing.T) {1358	rootCmd := &Command{1359		Use: "root",1360		// If we don't specify a value for Args, this test fails.1361		// This is only true for a root command without any subcommands, and is caused1362		// by the fact that the __complete command becomes a subcommand when there should not be one.1363		// The problem is in the implementation of legacyArgs().1364		Args:              MinimumNArgs(1),1365		ValidArgsFunction: validArgsFunc,1366		Run:               emptyRun,1367	}13681369	// Check completing with wrong number of args1370	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "unexpectedArg", "t")1371	if err != nil {1372		t.Errorf("Unexpected error: %v", err)1373	}13741375	expected := strings.Join([]string{1376		":4",1377		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")13781379	if output != expected {1380		t.Errorf("expected: %q, got: %q", expected, output)1381	}1382}13831384func TestValidArgsFuncChildCmds(t *testing.T) {1385	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1386	child1Cmd := &Command{1387		Use:               "child1",1388		ValidArgsFunction: validArgsFunc,1389		Run:               emptyRun,1390	}1391	child2Cmd := &Command{1392		Use:               "child2",1393		ValidArgsFunction: validArgsFunc2,1394		Run:               emptyRun,1395	}1396	rootCmd.AddCommand(child1Cmd, child2Cmd)13971398	// Test completion of first sub-command with empty argument1399	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "")1400	if err != nil {1401		t.Errorf("Unexpected error: %v", err)1402	}14031404	expected := strings.Join([]string{1405		"one",1406		"two",1407		":0",1408		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")14091410	if output != expected {1411		t.Errorf("expected: %q, got: %q", expected, output)1412	}14131414	// Test completion of first sub-command with a prefix to complete1415	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "t")1416	if err != nil {1417		t.Errorf("Unexpected error: %v", err)1418	}14191420	expected = strings.Join([]string{1421		"two",1422		":0",1423		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")14241425	if output != expected {1426		t.Errorf("expected: %q, got: %q", expected, output)1427	}14281429	// Check completing with wrong number of args1430	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child1", "unexpectedArg", "t")1431	if err != nil {1432		t.Errorf("Unexpected error: %v", err)1433	}14341435	expected = strings.Join([]string{1436		":4",1437		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")14381439	if output != expected {1440		t.Errorf("expected: %q, got: %q", expected, output)1441	}14421443	// Test completion of second sub-command with empty argument1444	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "")1445	if err != nil {1446		t.Errorf("Unexpected error: %v", err)1447	}14481449	expected = strings.Join([]string{1450		"three",1451		"four",1452		":0",1453		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")14541455	if output != expected {1456		t.Errorf("expected: %q, got: %q", expected, output)1457	}14581459	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "t")1460	if err != nil {1461		t.Errorf("Unexpected error: %v", err)1462	}14631464	expected = strings.Join([]string{1465		"three",1466		":0",1467		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")14681469	if output != expected {1470		t.Errorf("expected: %q, got: %q", expected, output)1471	}14721473	// Check completing with wrong number of args1474	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "child2", "unexpectedArg", "t")1475	if err != nil {1476		t.Errorf("Unexpected error: %v", err)1477	}14781479	expected = strings.Join([]string{1480		":4",1481		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")14821483	if output != expected {1484		t.Errorf("expected: %q, got: %q", expected, output)1485	}1486}14871488func TestValidArgsFuncAliases(t *testing.T) {1489	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1490	child := &Command{1491		Use:               "child",1492		Aliases:           []string{"son", "daughter"},1493		ValidArgsFunction: validArgsFunc,1494		Run:               emptyRun,1495	}1496	rootCmd.AddCommand(child)14971498	// Test completion of first sub-command with empty argument1499	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "")1500	if err != nil {1501		t.Errorf("Unexpected error: %v", err)1502	}15031504	expected := strings.Join([]string{1505		"one",1506		"two",1507		":0",1508		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")15091510	if output != expected {1511		t.Errorf("expected: %q, got: %q", expected, output)1512	}15131514	// Test completion of first sub-command with a prefix to complete1515	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "daughter", "t")1516	if err != nil {1517		t.Errorf("Unexpected error: %v", err)1518	}15191520	expected = strings.Join([]string{1521		"two",1522		":0",1523		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")15241525	if output != expected {1526		t.Errorf("expected: %q, got: %q", expected, output)1527	}15281529	// Check completing with wrong number of args1530	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "son", "unexpectedArg", "t")1531	if err != nil {1532		t.Errorf("Unexpected error: %v", err)1533	}15341535	expected = strings.Join([]string{1536		":4",1537		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")15381539	if output != expected {1540		t.Errorf("expected: %q, got: %q", expected, output)1541	}1542}15431544func TestValidArgsFuncInBashScript(t *testing.T) {1545	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1546	child := &Command{1547		Use:               "child",1548		ValidArgsFunction: validArgsFunc,1549		Run:               emptyRun,1550	}1551	rootCmd.AddCommand(child)15521553	buf := new(bytes.Buffer)1554	assertNoErr(t, rootCmd.GenBashCompletion(buf))1555	output := buf.String()15561557	check(t, output, "has_completion_function=1")1558}15591560func TestNoValidArgsFuncInBashScript(t *testing.T) {1561	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1562	child := &Command{1563		Use: "child",1564		Run: emptyRun,1565	}1566	rootCmd.AddCommand(child)15671568	buf := new(bytes.Buffer)1569	assertNoErr(t, rootCmd.GenBashCompletion(buf))1570	output := buf.String()15711572	checkOmit(t, output, "has_completion_function=1")1573}15741575func TestCompleteCmdInBashScript(t *testing.T) {1576	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1577	child := &Command{1578		Use:               "child",1579		ValidArgsFunction: validArgsFunc,1580		Run:               emptyRun,1581	}1582	rootCmd.AddCommand(child)15831584	buf := new(bytes.Buffer)1585	assertNoErr(t, rootCmd.GenBashCompletion(buf))1586	output := buf.String()15871588	check(t, output, ShellCompNoDescRequestCmd)1589}15901591func TestCompleteNoDesCmdInZshScript(t *testing.T) {1592	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1593	child := &Command{1594		Use:               "child",1595		ValidArgsFunction: validArgsFunc,1596		Run:               emptyRun,1597	}1598	rootCmd.AddCommand(child)15991600	buf := new(bytes.Buffer)1601	assertNoErr(t, rootCmd.GenZshCompletionNoDesc(buf))1602	output := buf.String()16031604	check(t, output, ShellCompNoDescRequestCmd)1605}16061607func TestCompleteCmdInZshScript(t *testing.T) {1608	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1609	child := &Command{1610		Use:               "child",1611		ValidArgsFunction: validArgsFunc,1612		Run:               emptyRun,1613	}1614	rootCmd.AddCommand(child)16151616	buf := new(bytes.Buffer)1617	assertNoErr(t, rootCmd.GenZshCompletion(buf))1618	output := buf.String()16191620	check(t, output, ShellCompRequestCmd)1621	checkOmit(t, output, ShellCompNoDescRequestCmd)1622}16231624func TestFlagCompletionInGo(t *testing.T) {1625	rootCmd := &Command{1626		Use: "root",1627		Run: emptyRun,1628	}1629	rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")1630	assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("introot", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1631		completions := []string{}1632		for _, comp := range []string{"1\tThe first", "2\tThe second", "10\tThe tenth"} {1633			if strings.HasPrefix(comp, toComplete) {1634				completions = append(completions, comp)1635			}1636		}1637		return completions, ShellCompDirectiveDefault1638	}))1639	rootCmd.Flags().String("filename", "", "Enter a filename")1640	assertNoErr(t, rootCmd.RegisterFlagCompletionFunc("filename", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1641		completions := []string{}1642		for _, comp := range []string{"file.yaml\tYAML format", "myfile.json\tJSON format", "file.xml\tXML format"} {1643			if strings.HasPrefix(comp, toComplete) {1644				completions = append(completions, comp)1645			}1646		}1647		return completions, ShellCompDirectiveNoSpace | ShellCompDirectiveNoFileComp1648	}))16491650	// Test completing an empty string1651	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "")1652	if err != nil {1653		t.Errorf("Unexpected error: %v", err)1654	}16551656	expected := strings.Join([]string{1657		"1",1658		"2",1659		"10",1660		":0",1661		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")16621663	if output != expected {1664		t.Errorf("expected: %q, got: %q", expected, output)1665	}16661667	// Check completing with a prefix1668	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--introot", "1")1669	if err != nil {1670		t.Errorf("Unexpected error: %v", err)1671	}16721673	expected = strings.Join([]string{1674		"1",1675		"10",1676		":0",1677		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")16781679	if output != expected {1680		t.Errorf("expected: %q, got: %q", expected, output)1681	}16821683	// Test completing an empty string1684	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "")1685	if err != nil {1686		t.Errorf("Unexpected error: %v", err)1687	}16881689	expected = strings.Join([]string{1690		"file.yaml",1691		"myfile.json",1692		"file.xml",1693		":6",1694		"Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")16951696	if output != expected {1697		t.Errorf("expected: %q, got: %q", expected, output)1698	}16991700	// Check completing with a prefix1701	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--filename", "f")1702	if err != nil {1703		t.Errorf("Unexpected error: %v", err)1704	}17051706	expected = strings.Join([]string{1707		"file.yaml",1708		"file.xml",1709		":6",1710		"Completion ended with directive: ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp", ""}, "\n")17111712	if output != expected {1713		t.Errorf("expected: %q, got: %q", expected, output)1714	}1715}17161717func TestValidArgsFuncChildCmdsWithDesc(t *testing.T) {1718	rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}1719	child1Cmd := &Command{1720		Use:               "child1",1721		ValidArgsFunction: validArgsFunc,1722		Run:               emptyRun,1723	}1724	child2Cmd := &Command{1725		Use:               "child2",1726		ValidArgsFunction: validArgsFunc2,1727		Run:               emptyRun,1728	}1729	rootCmd.AddCommand(child1Cmd, child2Cmd)17301731	// Test completion of first sub-command with empty argument1732	output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child1", "")1733	if err != nil {1734		t.Errorf("Unexpected error: %v", err)1735	}17361737	expected := strings.Join([]string{1738		"one\tThe first",1739		"two\tThe second",1740		":0",1741		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")17421743	if output != expected {1744		t.Errorf("expected: %q, got: %q", expected, output)1745	}17461747	// Test completion of first sub-command with a prefix to complete1748	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "t")1749	if err != nil {1750		t.Errorf("Unexpected error: %v", err)1751	}17521753	expected = strings.Join([]string{1754		"two\tThe second",1755		":0",1756		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")17571758	if output != expected {1759		t.Errorf("expected: %q, got: %q", expected, output)1760	}17611762	// Check completing with wrong number of args1763	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child1", "unexpectedArg", "t")1764	if err != nil {1765		t.Errorf("Unexpected error: %v", err)1766	}17671768	expected = strings.Join([]string{1769		":4",1770		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")17711772	if output != expected {1773		t.Errorf("expected: %q, got: %q", expected, output)1774	}17751776	// Test completion of second sub-command with empty argument1777	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "")1778	if err != nil {1779		t.Errorf("Unexpected error: %v", err)1780	}17811782	expected = strings.Join([]string{1783		"three\tThe third",1784		"four\tThe fourth",1785		":0",1786		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")17871788	if output != expected {1789		t.Errorf("expected: %q, got: %q", expected, output)1790	}17911792	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "t")1793	if err != nil {1794		t.Errorf("Unexpected error: %v", err)1795	}17961797	expected = strings.Join([]string{1798		"three\tThe third",1799		":0",1800		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")18011802	if output != expected {1803		t.Errorf("expected: %q, got: %q", expected, output)1804	}18051806	// Check completing with wrong number of args1807	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "unexpectedArg", "t")1808	if err != nil {1809		t.Errorf("Unexpected error: %v", err)1810	}18111812	expected = strings.Join([]string{1813		":4",1814		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")18151816	if output != expected {1817		t.Errorf("expected: %q, got: %q", expected, output)1818	}1819}18201821func TestFlagCompletionWithNotInterspersedArgs(t *testing.T) {1822	rootCmd := &Command{Use: "root", Run: emptyRun}1823	childCmd := &Command{1824		Use: "child",1825		Run: emptyRun,1826		ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1827			return []string{"--validarg", "test"}, ShellCompDirectiveDefault1828		},1829	}1830	childCmd2 := &Command{1831		Use:       "child2",1832		Run:       emptyRun,1833		ValidArgs: []string{"arg1", "arg2"},1834	}1835	rootCmd.AddCommand(childCmd, childCmd2)1836	childCmd.Flags().Bool("bool", false, "test bool flag")1837	childCmd.Flags().String("string", "", "test string flag")1838	_ = childCmd.RegisterFlagCompletionFunc("string", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {1839		return []string{"myval"}, ShellCompDirectiveDefault1840	})18411842	// Test flag completion with no argument1843	output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--")1844	if err != nil {1845		t.Errorf("Unexpected error: %v", err)1846	}18471848	expected := strings.Join([]string{1849		"--bool\ttest bool flag",1850		"--help\thelp for child",1851		"--string\ttest string flag",1852		":4",1853		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")18541855	if output != expected {1856		t.Errorf("expected: %q, got: %q", expected, output)1857	}18581859	// Test that no flags are completed after the -- arg1860	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "-")1861	if err != nil {1862		t.Errorf("Unexpected error: %v", err)1863	}18641865	expected = strings.Join([]string{1866		"--validarg",1867		"test",1868		":0",1869		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")18701871	if output != expected {1872		t.Errorf("expected: %q, got: %q", expected, output)1873	}18741875	// Test that no flags are completed after the -- arg with a flag set1876	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--bool", "--", "-")1877	if err != nil {1878		t.Errorf("Unexpected error: %v", err)1879	}18801881	expected = strings.Join([]string{1882		"--validarg",1883		"test",1884		":0",1885		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")18861887	if output != expected {1888		t.Errorf("expected: %q, got: %q", expected, output)1889	}18901891	// set Interspersed to false which means that no flags should be completed after the first arg1892	childCmd.Flags().SetInterspersed(false)18931894	// Test that no flags are completed after the first arg1895	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "arg", "--")1896	if err != nil {1897		t.Errorf("Unexpected error: %v", err)1898	}18991900	expected = strings.Join([]string{1901		"--validarg",1902		"test",1903		":0",1904		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")19051906	if output != expected {1907		t.Errorf("expected: %q, got: %q", expected, output)1908	}19091910	// Test that no flags are completed after the fist arg with a flag set1911	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--string", "t", "arg", "--")1912	if err != nil {1913		t.Errorf("Unexpected error: %v", err)1914	}19151916	expected = strings.Join([]string{1917		"--validarg",1918		"test",1919		":0",1920		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")19211922	if output != expected {1923		t.Errorf("expected: %q, got: %q", expected, output)1924	}19251926	// Check that args are still completed after --1927	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "")1928	if err != nil {1929		t.Errorf("Unexpected error: %v", err)1930	}19311932	expected = strings.Join([]string{1933		"--validarg",1934		"test",1935		":0",1936		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")19371938	if output != expected {1939		t.Errorf("expected: %q, got: %q", expected, output)1940	}19411942	// Check that args are still completed even if flagname with ValidArgsFunction exists1943	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--string", "")1944	if err != nil {1945		t.Errorf("Unexpected error: %v", err)1946	}19471948	expected = strings.Join([]string{1949		"--validarg",1950		"test",1951		":0",1952		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")19531954	if output != expected {1955		t.Errorf("expected: %q, got: %q", expected, output)1956	}19571958	// Check that args are still completed even if flagname with ValidArgsFunction exists1959	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child2", "--", "a")1960	if err != nil {1961		t.Errorf("Unexpected error: %v", err)1962	}19631964	expected = strings.Join([]string{1965		"arg1",1966		"arg2",1967		":4",1968		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")19691970	if output != expected {1971		t.Errorf("expected: %q, got: %q", expected, output)1972	}19731974	// Check that --validarg is not parsed as flag after --1975	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--", "--validarg", "")1976	if err != nil {1977		t.Errorf("Unexpected error: %v", err)1978	}19791980	expected = strings.Join([]string{1981		"--validarg",1982		"test",1983		":0",1984		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")19851986	if output != expected {1987		t.Errorf("expected: %q, got: %q", expected, output)1988	}19891990	// Check that --validarg is not parsed as flag after an arg1991	output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "arg", "--validarg", "")1992	if err != nil {1993		t.Errorf("Unexpected error: %v", err)1994	}19951996	expected = strings.Join([]string{1997		"--validarg",1998		"test",1999		":0",2000		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")

The first 2,000 of 4,129 lines.