mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
Update dependencies
This commit is contained in:
90
vendor/github.com/spf13/cobra/command_test.go
generated
vendored
90
vendor/github.com/spf13/cobra/command_test.go
generated
vendored
@@ -5,7 +5,10 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// test to ensure hidden commands run as intended
|
||||
@@ -116,7 +119,7 @@ func TestStripFlags(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_DisableFlagParsing(t *testing.T) {
|
||||
func TestDisableFlagParsing(t *testing.T) {
|
||||
as := []string{"-v", "-race", "-file", "foo.go"}
|
||||
targs := []string{}
|
||||
cmdPrint := &Command{
|
||||
@@ -143,7 +146,7 @@ func TestInitHelpFlagMergesFlags(t *testing.T) {
|
||||
cmd := Command{Use: "do"}
|
||||
baseCmd.AddCommand(&cmd)
|
||||
|
||||
cmd.initHelpFlag()
|
||||
cmd.InitDefaultHelpFlag()
|
||||
actual := cmd.Flags().Lookup("help").Usage
|
||||
if actual != usage {
|
||||
t.Fatalf("Expected the help flag from the base command with usage '%s', but got the default with usage '%s'", usage, actual)
|
||||
@@ -191,8 +194,15 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
|
||||
EnableCommandSorting = true
|
||||
}
|
||||
|
||||
func TestFlagErrorFunc(t *testing.T) {
|
||||
func TestSetOutput(t *testing.T) {
|
||||
cmd := &Command{}
|
||||
cmd.SetOutput(nil)
|
||||
if out := cmd.OutOrStdout(); out != os.Stdout {
|
||||
t.Fatalf("expected setting output to nil to revert back to stdout, got %v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagErrorFunc(t *testing.T) {
|
||||
cmd := &Command{
|
||||
Use: "print",
|
||||
RunE: func(cmd *Command, args []string) error {
|
||||
@@ -214,3 +224,77 @@ func TestFlagErrorFunc(t *testing.T) {
|
||||
t.Errorf("expected %v, got %v", expected, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestSortedFlags checks,
|
||||
// if cmd.LocalFlags() is unsorted when cmd.Flags().SortFlags set to false.
|
||||
// Related to https://github.com/spf13/cobra/issues/404.
|
||||
func TestSortedFlags(t *testing.T) {
|
||||
cmd := &Command{}
|
||||
cmd.Flags().SortFlags = false
|
||||
names := []string{"C", "B", "A", "D"}
|
||||
for _, name := range names {
|
||||
cmd.Flags().Bool(name, false, "")
|
||||
}
|
||||
|
||||
i := 0
|
||||
cmd.LocalFlags().VisitAll(func(f *pflag.Flag) {
|
||||
if i == len(names) {
|
||||
return
|
||||
}
|
||||
if isStringInStringSlice(f.Name, names) {
|
||||
if names[i] != f.Name {
|
||||
t.Errorf("Incorrect order. Expected %v, got %v", names[i], f.Name)
|
||||
}
|
||||
i++
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// contains checks, if s is in ss.
|
||||
func isStringInStringSlice(s string, ss []string) bool {
|
||||
for _, v := range ss {
|
||||
if v == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestHelpFlagInHelp checks,
|
||||
// if '--help' flag is shown in help for child (executing `parent help child`),
|
||||
// that has no other flags.
|
||||
// Related to https://github.com/spf13/cobra/issues/302.
|
||||
func TestHelpFlagInHelp(t *testing.T) {
|
||||
output := new(bytes.Buffer)
|
||||
parent := &Command{Use: "parent", Run: func(*Command, []string) {}}
|
||||
parent.SetOutput(output)
|
||||
|
||||
child := &Command{Use: "child", Run: func(*Command, []string) {}}
|
||||
parent.AddCommand(child)
|
||||
|
||||
parent.SetArgs([]string{"help", "child"})
|
||||
err := parent.Execute()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !strings.Contains(output.String(), "[flags]") {
|
||||
t.Errorf("\nExpecting to contain: %v\nGot: %v", "[flags]", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestMergeCommandLineToFlags checks,
|
||||
// if pflag.CommandLine is correctly merged to c.Flags() after first call
|
||||
// of c.mergePersistentFlags.
|
||||
// Related to https://github.com/spf13/cobra/issues/443.
|
||||
func TestMergeCommandLineToFlags(t *testing.T) {
|
||||
pflag.Bool("boolflag", false, "")
|
||||
c := &Command{Use: "c", Run: func(*Command, []string) {}}
|
||||
c.mergePersistentFlags()
|
||||
if c.Flags().Lookup("boolflag") == nil {
|
||||
t.Fatal("Expecting to have flag from CommandLine in c.Flags()")
|
||||
}
|
||||
|
||||
// Reset pflag.CommandLine flagset.
|
||||
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user