Skip to content
Myr

Add tests for flag completion registration (#2053)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Different problems have been reported about flag completion registration. These two tests are the cases that were not being verified but had been mentioned as problematic. Ref: - https://github.com/spf13/cobra/issues/1320 - https://github.com/spf13/cobra/pull/1438#issuecomment-872928669

authored by Marc Khouzam · committed by GitHub · parent b711e87 · Browse files · 1 file+110 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>

Diff stats

1 file+110 added−0 removed

  • other:+110 added−0 removed

1 file+110 added−0 removed

modified:completions_test.go+110 added
15 unmodified lines
1617import (18 "bytes"19 "context"20 "fmt"21 "strings"22 "sync"23 "testing"24)2526func validArgsFunc(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
2014 unmodified lines
2041 t.Errorf("expected: %q, got: %q", expected, output)2042 }2043}20442045func TestFlagCompletionForPersistentFlagsCalledFromSubCmd(t *testing.T) {2046 rootCmd := &Command{Use: "root", Run: emptyRun}2047 rootCmd.PersistentFlags().String("string", "", "test string flag")2048 _ = rootCmd.RegisterFlagCompletionFunc("string", func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {2049 return []string{"myval"}, ShellCompDirectiveDefault2050 })20512052 childCmd := &Command{2053 Use: "child",2054 Run: emptyRun,2055 ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {2056 return []string{"--validarg", "test"}, ShellCompDirectiveDefault2057 },2058 }2059 childCmd.Flags().Bool("bool", false, "test bool flag")2060 rootCmd.AddCommand(childCmd)20612062 // Test that persistent flag completion works for the subcmd2063 output, err := executeCommand(rootCmd, ShellCompRequestCmd, "child", "--string", "")2064 if err != nil {2065 t.Errorf("Unexpected error: %v", err)2066 }20672068 expected := strings.Join([]string{2069 "myval",2070 ":0",2071 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")20722073 if output != expected {2074 t.Errorf("expected: %q, got: %q", expected, output)2075 }2076}20772078// This test tries to register flag completion concurrently to make sure the2079// code handles concurrency properly.2080// This was reported as a problem when tests are run concurrently:2081// https://github.com/spf13/cobra/issues/13202082//2083// NOTE: this test can sometimes pass even if the code were to not handle2084// concurrency properly. This is not great but the important part is that2085// it should never fail. Therefore, if the tests fails sometimes, we will2086// still be able to know there is a problem.2087func TestFlagCompletionConcurrentRegistration(t *testing.T) {2088 rootCmd := &Command{Use: "root", Run: emptyRun}2089 const maxFlags = 502090 for i := 1; i < maxFlags; i += 2 {2091 flagName := fmt.Sprintf("flag%d", i)2092 rootCmd.Flags().String(flagName, "", fmt.Sprintf("test %s flag on root", flagName))2093 }20942095 childCmd := &Command{2096 Use: "child",2097 Run: emptyRun,2098 }2099 for i := 2; i <= maxFlags; i += 2 {2100 flagName := fmt.Sprintf("flag%d", i)2101 childCmd.Flags().String(flagName, "", fmt.Sprintf("test %s flag on child", flagName))2102 }21032104 rootCmd.AddCommand(childCmd)21052106 // Register completion in different threads to test concurrency.2107 var wg sync.WaitGroup2108 for i := 1; i <= maxFlags; i++ {2109 index := i2110 flagName := fmt.Sprintf("flag%d", i)2111 wg.Add(1)2112 go func() {2113 defer wg.Done()2114 cmd := rootCmd2115 if index%2 == 0 {2116 cmd = childCmd2117 }2118 _ = cmd.RegisterFlagCompletionFunc(flagName, func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {2119 return []string{fmt.Sprintf("flag%d", index)}, ShellCompDirectiveDefault2120 })2121 }()2122 }21232124 wg.Wait()21252126 // Test that flag completion works for each flag2127 for i := 1; i <= 6; i++ {2128 var output string2129 var err error2130 flagName := fmt.Sprintf("flag%d", i)21312132 if i%2 == 1 {2133 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "--"+flagName, "")2134 } else {2135 output, err = executeCommand(rootCmd, ShellCompRequestCmd, "child", "--"+flagName, "")2136 }21372138 if err != nil {2139 t.Errorf("Unexpected error: %v", err)2140 }21412142 expected := strings.Join([]string{2143 flagName,2144 ":0",2145 "Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")21462147 if output != expected {2148 t.Errorf("expected: %q, got: %q", expected, output)2149 }2150 }2151}21522153func TestFlagCompletionInGoWithDesc(t *testing.T) {2154 rootCmd := &Command{2155 Use: "root",2156 Run: emptyRun,
1273 unmodified lines