Skip to content
Myr

active_help_test.go

This file

Path

active_help_test.go

400 lines · 12.0 KiB · Go

Last changed by

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

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	"fmt"19	"os"20	"strings"21	"testing"22)2324const (25	activeHelpMessage  = "This is an activeHelp message"26	activeHelpMessage2 = "This is the rest of the activeHelp message"27)2829func TestActiveHelpAlone(t *testing.T) {30	rootCmd := &Command{31		Use: "root",32		Run: emptyRun,33	}3435	activeHelpFunc := func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {36		comps := AppendActiveHelp(nil, activeHelpMessage)37		return comps, ShellCompDirectiveDefault38	}3940	// Test that activeHelp can be added to a root command41	rootCmd.ValidArgsFunction = activeHelpFunc4243	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "")44	if err != nil {45		t.Errorf("Unexpected error: %v", err)46	}4748	expected := strings.Join([]string{49		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),50		":0",51		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")5253	if output != expected {54		t.Errorf("expected: %q, got: %q", expected, output)55	}5657	rootCmd.ValidArgsFunction = nil5859	// Test that activeHelp can be added to a child command60	childCmd := &Command{61		Use:   "thechild",62		Short: "The child command",63		Run:   emptyRun,64	}65	rootCmd.AddCommand(childCmd)6667	childCmd.ValidArgsFunction = activeHelpFunc6869	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")70	if err != nil {71		t.Errorf("Unexpected error: %v", err)72	}7374	expected = strings.Join([]string{75		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),76		":0",77		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")7879	if output != expected {80		t.Errorf("expected: %q, got: %q", expected, output)81	}82}8384func TestActiveHelpWithComps(t *testing.T) {85	rootCmd := &Command{86		Use: "root",87		Run: emptyRun,88	}8990	childCmd := &Command{91		Use:   "thechild",92		Short: "The child command",93		Run:   emptyRun,94	}95	rootCmd.AddCommand(childCmd)9697	// Test that activeHelp can be added following other completions98	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {99		comps := []string{"first", "second"}100		comps = AppendActiveHelp(comps, activeHelpMessage)101		return comps, ShellCompDirectiveDefault102	}103104	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")105	if err != nil {106		t.Errorf("Unexpected error: %v", err)107	}108109	expected := strings.Join([]string{110		"first",111		"second",112		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),113		":0",114		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")115116	if output != expected {117		t.Errorf("expected: %q, got: %q", expected, output)118	}119120	// Test that activeHelp can be added preceding other completions121	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {122		var comps []string123		comps = AppendActiveHelp(comps, activeHelpMessage)124		comps = append(comps, []string{"first", "second"}...)125		return comps, ShellCompDirectiveDefault126	}127128	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")129	if err != nil {130		t.Errorf("Unexpected error: %v", err)131	}132133	expected = strings.Join([]string{134		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),135		"first",136		"second",137		":0",138		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")139140	if output != expected {141		t.Errorf("expected: %q, got: %q", expected, output)142	}143144	// Test that activeHelp can be added interleaved with other completions145	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {146		comps := []string{"first"}147		comps = AppendActiveHelp(comps, activeHelpMessage)148		comps = append(comps, "second")149		return comps, ShellCompDirectiveDefault150	}151152	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")153	if err != nil {154		t.Errorf("Unexpected error: %v", err)155	}156157	expected = strings.Join([]string{158		"first",159		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),160		"second",161		":0",162		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")163164	if output != expected {165		t.Errorf("expected: %q, got: %q", expected, output)166	}167}168169func TestMultiActiveHelp(t *testing.T) {170	rootCmd := &Command{171		Use: "root",172		Run: emptyRun,173	}174175	childCmd := &Command{176		Use:   "thechild",177		Short: "The child command",178		Run:   emptyRun,179	}180	rootCmd.AddCommand(childCmd)181182	// Test that multiple activeHelp message can be added183	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {184		comps := AppendActiveHelp(nil, activeHelpMessage)185		comps = AppendActiveHelp(comps, activeHelpMessage2)186		return comps, ShellCompDirectiveNoFileComp187	}188189	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")190	if err != nil {191		t.Errorf("Unexpected error: %v", err)192	}193194	expected := strings.Join([]string{195		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),196		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),197		":4",198		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")199200	if output != expected {201		t.Errorf("expected: %q, got: %q", expected, output)202	}203204	// Test that multiple activeHelp messages can be used along with completions205	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {206		comps := []string{"first"}207		comps = AppendActiveHelp(comps, activeHelpMessage)208		comps = append(comps, "second")209		comps = AppendActiveHelp(comps, activeHelpMessage2)210		return comps, ShellCompDirectiveNoFileComp211	}212213	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")214	if err != nil {215		t.Errorf("Unexpected error: %v", err)216	}217218	expected = strings.Join([]string{219		"first",220		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),221		"second",222		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),223		":4",224		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")225226	if output != expected {227		t.Errorf("expected: %q, got: %q", expected, output)228	}229}230231func TestActiveHelpForFlag(t *testing.T) {232	rootCmd := &Command{233		Use: "root",234		Run: emptyRun,235	}236	flagname := "flag"237	rootCmd.Flags().String(flagname, "", "A flag")238239	// Test that multiple activeHelp message can be added240	_ = rootCmd.RegisterFlagCompletionFunc(flagname, func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {241		comps := []string{"first"}242		comps = AppendActiveHelp(comps, activeHelpMessage)243		comps = append(comps, "second")244		comps = AppendActiveHelp(comps, activeHelpMessage2)245		return comps, ShellCompDirectiveNoFileComp246	})247248	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "--flag", "")249	if err != nil {250		t.Errorf("Unexpected error: %v", err)251	}252253	expected := strings.Join([]string{254		"first",255		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage),256		"second",257		fmt.Sprintf("%s%s", activeHelpMarker, activeHelpMessage2),258		":4",259		"Completion ended with directive: ShellCompDirectiveNoFileComp", ""}, "\n")260261	if output != expected {262		t.Errorf("expected: %q, got: %q", expected, output)263	}264}265266func TestConfigActiveHelp(t *testing.T) {267	rootCmd := &Command{268		Use: "root",269		Run: emptyRun,270	}271272	childCmd := &Command{273		Use:   "thechild",274		Short: "The child command",275		Run:   emptyRun,276	}277	rootCmd.AddCommand(childCmd)278279	activeHelpCfg := "someconfig,anotherconfig"280	// Set the variable that the user would be setting281	os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)282283	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {284		receivedActiveHelpCfg := GetActiveHelpConfig(cmd)285		if receivedActiveHelpCfg != activeHelpCfg {286			t.Errorf("expected activeHelpConfig: %q, but got: %q", activeHelpCfg, receivedActiveHelpCfg)287		}288		return nil, ShellCompDirectiveDefault289	}290291	_, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")292	if err != nil {293		t.Errorf("Unexpected error: %v", err)294	}295296	// Test active help config for a flag297	activeHelpCfg = "a config for a flag"298	// Set the variable that the completions scripts will be setting299	os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)300301	flagname := "flag"302	childCmd.Flags().String(flagname, "", "A flag")303304	// Test that multiple activeHelp message can be added305	_ = childCmd.RegisterFlagCompletionFunc(flagname, func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {306		receivedActiveHelpCfg := GetActiveHelpConfig(cmd)307		if receivedActiveHelpCfg != activeHelpCfg {308			t.Errorf("expected activeHelpConfig: %q, but got: %q", activeHelpCfg, receivedActiveHelpCfg)309		}310		return nil, ShellCompDirectiveDefault311	})312313	_, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "--flag", "")314	if err != nil {315		t.Errorf("Unexpected error: %v", err)316	}317}318319func TestDisableActiveHelp(t *testing.T) {320	rootCmd := &Command{321		Use: "root",322		Run: emptyRun,323	}324325	childCmd := &Command{326		Use:   "thechild",327		Short: "The child command",328		Run:   emptyRun,329	}330	rootCmd.AddCommand(childCmd)331332	// Test the disabling of activeHelp using the specific program333	// environment variable that the completions scripts will be setting.334	// Make sure the disabling value is "0" by hard-coding it in the tests;335	// this is for backwards-compatibility as programs will be using this value.336	os.Setenv(activeHelpEnvVar(rootCmd.Name()), "0")337338	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {339		comps := []string{"first"}340		comps = AppendActiveHelp(comps, activeHelpMessage)341		return comps, ShellCompDirectiveDefault342	}343344	output, err := executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")345	if err != nil {346		t.Errorf("Unexpected error: %v", err)347	}348	os.Unsetenv(activeHelpEnvVar(rootCmd.Name()))349350	// Make sure there is no ActiveHelp in the output351	expected := strings.Join([]string{352		"first",353		":0",354		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")355356	if output != expected {357		t.Errorf("expected: %q, got: %q", expected, output)358	}359360	// Now test the global disabling of ActiveHelp361	os.Setenv(activeHelpGlobalEnvVar, "0")362	// Set the specific variable, to make sure it is ignored when the global env363	// var is set properly364	os.Setenv(activeHelpEnvVar(rootCmd.Name()), "1")365366	output, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")367	if err != nil {368		t.Errorf("Unexpected error: %v", err)369	}370371	// Make sure there is no ActiveHelp in the output372	expected = strings.Join([]string{373		"first",374		":0",375		"Completion ended with directive: ShellCompDirectiveDefault", ""}, "\n")376377	if output != expected {378		t.Errorf("expected: %q, got: %q", expected, output)379	}380381	// Make sure that if the global env variable is set to anything else than382	// the disable value it is ignored383	os.Setenv(activeHelpGlobalEnvVar, "on")384	// Set the specific variable, to make sure it is used (while ignoring the global env var)385	activeHelpCfg := "1"386	os.Setenv(activeHelpEnvVar(rootCmd.Name()), activeHelpCfg)387388	childCmd.ValidArgsFunction = func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {389		receivedActiveHelpCfg := GetActiveHelpConfig(cmd)390		if receivedActiveHelpCfg != activeHelpCfg {391			t.Errorf("expected activeHelpConfig: %q, but got: %q", activeHelpCfg, receivedActiveHelpCfg)392		}393		return nil, ShellCompDirectiveDefault394	}395396	_, err = executeCommand(rootCmd, ShellCompNoDescRequestCmd, "thechild", "")397	if err != nil {398		t.Errorf("Unexpected error: %v", err)399	}400}