From b9f33fbe6deac41734fe1768ab3d5fa73fc743e6 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 9 May 2016 08:41:01 -0400 Subject: [PATCH 01/10] Add a vet/test runner script with coverage on by default --- .gitignore | 1 + .travis.yml | 9 +++---- CHANGELOG.md | 1 + runtests | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100755 runtests diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7823778 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.coverprofile diff --git a/.travis.yml b/.travis.yml index 76f38a4..4f7bdd7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: go + sudo: false go: @@ -8,16 +9,14 @@ go: - 1.4 - 1.5.4 - 1.6.2 -- tip +- master matrix: allow_failures: - - go: tip + - go: master before_script: - go get github.com/meatballhat/gfmxr/... script: -- go vet ./... -- go test -v ./... -- gfmxr -c $(grep -c 'package main' README.md) -s README.md +- ./runtests diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d3d9c1..0403912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `context.GlobalBoolT` was added as an analogue to `context.GlobalBool` - Support for hiding commands by setting `Hidden: true` -- this will hide the commands in help output +- `./runtests` test runner with coverage tracking by default ### Changed - `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer diff --git a/runtests b/runtests new file mode 100755 index 0000000..fe8b013 --- /dev/null +++ b/runtests @@ -0,0 +1,70 @@ +#!/usr/bin/env python +from __future__ import print_function + +import os +import sys +import tempfile + +from subprocess import check_call + + +PACKAGE_NAME = os.environ.get( + 'CLI_PACKAGE_NAME', 'github.com/codegangsta/cli' +) + + +def main(): + _run('go vet ./...'.split()) + + coverprofiles = [] + for subpackage in ['', 'altsrc']: + coverprofile = 'cli.coverprofile' + if subpackage != '': + coverprofile = '{}.coverprofile'.format(subpackage) + + coverprofiles.append(coverprofile) + + _run('go test -v'.split() + [ + '-coverprofile={}'.format(coverprofile), + '{}/{}'.format(PACKAGE_NAME, subpackage) + ]) + + combined = _combine_coverprofiles(coverprofiles) + _run('go tool cover -func={}'.format(combined.name).split()) + combined.close() + + _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md']) + return 0 + + +def _run(command): + print('runtests: {}'.format(' '.join(command)), file=sys.stderr) + check_call(command) + + +def _gfmxr_count(): + with open('README.md') as infile: + lines = infile.read().splitlines() + return len(filter(_is_go_runnable, lines)) + + +def _is_go_runnable(line): + return line.startswith('package main') + + +def _combine_coverprofiles(coverprofiles): + combined = tempfile.NamedTemporaryFile(suffix='.coverprofile') + combined.write('mode: set\n') + + for coverprofile in coverprofiles: + with open(coverprofile, 'r') as infile: + for line in infile.readlines(): + if not line.startswith('mode: '): + combined.write(line) + + combined.flush() + return combined + + +if __name__ == '__main__': + sys.exit(main()) From 2df2fa514dddba4672c3b0b585cb72b98d1a8032 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 9 May 2016 08:49:38 -0400 Subject: [PATCH 02/10] Skip coverage bits on < go1.2 --- runtests | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/runtests b/runtests index fe8b013..923a80f 100755 --- a/runtests +++ b/runtests @@ -5,7 +5,7 @@ import os import sys import tempfile -from subprocess import check_call +from subprocess import check_call, check_output PACKAGE_NAME = os.environ.get( @@ -16,22 +16,25 @@ PACKAGE_NAME = os.environ.get( def main(): _run('go vet ./...'.split()) - coverprofiles = [] - for subpackage in ['', 'altsrc']: - coverprofile = 'cli.coverprofile' - if subpackage != '': - coverprofile = '{}.coverprofile'.format(subpackage) + if check_output('go version'.split()).split()[2] >= 'go1.2': + coverprofiles = [] + for subpackage in ['', 'altsrc']: + coverprofile = 'cli.coverprofile' + if subpackage != '': + coverprofile = '{}.coverprofile'.format(subpackage) - coverprofiles.append(coverprofile) + coverprofiles.append(coverprofile) - _run('go test -v'.split() + [ - '-coverprofile={}'.format(coverprofile), - '{}/{}'.format(PACKAGE_NAME, subpackage) - ]) + _run('go test -v'.split() + [ + '-coverprofile={}'.format(coverprofile), + '{}/{}'.format(PACKAGE_NAME, subpackage) + ]) - combined = _combine_coverprofiles(coverprofiles) - _run('go tool cover -func={}'.format(combined.name).split()) - combined.close() + combined = _combine_coverprofiles(coverprofiles) + _run('go tool cover -func={}'.format(combined.name).split()) + combined.close() + else: + _run('go test -v ./...'.split()) _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md']) return 0 From d94fdb3e84260d33551531fec6722f863016fc2f Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 9 May 2016 08:58:20 -0400 Subject: [PATCH 03/10] Break runtests back into steps for more granular CI feedback --- .travis.yml | 4 +++- runtests | 62 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4f7bdd7..b117165 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,4 +19,6 @@ before_script: - go get github.com/meatballhat/gfmxr/... script: -- ./runtests +- ./runtests vet +- ./runtests test +- ./runtests gfmxr diff --git a/runtests b/runtests index 923a80f..c295f0c 100755 --- a/runtests +++ b/runtests @@ -13,33 +13,51 @@ PACKAGE_NAME = os.environ.get( ) -def main(): - _run('go vet ./...'.split()) +def main(sysargs=sys.argv[:]): + target = 'test' + if len(sysargs) > 1: + target = sysargs[1] - if check_output('go version'.split()).split()[2] >= 'go1.2': - coverprofiles = [] - for subpackage in ['', 'altsrc']: - coverprofile = 'cli.coverprofile' - if subpackage != '': - coverprofile = '{}.coverprofile'.format(subpackage) + { + 'vet': _vet, + 'test': _test, + 'gfmxr': _gfmxr + }[target]() - coverprofiles.append(coverprofile) - - _run('go test -v'.split() + [ - '-coverprofile={}'.format(coverprofile), - '{}/{}'.format(PACKAGE_NAME, subpackage) - ]) - - combined = _combine_coverprofiles(coverprofiles) - _run('go tool cover -func={}'.format(combined.name).split()) - combined.close() - else: - _run('go test -v ./...'.split()) - - _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md']) return 0 +def _test(): + if check_output('go version'.split()).split()[2] < 'go1.2': + _run('go test -v ./...'.split()) + return + + coverprofiles = [] + for subpackage in ['', 'altsrc']: + coverprofile = 'cli.coverprofile' + if subpackage != '': + coverprofile = '{}.coverprofile'.format(subpackage) + + coverprofiles.append(coverprofile) + + _run('go test -v'.split() + [ + '-coverprofile={}'.format(coverprofile), + '{}/{}'.format(PACKAGE_NAME, subpackage) + ]) + + combined = _combine_coverprofiles(coverprofiles) + _run('go tool cover -func={}'.format(combined.name).split()) + combined.close() + + +def _gfmxr(): + _run(['gfmxr', '-c', str(_gfmxr_count()), '-s', 'README.md']) + + +def _vet(): + _run('go vet ./...'.split()) + + def _run(command): print('runtests: {}'.format(' '.join(command)), file=sys.stderr) check_call(command) From 5fa09b8e23737d63a6b7c51cde75248743067145 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 9 May 2016 09:00:22 -0400 Subject: [PATCH 04/10] Strip trailing slash for cleanliness --- runtests | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtests b/runtests index c295f0c..162e23e 100755 --- a/runtests +++ b/runtests @@ -42,7 +42,7 @@ def _test(): _run('go test -v'.split() + [ '-coverprofile={}'.format(coverprofile), - '{}/{}'.format(PACKAGE_NAME, subpackage) + ('{}/{}'.format(PACKAGE_NAME, subpackage)).rstrip('/') ]) combined = _combine_coverprofiles(coverprofiles) From 33f5de5f18e5bc0703d867bcc356a24853ef2e6a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 10 May 2016 08:09:27 -0400 Subject: [PATCH 05/10] Move the `./runtests` changelog entry to up `Unreleased` --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7416fe5..c974c72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ **ATTN**: This project uses [semantic versioning](http://semver.org/). ## [Unreleased] +### Added +- `./runtests` test runner with coverage tracking by default ## [1.17.0] - 2016-05-09 ### Added @@ -10,7 +12,6 @@ - `context.GlobalBoolT` was added as an analogue to `context.GlobalBool` - Support for hiding commands by setting `Hidden: true` -- this will hide the commands in help output -- `./runtests` test runner with coverage tracking by default ### Changed - `Float64Flag`, `IntFlag`, and `DurationFlag` default values are no longer From 139815765414d09bc7e7de2c9abb8bd9398a5130 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 10 May 2016 08:16:33 -0400 Subject: [PATCH 06/10] Use argparse in runtests like Zeus intended --- runtests | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/runtests b/runtests index 162e23e..feacff3 100755 --- a/runtests +++ b/runtests @@ -1,6 +1,7 @@ #!/usr/bin/env python from __future__ import print_function +import argparse import os import sys import tempfile @@ -14,16 +15,19 @@ PACKAGE_NAME = os.environ.get( def main(sysargs=sys.argv[:]): - target = 'test' - if len(sysargs) > 1: - target = sysargs[1] - - { + targets = { 'vet': _vet, 'test': _test, 'gfmxr': _gfmxr - }[target]() + } + parser = argparse.ArgumentParser() + parser.add_argument( + 'target', nargs='?', choices=tuple(targets.keys()), default='test' + ) + args = parser.parse_args(sysargs[1:]) + + targets[args.target]() return 0 From b9d96954ca2d0794de7a1e5c8580e83ad43a12aa Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 10 May 2016 13:41:43 -0400 Subject: [PATCH 07/10] Fix command alias printing in help text Closes #405 --- CHANGELOG.md | 3 +++ help.go | 4 +-- help_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c974c72..87a3ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ ### Added - `./runtests` test runner with coverage tracking by default +### Fixed +- Printing of command aliases in help text + ## [1.17.0] - 2016-05-09 ### Added - Pluggable flag-level help text rendering via `cli.DefaultFlagStringFunc` diff --git a/help.go b/help.go index 801d2b1..a9e7327 100644 --- a/help.go +++ b/help.go @@ -26,7 +26,7 @@ AUTHOR(S): {{end}}{{if .VisibleCommands}} COMMANDS:{{range .VisibleCategories}}{{if .Name}} {{.Name}}:{{end}}{{range .VisibleCommands}} - {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{"\t"}}{{.Usage}}{{end}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}} {{end}}{{end}}{{if .VisibleFlags}} GLOBAL OPTIONS: {{range .VisibleFlags}}{{.}} @@ -67,7 +67,7 @@ USAGE: COMMANDS:{{range .VisibleCategories}}{{if .Name}} {{.Name}}:{{end}}{{range .VisibleCommands}} - {{.Name}}{{with .ShortName}}, {{.}}{{end}}{{"\t"}}{{.Usage}}{{end}} + {{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}} {{end}}{{if .VisibleFlags}} OPTIONS: {{range .VisibleFlags}}{{.}} diff --git a/help_test.go b/help_test.go index 54836d7..6ed525b 100644 --- a/help_test.go +++ b/help_test.go @@ -169,6 +169,76 @@ func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) { } } +func TestShowAppHelp_CommandAliases(t *testing.T) { + app := &App{ + Commands: []Command{ + { + Name: "frobbly", + Aliases: []string{"fr", "frob"}, + Action: func(ctx *Context) error { + return nil + }, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + app.Run([]string{"foo", "--help"}) + + if !strings.Contains(output.String(), "frobbly, fr, frob") { + t.Errorf("expected output to include all command aliases; got: %q", output.String()) + } +} + +func TestShowCommandHelp_CommandAliases(t *testing.T) { + app := &App{ + Commands: []Command{ + { + Name: "frobbly", + Aliases: []string{"fr", "frob", "bork"}, + Action: func(ctx *Context) error { + return nil + }, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + app.Run([]string{"foo", "help", "fr"}) + + if !strings.Contains(output.String(), "frobbly") { + t.Errorf("expected output to include command name; got: %q", output.String()) + } + + if strings.Contains(output.String(), "bork") { + t.Errorf("expected output to exclude command aliases; got: %q", output.String()) + } +} + +func TestShowSubcommandHelp_CommandAliases(t *testing.T) { + app := &App{ + Commands: []Command{ + { + Name: "frobbly", + Aliases: []string{"fr", "frob", "bork"}, + Action: func(ctx *Context) error { + return nil + }, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + app.Run([]string{"foo", "help"}) + + if !strings.Contains(output.String(), "frobbly, fr, frob, bork") { + t.Errorf("expected output to include command name; got: %q", output.String()) + } +} + func TestShowAppHelp_HiddenCommand(t *testing.T) { app := &App{ Commands: []Command{ From 6f3bb94eae46a2a81011105648c056ffa471e659 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 10 May 2016 13:51:54 -0400 Subject: [PATCH 08/10] Correct assertion text --- help_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help_test.go b/help_test.go index 6ed525b..a664865 100644 --- a/help_test.go +++ b/help_test.go @@ -235,7 +235,7 @@ func TestShowSubcommandHelp_CommandAliases(t *testing.T) { app.Run([]string{"foo", "help"}) if !strings.Contains(output.String(), "frobbly, fr, frob, bork") { - t.Errorf("expected output to include command name; got: %q", output.String()) + t.Errorf("expected output to include all command aliases; got: %q", output.String()) } } From 50b384d9044c17276f2369d333d765a8b01c3bea Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 10 May 2016 14:40:56 -0400 Subject: [PATCH 09/10] Separate badges for package coverage --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c1709ce..409b338 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ -[![Coverage](http://gocover.io/_badge/github.com/codegangsta/cli?0)](http://gocover.io/github.com/codegangsta/cli) [![Build Status](https://travis-ci.org/codegangsta/cli.svg?branch=master)](https://travis-ci.org/codegangsta/cli) [![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli) [![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-codegangsta-cli) [![Go Report Card](https://goreportcard.com/badge/codegangsta/cli)](https://goreportcard.com/report/codegangsta/cli) +[![top level coverage](http://gocover.io/_badge/github.com/codegangsta/cli?0 "top level coverage")](http://gocover.io/github.com/codegangsta/cli) / +[![altsrc coverage](http://gocover.io/_badge/github.com/codegangsta/cli/altsrc?0 "altsrc coverage")](http://gocover.io/github.com/codegangsta/cli/altsrc) + # cli From 92897b9bf6639511e0830b4fc5e9c1c5e796fc34 Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Tue, 10 May 2016 20:42:38 -0700 Subject: [PATCH 10/10] Updating coverage badges --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 409b338..bffd052 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ [![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli) [![codebeat](https://codebeat.co/badges/0a8f30aa-f975-404b-b878-5fab3ae1cc5f)](https://codebeat.co/projects/github-com-codegangsta-cli) [![Go Report Card](https://goreportcard.com/badge/codegangsta/cli)](https://goreportcard.com/report/codegangsta/cli) -[![top level coverage](http://gocover.io/_badge/github.com/codegangsta/cli?0 "top level coverage")](http://gocover.io/github.com/codegangsta/cli) / -[![altsrc coverage](http://gocover.io/_badge/github.com/codegangsta/cli/altsrc?0 "altsrc coverage")](http://gocover.io/github.com/codegangsta/cli/altsrc) +[![top level coverage](https://gocover.io/_badge/github.com/codegangsta/cli?0 "top level coverage")](http://gocover.io/github.com/codegangsta/cli) / +[![altsrc coverage](https://gocover.io/_badge/github.com/codegangsta/cli/altsrc?0 "altsrc coverage")](http://gocover.io/github.com/codegangsta/cli/altsrc) # cli