Skip to content

Commit

Permalink
Really fix TestApp_RunAsSubCommandIncorrectUsage
Browse files Browse the repository at this point in the history
An earlier commit 6564c0f made an attempt at fixing this test,
but apparently it is not working correctly with Go <= 1.16, because
the panic is expected, and it only happens with Go 1.17+.

Rather than adding an ugly kludge checking the runtime go version,
let's have two versions of the test.

Once Go 1.16 is no longer supported, we can remove the go116_test.go
file.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed May 3, 2022
1 parent 936bd2e commit b370d04
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 b370d04

Please sign in to comment.