From ba801d7ba35475f0b7554c46b63479c543161a2a Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 26 Apr 2022 17:27:35 -0700 Subject: [PATCH 1/3] Move some test helpers from docs_test to fish_test This is in preparation to make docs optional. Backported from commit aabfea87c811de4c6bf6dcd01f64309a8864fead Signed-off-by: Kir Kolyshkin --- docs_test.go | 67 ---------------------------------------------------- fish_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 67 deletions(-) diff --git a/docs_test.go b/docs_test.go index f18d12d2f9..c0c851ed3c 100644 --- a/docs_test.go +++ b/docs_test.go @@ -1,76 +1,9 @@ package cli import ( - "io/ioutil" "testing" ) -func testApp() *App { - app := NewApp() - app.Name = "greet" - app.Flags = []Flag{ - StringFlag{ - Name: "socket, s", - Usage: "some 'usage' text", - Value: "value", - TakesFile: true, - }, - StringFlag{Name: "flag, fl, f"}, - BoolFlag{ - Name: "another-flag, b", - Usage: "another usage text", - }, - } - app.Commands = []Command{{ - Aliases: []string{"c"}, - Flags: []Flag{ - StringFlag{ - Name: "flag, fl, f", - TakesFile: true, - }, - BoolFlag{ - Name: "another-flag, b", - Usage: "another usage text", - }, - }, - Name: "config", - Usage: "another usage test", - Subcommands: []Command{{ - Aliases: []string{"s", "ss"}, - Flags: []Flag{ - StringFlag{Name: "sub-flag, sub-fl, s"}, - BoolFlag{ - Name: "sub-command-flag, s", - Usage: "some usage text", - }, - }, - Name: "sub-config", - Usage: "another usage test", - }}, - }, { - Aliases: []string{"i", "in"}, - Name: "info", - Usage: "retrieve generic information", - }, { - Name: "some-command", - }, { - Name: "hidden-command", - Hidden: true, - }} - app.UsageText = "app [first_arg] [second_arg]" - app.Usage = "Some app" - app.Author = "Harrison" - app.Email = "harrison@lolwut.com" - app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}} - return app -} - -func expectFileContent(t *testing.T, file, expected string) { - data, err := ioutil.ReadFile(file) - expect(t, err, nil) - expect(t, string(data), expected) -} - func TestToMarkdownFull(t *testing.T) { // Given app := testApp() diff --git a/fish_test.go b/fish_test.go index a4c1871438..62313f0bbb 100644 --- a/fish_test.go +++ b/fish_test.go @@ -1,9 +1,76 @@ package cli import ( + "io/ioutil" "testing" ) +func testApp() *App { + app := NewApp() + app.Name = "greet" + app.Flags = []Flag{ + StringFlag{ + Name: "socket, s", + Usage: "some 'usage' text", + Value: "value", + TakesFile: true, + }, + StringFlag{Name: "flag, fl, f"}, + BoolFlag{ + Name: "another-flag, b", + Usage: "another usage text", + }, + } + app.Commands = []Command{{ + Aliases: []string{"c"}, + Flags: []Flag{ + StringFlag{ + Name: "flag, fl, f", + TakesFile: true, + }, + BoolFlag{ + Name: "another-flag, b", + Usage: "another usage text", + }, + }, + Name: "config", + Usage: "another usage test", + Subcommands: []Command{{ + Aliases: []string{"s", "ss"}, + Flags: []Flag{ + StringFlag{Name: "sub-flag, sub-fl, s"}, + BoolFlag{ + Name: "sub-command-flag, s", + Usage: "some usage text", + }, + }, + Name: "sub-config", + Usage: "another usage test", + }}, + }, { + Aliases: []string{"i", "in"}, + Name: "info", + Usage: "retrieve generic information", + }, { + Name: "some-command", + }, { + Name: "hidden-command", + Hidden: true, + }} + app.UsageText = "app [first_arg] [second_arg]" + app.Usage = "Some app" + app.Author = "Harrison" + app.Email = "harrison@lolwut.com" + app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}} + return app +} + +func expectFileContent(t *testing.T, file, expected string) { + data, err := ioutil.ReadFile(file) + expect(t, err, nil) + expect(t, string(data), expected) +} + func TestFishCompletion(t *testing.T) { // Given app := testApp() From fc47b1a82eb945ad87e0c5f49c759ccdeae03df8 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 25 Apr 2022 10:16:31 -0700 Subject: [PATCH 2/3] Add urfave_cli_no_docs build tag This removes the resulting binary dependency on cpuguy83/md2man and russross/blackfriday, which saves more than 400 KB (more than 300 KB once stripped) from the resulting binary. Document this in README. (cherry picked from commit 49e43beba32f7fb2007f557bb77129bf8a0e4300) Signed-off-by: Kir Kolyshkin --- README.md | 10 ++++++++++ docs.go | 3 +++ docs_test.go | 3 +++ 3 files changed, 16 insertions(+) diff --git a/README.md b/README.md index b2abbcf9db..9c2cf851a3 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,16 @@ cli is tested against multiple versions of Go on Linux, and against the latest released version of Go on OS X and Windows. For full details, see [`./.travis.yml`](./.travis.yml) and [`./appveyor.yml`](./appveyor.yml). +### Build tags + +You can use the following build tags: + +#### `urfave_cli_no_docs` + +When set, this removes `ToMarkdown` and `ToMan` methods, so your application +won't be able to call those. This reduces the resulting binary size by about +300-400 KB (measured using Go 1.18.1 on Linux/amd64), due to less dependencies. + ### Using `v1` releases ``` diff --git a/docs.go b/docs.go index 5b94566128..725fa7ff2b 100644 --- a/docs.go +++ b/docs.go @@ -1,3 +1,6 @@ +//go:build !urfave_cli_no_docs +// +build !urfave_cli_no_docs + package cli import ( diff --git a/docs_test.go b/docs_test.go index c0c851ed3c..f4bf1c54b6 100644 --- a/docs_test.go +++ b/docs_test.go @@ -1,3 +1,6 @@ +//go:build !urfave_cli_no_docs +// +build !urfave_cli_no_docs + package cli import ( From 29309253f7da9a60d82e9ea4122096376665a985 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 26 Apr 2022 18:52:43 -0700 Subject: [PATCH 3/3] ci: test newly added tag We run test with the tag set (to make sure nothing is broken). Signed-off-by: Kir Kolyshkin --- .github/workflows/cli.yml | 1 + build.go | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cli.yml b/.github/workflows/cli.yml index 93aca5781b..eccb6a5f58 100644 --- a/.github/workflows/cli.yml +++ b/.github/workflows/cli.yml @@ -34,4 +34,5 @@ jobs: run: | go run build.go vet go run build.go test + go run build.go -tags urfave_cli_no_docs test go run build.go toc docs/v1/manual.md diff --git a/build.go b/build.go index a78ded358f..08cf7bbc21 100644 --- a/build.go +++ b/build.go @@ -41,6 +41,12 @@ func main() { Action: TocActionFunc, }, } + app.Flags = []cli.Flag{ + &cli.StringFlag{ + Name: "tags", + Usage: "set build tags", + }, + } err := app.Run(os.Args) if err != nil { @@ -63,6 +69,8 @@ func VetActionFunc(_ *cli.Context) error { } func TestActionFunc(c *cli.Context) error { + tags := c.String("tags") + for _, pkg := range packages { var packageName string @@ -74,7 +82,7 @@ func TestActionFunc(c *cli.Context) error { coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg) - err := runCmd("go", "test", "-v", coverProfile, packageName) + err := runCmd("go", "test", "-tags", tags, "-v", coverProfile, packageName) if err != nil { return err }