From b370d046abafab082f73631ba6089edbb9105dfe Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 2 May 2022 19:25:55 -0700 Subject: [PATCH 1/2] Really fix TestApp_RunAsSubCommandIncorrectUsage An earlier commit 6564c0f1f9 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 --- app_test.go | 25 ------------------------- go116_test.go | 28 ++++++++++++++++++++++++++++ go117_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 go116_test.go create mode 100644 go117_test.go diff --git a/app_test.go b/app_test.go index 9aac30cb83..4dc43ba623 100644 --- a/app_test.go +++ b/app_test.go @@ -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 diff --git a/go116_test.go b/go116_test.go new file mode 100644 index 0000000000..f6b5524015 --- /dev/null +++ b/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")) +} diff --git a/go117_test.go b/go117_test.go new file mode 100644 index 0000000000..f40ec4ea82 --- /dev/null +++ b/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) +} From 22281d3e165c85eacb3ae38fe375043907ebe951 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 2 May 2022 18:56:50 -0700 Subject: [PATCH 2/2] Fix CI Due to an issue with commit 6564c0f1f96, only the first line was executed, so the tests were not executed on GHA CI. Fixes: 6564c0f1f96 Signed-off-by: Kir Kolyshkin --- .github/workflows/cli.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 91c8aa533e..93aca5781b 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -31,7 +31,7 @@ jobs: run: npm install markdown-toc - name: Run Tests - run: + run: | go run build.go vet go run build.go test go run build.go toc docs/v1/manual.md