...

Source file src/github.com/spf13/cobra/doc/cmd_test.go

Documentation: github.com/spf13/cobra/doc

     1  // Copyright 2013-2023 The Cobra Authors
     2  //
     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 at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // 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 and
    13  // limitations under the License.
    14  
    15  package doc
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  func emptyRun(*cobra.Command, []string) {}
    25  
    26  func init() {
    27  	rootCmd.PersistentFlags().StringP("rootflag", "r", "two", "")
    28  	rootCmd.PersistentFlags().StringP("strtwo", "t", "two", "help message for parent flag strtwo")
    29  
    30  	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")
    34  
    35  	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")
    38  
    39  	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")
    42  
    43  	echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)
    44  	rootCmd.AddCommand(printCmd, echoCmd, dummyCmd)
    45  }
    46  
    47  var rootCmd = &cobra.Command{
    48  	Use:   "root",
    49  	Short: "Root short description",
    50  	Long:  "Root long description",
    51  	Run:   emptyRun,
    52  }
    53  
    54  var 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  }
    61  
    62  var 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  }
    68  
    69  var 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  }
    76  
    77  var 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  }
    83  
    84  var 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  }
    89  
    90  var dummyCmd = &cobra.Command{
    91  	Use:   "dummy [action]",
    92  	Short: "Performs a dummy action",
    93  }
    94  
    95  func 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  }
   100  
   101  func 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  }
   106  

View as plain text