Skip to content

Commit

Permalink
Merge branch 'v1' into v1-no-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed May 4, 2022
2 parents c8432d2 + 9810d12 commit 654178d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
25 changes: 0 additions & 25 deletions app_test.go
Expand Up @@ -507,31 +507,6 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
expect(t, context.String("lang"), "spanish")
}

func TestApp_RunAsSubCommandIncorrectUsage(t *testing.T) {
// Go 1.17+ panics when invalid flag is given.
// Catch it here and consider the test passed.
defer func() {
if err := recover(); err == nil {
t.Fatal("expected error, got nothing")
}
}()

a := App{
Flags: []Flag{
StringFlag{Name: "--foo"},
},
Writer: bytes.NewBufferString(""),
}

set := flag.NewFlagSet("", flag.ContinueOnError)
_ = set.Parse([]string{"", "---foo"})
c := &Context{flagSet: set}

err := a.RunAsSubcommand(c)

expect(t, err, errors.New("bad flag syntax: ---foo"))
}

func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
var parsedOption string
var args []string
Expand Down
28 changes: 28 additions & 0 deletions go116_test.go
@@ -0,0 +1,28 @@
//go:build !go1.17
// +build !go1.17

package cli

import (
"bytes"
"errors"
"flag"
"testing"
)

func TestApp_RunAsSubCommandIncorrectUsage(t *testing.T) {
a := App{
Flags: []Flag{
StringFlag{Name: "--foo"},
},
Writer: bytes.NewBufferString(""),
}

set := flag.NewFlagSet("", flag.ContinueOnError)
_ = set.Parse([]string{"", "---foo"})
c := &Context{flagSet: set}

err := a.RunAsSubcommand(c)

expect(t, err, errors.New("bad flag syntax: ---foo"))
}
33 changes: 33 additions & 0 deletions go117_test.go
@@ -0,0 +1,33 @@
//go:build go1.17
// +build go1.17

package cli

import (
"bytes"
"flag"
"testing"
)

func TestApp_RunAsSubCommandIncorrectUsage(t *testing.T) {
a := App{
Flags: []Flag{
StringFlag{Name: "--foo"},
},
Writer: bytes.NewBufferString(""),
}

set := flag.NewFlagSet("", flag.ContinueOnError)
_ = set.Parse([]string{"", "---foo"})
c := &Context{flagSet: set}

// Go 1.17+ panics when invalid flag is given.
// Catch it here and consider the test passed.
defer func() {
if err := recover(); err == nil {
t.Fatal("expected error, got nothing")
}
}()

_ = a.RunAsSubcommand(c)
}

0 comments on commit 654178d

Please sign in to comment.