Skip to content
Myr

cmd_test.go

This file

Path

doc/cmd_test.go

105 lines · 3.5 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 doc1617import (18	"strings"19	"testing"2021	"github.com/spf13/cobra"22)2324func emptyRun(*cobra.Command, []string) {}2526func init() {27	rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")28	rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")2930	echoCmd.PersistentFlags().StringP("strone", "s", "one", "help message for flag strone")31	echoCmd.PersistentFlags().BoolP("persistentbool", "p", false, "help message for flag persistentbool")32	echoCmd.Flags().IntP("intone", "i", 123, "help message for flag intone")33	echoCmd.Flags().BoolP("boolone", "b", true, "help message for flag boolone")3435	timesCmd.PersistentFlags().StringP("strtwo", "t", "2", "help message for child flag strtwo")36	timesCmd.Flags().IntP("inttwo", "j", 234, "help message for flag inttwo")37	timesCmd.Flags().BoolP("booltwo", "c", false, "help message for flag booltwo")3839	printCmd.PersistentFlags().StringP("strthree", "s", "three", "help message for flag strthree")40	printCmd.Flags().IntP("intthree", "i", 345, "help message for flag intthree")41	printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")4243	echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)44	rootCmd.AddCommand(printCmd, echoCmd, dummyCmd)45}4647var rootCmd = &cobra.Command{48	Use:   "root",49	Short: "Root short description",50	Long:  "Root long description",51	Run:   emptyRun,52}5354var echoCmd = &cobra.Command{55	Use:     "echo [string to echo]",56	Aliases: []string{"say"},57	Short:   "Echo anything to the screen",58	Long:    "an utterly useless command for testing",59	Example: "Just run cobra-test echo",60}6162var echoSubCmd = &cobra.Command{63	Use:   "echosub [string to print]",64	Short: "second sub command for echo",65	Long:  "an absolutely utterly useless command for testing gendocs!.",66	Run:   emptyRun,67}6869var timesCmd = &cobra.Command{70	Use:        "times [# times] [string to echo]",71	SuggestFor: []string{"counts"},72	Short:      "Echo anything to the screen more times",73	Long:       `a slightly useless command for testing.`,74	Run:        emptyRun,75}7677var deprecatedCmd = &cobra.Command{78	Use:        "deprecated [can't do anything here]",79	Short:      "A command which is deprecated",80	Long:       `an absolutely utterly useless command for testing deprecation!.`,81	Deprecated: "Please use echo instead",82}8384var printCmd = &cobra.Command{85	Use:   "print [string to print]",86	Short: "Print anything to the screen",87	Long:  `an absolutely utterly useless command for testing.`,88}8990var dummyCmd = &cobra.Command{91	Use:   "dummy [action]",92	Short: "Performs a dummy action",93}9495func checkStringContains(t *testing.T, got, expected string) {96	if !strings.Contains(got, expected) {97		t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)98	}99}100101func checkStringOmits(t *testing.T, got, expected string) {102	if strings.Contains(got, expected) {103		t.Errorf("Expected to not contain: \n %v\nGot: %v", expected, got)104	}105}