From 7109cf0739444a4af1c69aa32e3464287e8a1b15 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 4 Dec 2019 21:20:58 +0530 Subject: [PATCH 01/11] add ProgramName and CommandName variables --- app.go | 4 ++++ command.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/app.go b/app.go index 6e729841c1..ec3979ddc5 100644 --- a/app.go +++ b/app.go @@ -28,6 +28,10 @@ var ( type App struct { // The name of the program. Defaults to path.Base(os.Args[0]) Name string + // + ProgramName string + // + CommandName string // Full name of command for help, defaults to Name HelpName string // Description of the program. diff --git a/command.go b/command.go index e7cb97a6af..c87fe8411c 100644 --- a/command.go +++ b/command.go @@ -100,6 +100,8 @@ type Commands []Command func (c Command) Run(ctx *Context) (err error) { if len(c.Subcommands) > 0 { return c.startApp(ctx) + } else { + ctx.App.CommandName = fmt.Sprintf("%s %s", ctx.App.CommandName, c.Name) } if !c.HideHelp && (HelpFlag != BoolFlag{}) { @@ -315,6 +317,9 @@ func (c Command) startApp(ctx *Context) error { app.ExitErrHandler = ctx.App.ExitErrHandler // set the name and usage app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) + app.ProgramName = ctx.App.Name + app.CommandName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) + if c.HelpName == "" { app.HelpName = c.HelpName } else { From 273c22d6cc3dccb56bc9a5185569a5c0983418a3 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 4 Dec 2019 21:24:26 +0530 Subject: [PATCH 02/11] add comments to variables --- app.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index ec3979ddc5..d447dde311 100644 --- a/app.go +++ b/app.go @@ -28,9 +28,10 @@ var ( type App struct { // The name of the program. Defaults to path.Base(os.Args[0]) Name string - // + // The name of the program. Defaults to path.Base(os.Args[0]) ProgramName string - // + // The name of the current running command. In case of a sub-command, this variable would also contain + // the names of its parents. CommandName string // Full name of command for help, defaults to Name HelpName string From 04cf24dc863a0091e3ed7698dc704b79db85fbe6 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 4 Dec 2019 21:36:46 +0530 Subject: [PATCH 03/11] add test case --- command_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/command_test.go b/command_test.go index bcfbee747e..999d1a3b8d 100644 --- a/command_test.go +++ b/command_test.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "io/ioutil" + "log" "strings" "testing" ) @@ -371,3 +372,78 @@ func TestCommandSkipFlagParsing(t *testing.T) { expect(t, args, c.expectedArgs) } } + +func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) { + cases := []struct { + testArgs []string + expectedOutput []string + }{ + { + testArgs: []string{""}, + expectedOutput: []string{"myprogramname", "", ""}, + }, + { + testArgs: []string{"foo"}, + expectedOutput: []string{"myprogramname", "foo", ""}, + }, + { + testArgs: []string{"foo", "bar"}, + expectedOutput: []string{"myprogramname", "foo", "bar"}, + }, + { + testArgs: []string{"foo", "bar", "baz"}, + expectedOutput: []string{"myprogramname", "foo", "bar", "baz"}, + }, + } + + for _, c := range cases { + var output []string + app := &App{ + Name: "myprogramname", + Action: func(c *Context) error { + fmt.Println("c.App.Name for app.Action is", c.App.Name) + fmt.Println("c.App.ProgramName for app.Action is", c.App.ProgramName) + fmt.Println("c.App.CommandName for app.Action is", c.App.CommandName) + return nil + }, + Commands: []Command{ + { + Name: "foo", + Action: func(c *Context) error { + output = append(output, c.App.Name) + output = append(output, c.App.ProgramName) + output = append(output, c.App.CommandName) + return nil + }, + Subcommands: []Command{ + { + Name: "bar", + Action: func(c *Context) error { + output = append(output, c.App.Name) + output = append(output, c.App.ProgramName) + output = append(output, c.App.CommandName) + return nil + }, + Subcommands: []Command{ + { + Name: "baz", + Action: func(c *Context) error { + output = append(output, c.App.Name) + output = append(output, c.App.ProgramName) + output = append(output, c.App.CommandName) + return nil + }, + }, + }, + }, + }, + }, + }, + } + + err := app.Run(c.testArgs) + if err != nil { + log.Fatal() + } + } +} From 3e600cd3efd17d86ad51ac42810d2f1bb1cc1d5f Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 4 Dec 2019 21:54:01 +0530 Subject: [PATCH 04/11] refer program name instead of app name --- command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command.go b/command.go index c87fe8411c..6adc6c2378 100644 --- a/command.go +++ b/command.go @@ -318,7 +318,7 @@ func (c Command) startApp(ctx *Context) error { // set the name and usage app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) app.ProgramName = ctx.App.Name - app.CommandName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) + app.CommandName = fmt.Sprintf("%s %s", ctx.App.ProgramName, c.Name) if c.HelpName == "" { app.HelpName = c.HelpName From 435001909b04e1506da9d48ee8ff161e6ab2bf6a Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Wed, 4 Dec 2019 22:00:11 +0530 Subject: [PATCH 05/11] Update command_test.go add err to the log --- command_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command_test.go b/command_test.go index 999d1a3b8d..412b0bfd0c 100644 --- a/command_test.go +++ b/command_test.go @@ -443,7 +443,7 @@ func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) { err := app.Run(c.testArgs) if err != nil { - log.Fatal() + log.Fatal(err) } } } From a4e0c0194728ff9c665ca137429b334c2b514505 Mon Sep 17 00:00:00 2001 From: Ajitem Sahasrabuddhe Date: Thu, 5 Dec 2019 12:11:39 +0530 Subject: [PATCH 06/11] fix failing tests --- app.go | 4 ++++ command.go | 4 ++-- command_test.go | 30 ++++++++++++++++++------------ helpers_test.go | 12 ++++++++++++ 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/app.go b/app.go index d447dde311..c46c09909c 100644 --- a/app.go +++ b/app.go @@ -143,6 +143,10 @@ func (a *App) Setup() { a.didSetup = true + if a.Name != "" && a.ProgramName == "" { + a.ProgramName = a.Name + } + if a.Author != "" || a.Email != "" { a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) } diff --git a/command.go b/command.go index 6adc6c2378..4a665c5b6b 100644 --- a/command.go +++ b/command.go @@ -317,8 +317,8 @@ func (c Command) startApp(ctx *Context) error { app.ExitErrHandler = ctx.App.ExitErrHandler // set the name and usage app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) - app.ProgramName = ctx.App.Name - app.CommandName = fmt.Sprintf("%s %s", ctx.App.ProgramName, c.Name) + app.ProgramName = ctx.App.ProgramName + app.CommandName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) if c.HelpName == "" { app.HelpName = c.HelpName diff --git a/command_test.go b/command_test.go index 999d1a3b8d..43594e62d9 100644 --- a/command_test.go +++ b/command_test.go @@ -379,31 +379,32 @@ func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) { expectedOutput []string }{ { - testArgs: []string{""}, - expectedOutput: []string{"myprogramname", "", ""}, + testArgs: []string{"cmd"}, + expectedOutput: []string{"myprogramname", "myprogramname", ""}, }, { - testArgs: []string{"foo"}, - expectedOutput: []string{"myprogramname", "foo", ""}, + testArgs: []string{"cmd", "foo"}, + expectedOutput: []string{"myprogramname foo", "myprogramname", "myprogramname foo"}, }, { - testArgs: []string{"foo", "bar"}, - expectedOutput: []string{"myprogramname", "foo", "bar"}, + testArgs: []string{"cmd", "foo", "bar"}, + expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar"}, }, { - testArgs: []string{"foo", "bar", "baz"}, - expectedOutput: []string{"myprogramname", "foo", "bar", "baz"}, + testArgs: []string{"cmd", "foo", "bar", "baz"}, + expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar baz"}, }, } for _, c := range cases { var output []string + app := &App{ Name: "myprogramname", Action: func(c *Context) error { - fmt.Println("c.App.Name for app.Action is", c.App.Name) - fmt.Println("c.App.ProgramName for app.Action is", c.App.ProgramName) - fmt.Println("c.App.CommandName for app.Action is", c.App.CommandName) + output = append(output, c.App.Name) + output = append(output, c.App.ProgramName) + output = append(output, c.App.CommandName) return nil }, Commands: []Command{ @@ -443,7 +444,12 @@ func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) { err := app.Run(c.testArgs) if err != nil { - log.Fatal() + log.Fatal(err) + } + + if !equal(output, c.expectedOutput) { + t.Errorf("want %v, got %v", c.expectedOutput, output) } + } } diff --git a/helpers_test.go b/helpers_test.go index 109ea7ad91..d00db32aaa 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -26,3 +26,15 @@ func refute(t *testing.T, a interface{}, b interface{}) { t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) } } + +func equal(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} From 873a3f46b2520b69842a5911c6bf69d66c0ff9a2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 14:01:00 -0400 Subject: [PATCH 07/11] Cut out many CI things that either don't work on v1 anymore or are specific to the `master` branch. --- .github/workflows/cli.yml | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index c06a112b99..1d109b5311 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -3,7 +3,6 @@ name: Run Tests on: pull_request: branches: - - master - v1 env: @@ -36,30 +35,11 @@ jobs: ref: ${{ github.ref }} - name: Install Dependencies - run: | - mkdir -p $GOPATH/bin - curl -L -o $GOPATH/bin/gfmrun "https://github.com/urfave/gfmrun/releases/download/v1.2.14/gfmrun-$(go env GOOS)-amd64-v1.2.14" - chmod +x $GOPATH/bin/gfmrun - npm install markdown-toc + run: npm install markdown-toc - name: Run Tests (v1) if: contains(github.base_ref, 'v1') run: | go run build.go vet go run build.go test - go run build.go gfmrun docs/v1/manual.md go run build.go toc docs/v1/manual.md - - - name: Run Tests (v2) - if: contains(github.base_ref, 'master') - run: | - go run build.go vet - go run build.go test - go run build.go gfmrun docs/v2/manual.md - go run build.go toc docs/v2/manual.md - - - name: Send Coverage Report - if: success() - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - run: bash <(curl -s https://codecov.io/bash) \ No newline at end of file From 8bf3af2d0ac845c6643a68395b99f4f9df97e800 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 14:11:30 -0400 Subject: [PATCH 08/11] More general v1 branch CI updates :-/ --- .github/workflows/cli.yml | 25 ++++++++++--------------- .gitignore | 3 ++- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 1d109b5311..5540a227a5 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -4,31 +4,26 @@ on: pull_request: branches: - v1 - -env: - GO111MODULE: on - GOPROXY: https://proxy.golang.org + push: + branches: + - v1 + tags: + - v1.* jobs: test: strategy: matrix: os: [ubuntu-latest, macos-latest] - go: [1.11, 1.12, 1.13] + go: [^1.15, ^1.16] name: ${{ matrix.os }} @ Go ${{ matrix.go }} runs-on: ${{ matrix.os }} steps: - name: Set up Go ${{ matrix.go }} - uses: actions/setup-go@v1 + uses: actions/setup-go@v3 with: go-version: ${{ matrix.go }} - - name: Set GOPATH and PATH - run: | - echo "::set-env name=GOPATH::$(dirname $GITHUB_WORKSPACE)" - echo "::add-path::$(dirname $GITHUB_WORKSPACE)/bin" - shell: bash - - name: Checkout Code uses: actions/checkout@v1 with: @@ -39,7 +34,7 @@ jobs: - name: Run Tests (v1) if: contains(github.base_ref, 'v1') - run: | - go run build.go vet - go run build.go test + run: + go run build.go vet && + go run build.go test && go run build.go toc docs/v1/manual.md diff --git a/.gitignore b/.gitignore index 9c2506032c..8ae196feed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.coverprofile +coverage.txt node_modules/ vendor -.idea \ No newline at end of file +.idea From 8ec98e21eb6355ed7cb23e7a097a545ae7eff3a0 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 14:14:36 -0400 Subject: [PATCH 09/11] More ~fighting~ playing with CI bits --- .github/workflows/cli.yml | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 5540a227a5..4dc562eb45 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest] - go: [^1.15, ^1.16] + go: [1.15.x, 1.16.x] name: ${{ matrix.os }} @ Go ${{ matrix.go }} runs-on: ${{ matrix.os }} steps: @@ -25,15 +25,12 @@ jobs: go-version: ${{ matrix.go }} - name: Checkout Code - uses: actions/checkout@v1 - with: - ref: ${{ github.ref }} + uses: actions/checkout@v3 - name: Install Dependencies run: npm install markdown-toc - - name: Run Tests (v1) - if: contains(github.base_ref, 'v1') + - name: Run Tests run: go run build.go vet && go run build.go test && From dd02d52c40cbf898cc2aab66802b9dab33d54151 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 14:19:47 -0400 Subject: [PATCH 10/11] Drop back to only testing go 1.16.x as 1.15.x is no longer supported by the Go team and both 1.17.x and 1.18.x can't run the tests --- .github/workflows/cli.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 4dc562eb45..f95ee2e607 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -15,7 +15,13 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest] - go: [1.15.x, 1.16.x] + go: + - 1.16.x + # NOTE: tests fail with panic at + # TestApp_RunAsSubCommandIncorrectUsage on these + # versions: + # - 1.17.x + # - 1.18.x name: ${{ matrix.os }} @ Go ${{ matrix.go }} runs-on: ${{ matrix.os }} steps: From 6a3382c0dbd2bcab3c4ce831033fc341b43825fc Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 14:29:30 -0400 Subject: [PATCH 11/11] Back out some changes from #973 --- app.go | 9 ------ command.go | 5 ---- command_test.go | 80 ------------------------------------------------- helpers_test.go | 12 -------- 4 files changed, 106 deletions(-) diff --git a/app.go b/app.go index d3f825cad5..382f238f49 100644 --- a/app.go +++ b/app.go @@ -28,11 +28,6 @@ var ( type App struct { // The name of the program. Defaults to path.Base(os.Args[0]) Name string - // The name of the program. Defaults to path.Base(os.Args[0]) - ProgramName string - // The name of the current running command. In case of a sub-command, this variable would also contain - // the names of its parents. - CommandName string // Full name of command for help, defaults to Name HelpName string // Description of the program. @@ -143,10 +138,6 @@ func (a *App) Setup() { a.didSetup = true - if a.Name != "" && a.ProgramName == "" { - a.ProgramName = a.Name - } - if a.Author != "" || a.Email != "" { a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) } diff --git a/command.go b/command.go index c71770ae3c..f02d3589ff 100644 --- a/command.go +++ b/command.go @@ -100,8 +100,6 @@ type Commands []Command func (c Command) Run(ctx *Context) (err error) { if len(c.Subcommands) > 0 { return c.startApp(ctx) - } else { - ctx.App.CommandName = fmt.Sprintf("%s %s", ctx.App.CommandName, c.Name) } if !c.HideHelp && (HelpFlag != BoolFlag{}) { @@ -316,9 +314,6 @@ func (c Command) startApp(ctx *Context) error { app.ExitErrHandler = ctx.App.ExitErrHandler // set the name and usage app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) - app.ProgramName = ctx.App.ProgramName - app.CommandName = fmt.Sprintf("%s %s", ctx.App.Name, c.Name) - if c.HelpName == "" { app.HelpName = c.HelpName } else { diff --git a/command_test.go b/command_test.go index 17be1cc3e8..a59d368c46 100644 --- a/command_test.go +++ b/command_test.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io/ioutil" - "log" "strings" "testing" ) @@ -374,85 +373,6 @@ func TestCommandSkipFlagParsing(t *testing.T) { } } -func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) { - cases := []struct { - testArgs []string - expectedOutput []string - }{ - { - testArgs: []string{"cmd"}, - expectedOutput: []string{"myprogramname", "myprogramname", ""}, - }, - { - testArgs: []string{"cmd", "foo"}, - expectedOutput: []string{"myprogramname foo", "myprogramname", "myprogramname foo"}, - }, - { - testArgs: []string{"cmd", "foo", "bar"}, - expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar"}, - }, - { - testArgs: []string{"cmd", "foo", "bar", "baz"}, - expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar baz"}, - }, - } - - for _, c := range cases { - var output []string - - app := &App{ - Name: "myprogramname", - Action: func(c *Context) error { - output = append(output, c.App.Name) - output = append(output, c.App.ProgramName) - output = append(output, c.App.CommandName) - return nil - }, - Commands: []Command{ - { - Name: "foo", - Action: func(c *Context) error { - output = append(output, c.App.Name) - output = append(output, c.App.ProgramName) - output = append(output, c.App.CommandName) - return nil - }, - Subcommands: []Command{ - { - Name: "bar", - Action: func(c *Context) error { - output = append(output, c.App.Name) - output = append(output, c.App.ProgramName) - output = append(output, c.App.CommandName) - return nil - }, - Subcommands: []Command{ - { - Name: "baz", - Action: func(c *Context) error { - output = append(output, c.App.Name) - output = append(output, c.App.ProgramName) - output = append(output, c.App.CommandName) - return nil - }, - }, - }, - }, - }, - }, - }, - } - err := app.Run(c.testArgs) - if err != nil { - log.Fatal(err) - } - - if !equal(output, c.expectedOutput) { - t.Errorf("want %v, got %v", c.expectedOutput, output) - } - } -} - func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) { cases := []struct { testArgs []string diff --git a/helpers_test.go b/helpers_test.go index d00db32aaa..109ea7ad91 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -26,15 +26,3 @@ func refute(t *testing.T, a interface{}, b interface{}) { t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) } } - -func equal(a, b []string) bool { - if len(a) != len(b) { - return false - } - for i, v := range a { - if v != b[i] { - return false - } - } - return true -}