Merge branch 'master' into patch-1

This commit is contained in:
Ajitem Sahasrabuddhe 2020-01-13 22:09:06 +05:30 committed by GitHub
commit aee5241828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 185 additions and 194 deletions

12
.github/ISSUE_TEMPLATE/question.md vendored Normal file
View File

@ -0,0 +1,12 @@
---
name: ask a question
about: ask us question - assume stackoverflow's guidelines apply here
title: 'q: ( your question title goes here )'
labels: 'type: question, status: triage, version: v2'
assignees: lynncyrin
---
## my question is...
_**( Put the question text here )**_

View File

@ -28,6 +28,11 @@ jobs:
with:
go-version: ${{ matrix.go }}
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: Set GOPATH and PATH
run: |
echo "::set-env name=GOPATH::$(dirname $GITHUB_WORKSPACE)"
@ -44,7 +49,7 @@ jobs:
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
npm install -g markdown-toc@1.2.0
- name: Run Tests
run: |

View File

@ -4,6 +4,7 @@ package altsrc
import (
"flag"
"github.com/urfave/cli/v2"
)

View File

@ -3,12 +3,13 @@ package altsrc
import (
"flag"
"fmt"
"github.com/urfave/cli/v2"
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/urfave/cli/v2"
)
type testApplyInputSource struct {

View File

@ -1,8 +1,9 @@
package altsrc
import (
"github.com/urfave/cli/v2"
"time"
"github.com/urfave/cli/v2"
)
// InputSourceContext is an interface used to allow

View File

@ -2,10 +2,11 @@ package altsrc
import (
"flag"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"testing"
"github.com/urfave/cli/v2"
)
const (

View File

@ -3,11 +3,12 @@ package altsrc
import (
"encoding/json"
"fmt"
"github.com/urfave/cli/v2"
"io"
"io/ioutil"
"strings"
"time"
"github.com/urfave/cli/v2"
)
// NewJSONSourceFromFlagFunc returns a func that takes a cli.Context
@ -78,9 +79,9 @@ func (x *jsonSource) Duration(name string) (time.Duration, error) {
if err != nil {
return 0, err
}
v, ok := (time.Duration)(0), false
if v, ok = i.(time.Duration); !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name)
v, ok := i.(time.Duration)
if !ok {
return 0, fmt.Errorf("unexpected type %T for %q", i, name)
}
return v, nil
}
@ -90,9 +91,9 @@ func (x *jsonSource) Float64(name string) (float64, error) {
if err != nil {
return 0, err
}
v, ok := (float64)(0), false
if v, ok = i.(float64); !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name)
v, ok := i.(float64)
if !ok {
return 0, fmt.Errorf("unexpected type %T for %q", i, name)
}
return v, nil
}
@ -102,9 +103,9 @@ func (x *jsonSource) String(name string) (string, error) {
if err != nil {
return "", err
}
v, ok := "", false
if v, ok = i.(string); !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name)
v, ok := i.(string)
if !ok {
return "", fmt.Errorf("unexpected type %T for %q", i, name)
}
return v, nil
}
@ -160,9 +161,9 @@ func (x *jsonSource) Generic(name string) (cli.Generic, error) {
if err != nil {
return nil, err
}
v, ok := (cli.Generic)(nil), false
if v, ok = i.(cli.Generic); !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name)
v, ok := i.(cli.Generic)
if !ok {
return nil, fmt.Errorf("unexpected type %T for %q", i, name)
}
return v, nil
}
@ -172,9 +173,9 @@ func (x *jsonSource) Bool(name string) (bool, error) {
if err != nil {
return false, err
}
v, ok := false, false
if v, ok = i.(bool); !ok {
return v, fmt.Errorf("unexpected type %T for %q", i, name)
v, ok := i.(bool)
if !ok {
return false, fmt.Errorf("unexpected type %T for %q", i, name)
}
return v, nil
}

View File

@ -2,10 +2,11 @@ package altsrc
import (
"fmt"
"github.com/urfave/cli/v2"
"reflect"
"strings"
"time"
"github.com/urfave/cli/v2"
)
// MapInputSource implements InputSourceContext to return

View File

@ -20,6 +20,6 @@ func TestMapDuration(t *testing.T) {
d, err = inputSource.Duration("duration_of_string_type")
expect(t, time.Minute, d)
expect(t, nil, err)
d, err = inputSource.Duration("duration_of_int_type")
_, err = inputSource.Duration("duration_of_int_type")
refute(t, nil, err)
}

View File

@ -2,10 +2,11 @@ package altsrc
import (
"flag"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"testing"
"github.com/urfave/cli/v2"
)
func TestCommandTomFileTest(t *testing.T) {

View File

@ -2,10 +2,11 @@ package altsrc
import (
"flag"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"testing"
"github.com/urfave/cli/v2"
)
func TestCommandYamlFileTest(t *testing.T) {

11
app.go
View File

@ -7,7 +7,6 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"sort"
"time"
)
@ -485,16 +484,6 @@ func (a *App) VisibleFlags() []Flag {
return visibleFlags(a.Flags)
}
func (a *App) hasFlag(flag Flag) bool {
for _, f := range a.Flags {
if reflect.DeepEqual(flag, f) {
return true
}
}
return false
}
func (a *App) errWriter() io.Writer {
// When the app ErrWriter is nil use the package level one.
if a.ErrWriter == nil {

View File

@ -612,7 +612,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
app.Flags = []Flag{
&BoolFlag{Name: "one", Aliases: []string{"o"}},
@ -633,7 +633,7 @@ func TestApp_UseShortOptionHandling(t *testing.T) {
}
func TestApp_UseShortOptionHandling_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
app.Flags = []Flag{
&StringFlag{Name: "name", Aliases: []string{"n"}},
@ -648,7 +648,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -673,7 +673,7 @@ func TestApp_UseShortOptionHandlingCommand(t *testing.T) {
}
func TestApp_UseShortOptionHandlingCommand_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -692,7 +692,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
var name string
expected := "expectedName"
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -722,7 +722,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
}
func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) {
app := NewApp()
app := newTestApp()
app.UseShortOptionHandling = true
command := &Command{
Name: "cmd",
@ -859,30 +859,12 @@ func TestApp_DefaultStdout(t *testing.T) {
}
}
type mockWriter struct {
written []byte
}
func (fw *mockWriter) Write(p []byte) (n int, err error) {
if fw.written == nil {
fw.written = p
} else {
fw.written = append(fw.written, p...)
}
return len(p), nil
}
func (fw *mockWriter) GetWritten() (b []byte) {
return fw.written
}
func TestApp_SetStdout(t *testing.T) {
w := &mockWriter{}
var w bytes.Buffer
app := &App{
Name: "test",
Writer: w,
Writer: &w,
}
err := app.Run([]string{"help"})
@ -891,7 +873,7 @@ func TestApp_SetStdout(t *testing.T) {
t.Fatalf("Run error: %s", err)
}
if len(w.written) == 0 {
if w.Len() == 0 {
t.Error("App did not write output to desired writer.")
}
}
@ -925,6 +907,7 @@ func TestApp_BeforeFunc(t *testing.T) {
Flags: []Flag{
&StringFlag{Name: "opt"},
},
Writer: ioutil.Discard,
}
// run with the Before() func succeeding
@ -1186,7 +1169,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) {
// setup
app := NewApp()
app := newTestApp()
app.Flags = test.appFlags
app.Commands = test.appCommands
@ -1280,7 +1263,6 @@ func TestApp_OrderOfOperations(t *testing.T) {
app := &App{
EnableBashCompletion: true,
BashComplete: func(c *Context) {
_, _ = fmt.Fprintf(os.Stderr, "---> BashComplete(%#v)\n", c)
counts.Total++
counts.ShellComplete = counts.Total
},
@ -1289,6 +1271,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.OnUsageError = counts.Total
return errors.New("hay OnUsageError")
},
Writer: ioutil.Discard,
}
beforeNoError := func(c *Context) error {
@ -1422,7 +1405,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
}
for _, flagSet := range subcommandHelpTopics {
t.Logf("==> checking with flags %v", flagSet)
t.Run(fmt.Sprintf("checking with flags %v", flagSet), func(t *testing.T) {
app := &App{}
buf := new(bytes.Buffer)
@ -1465,6 +1448,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output)
}
}
})
}
}
@ -1610,9 +1594,9 @@ func TestApp_Run_Help(t *testing.T) {
var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}}
for _, args := range helpArguments {
buf := new(bytes.Buffer)
t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
buf := new(bytes.Buffer)
app := &App{
Name: "boom",
@ -1630,11 +1614,11 @@ func TestApp_Run_Help(t *testing.T) {
}
output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "boom - make an explosive entrance") {
t.Errorf("want help to contain %q, did not: \n%q", "boom - make an explosive entrance", output)
}
})
}
}
@ -1642,9 +1626,9 @@ func TestApp_Run_Version(t *testing.T) {
var versionArguments = [][]string{{"boom", "--version"}, {"boom", "-v"}}
for _, args := range versionArguments {
buf := new(bytes.Buffer)
t.Run(fmt.Sprintf("checking with arguments %v", args), func(t *testing.T) {
t.Logf("==> checking with arguments %v", args)
buf := new(bytes.Buffer)
app := &App{
Name: "boom",
@ -1663,11 +1647,11 @@ func TestApp_Run_Version(t *testing.T) {
}
output := buf.String()
t.Logf("output: %q\n", buf.Bytes())
if !strings.Contains(output, "0.1.0") {
t.Errorf("want version to contain %q, did not: \n%q", "0.1.0", output)
}
})
}
}
@ -1835,6 +1819,7 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
Action: func(c *Context) error { return nil },
Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) error { return fmt.Errorf("after error") },
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo"})
@ -1980,6 +1965,7 @@ func (c *customBoolFlag) IsSet() bool {
func TestCustomFlagsUnused(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo"})
@ -1991,6 +1977,7 @@ func TestCustomFlagsUnused(t *testing.T) {
func TestCustomFlagsUsed(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo", "--custom=bar"})
@ -2000,7 +1987,9 @@ func TestCustomFlagsUsed(t *testing.T) {
}
func TestCustomHelpVersionFlags(t *testing.T) {
app := &App{}
app := &App{
Writer: ioutil.Discard,
}
// Be sure to reset the global flags
defer func(helpFlag Flag, versionFlag Flag) {
@ -2018,7 +2007,7 @@ func TestCustomHelpVersionFlags(t *testing.T) {
}
func TestHandleExitCoder_Default(t *testing.T) {
app := NewApp()
app := newTestApp()
fs, err := flagSet(app.Name, app.Flags)
if err != nil {
t.Errorf("error creating FlagSet: %s", err)
@ -2034,7 +2023,7 @@ func TestHandleExitCoder_Default(t *testing.T) {
}
func TestHandleExitCoder_Custom(t *testing.T) {
app := NewApp()
app := newTestApp()
fs, err := flagSet(app.Name, app.Flags)
if err != nil {
t.Errorf("error creating FlagSet: %s", err)
@ -2091,6 +2080,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
Action: func(ctx *Context) error {
return fmt.Errorf("should not get here")
},
Writer: ioutil.Discard,
}
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
if err != nil {
@ -2101,7 +2091,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
testCode := 104
app := NewApp()
app := newTestApp()
app.Commands = []*Command{
{
Name: "cmd",
@ -2120,7 +2110,6 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
var exitCodeFromExitErrHandler int
app.ExitErrHandler = func(c *Context, err error) {
if exitErr, ok := err.(ExitCoder); ok {
t.Log(exitErr)
exitCodeFromExitErrHandler = exitErr.ExitCode()
}
}
@ -2151,3 +2140,9 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
t.Errorf("exitCodeFromOsExiter valeu should be %v, but its value is %v", testCode, exitCodeFromExitErrHandler)
}
}
func newTestApp() *App {
a := NewApp()
a.Writer = ioutil.Discard
return a
}

View File

@ -166,7 +166,7 @@ func TocActionFunc(c *cli.Context) error {
filename = "README.md"
}
err := runCmd("node_modules/.bin/markdown-toc", "-i", filename)
err := runCmd("markdown-toc", "-i", filename)
if err != nil {
return err
}

View File

@ -93,7 +93,7 @@ func TestParseAndRunShortOpts(t *testing.T) {
},
}
app := NewApp()
app := newTestApp()
app.Commands = []*Command{cmd}
err := app.Run(c.testArgs)
@ -116,6 +116,7 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
},
},
},
Writer: ioutil.Discard,
}
err := app.Run([]string{"foo", "bar"})
@ -317,12 +318,12 @@ func TestCommandSkipFlagParsing(t *testing.T) {
&StringFlag{Name: "flag"},
},
Action: func(c *Context) error {
fmt.Printf("%+v\n", c.String("flag"))
args = c.Args()
return nil
},
},
},
Writer: ioutil.Discard,
}
err := app.Run(c.testArgs)

View File

@ -17,7 +17,6 @@ type Context struct {
App *App
Command *Command
shellComplete bool
setFlags map[string]bool
flagSet *flag.FlagSet
parentContext *Context
}

View File

@ -6,7 +6,7 @@ import (
)
func testApp() *App {
app := NewApp()
app := newTestApp()
app.Name = "greet"
app.Flags = []Flag{
&StringFlag{

16
flag.go
View File

@ -203,12 +203,9 @@ func withEnvHint(envVars []string, str string) string {
return str + envText
}
func flagNames(f Flag) []string {
func flagNames(name string, aliases []string) []string {
var ret []string
name := flagStringField(f, "Name")
aliases := flagStringSliceField(f, "Aliases")
for _, part := range append([]string{name}, aliases...) {
// v1 -> v2 migration warning zone:
// Strip off anything after the first found comma or space, which
@ -231,17 +228,6 @@ func flagStringSliceField(f Flag, name string) []string {
return []string{}
}
func flagStringField(f Flag, name string) string {
fv := flagValue(f)
field := fv.FieldByName(name)
if field.IsValid() {
return field.String()
}
return ""
}
func withFileHint(filePath, str string) string {
fileText := ""
if filePath != "" {

View File

@ -34,7 +34,7 @@ func (f *BoolFlag) String() string {
// Names returns the names of the flag
func (f *BoolFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *DurationFlag) String() string {
// Names returns the names of the flag
func (f *DurationFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Float64Flag) String() string {
// Names returns the names of the flag
func (f *Float64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -90,7 +90,7 @@ func (f *Float64SliceFlag) String() string {
// Names returns the names of the flag
func (f *Float64SliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -39,7 +39,7 @@ func (f *GenericFlag) String() string {
// Names returns the names of the flag
func (f *GenericFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *IntFlag) String() string {
// Names returns the names of the flag
func (f *IntFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Int64Flag) String() string {
// Names returns the names of the flag
func (f *Int64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -91,7 +91,7 @@ func (f *Int64SliceFlag) String() string {
// Names returns the names of the flag
func (f *Int64SliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -102,7 +102,7 @@ func (f *IntSliceFlag) String() string {
// Names returns the names of the flag
func (f *IntSliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -30,7 +30,7 @@ func (f *PathFlag) String() string {
// Names returns the names of the flag
func (f *PathFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -31,7 +31,7 @@ func (f *StringFlag) String() string {
// Names returns the names of the flag
func (f *StringFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -86,7 +86,7 @@ func (f *StringSliceFlag) String() string {
// Names returns the names of the flag
func (f *StringSliceFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -1719,6 +1719,7 @@ func TestTimestampFlagApply(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "randomlayout"}
set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})
@ -1728,6 +1729,7 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Layout(t *testing.T) {
func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) {
fl := TimestampFlag{Name: "time", Aliases: []string{"t"}, Layout: "Jan 2, 2006 at 3:04pm (MST)"}
set := flag.NewFlagSet("test", 0)
set.SetOutput(ioutil.Discard)
_ = fl.Apply(set)
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})

View File

@ -86,7 +86,7 @@ func (f *TimestampFlag) String() string {
// Names returns the names of the flag
func (f *TimestampFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *UintFlag) String() string {
// Names returns the names of the flag
func (f *UintFlag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -34,7 +34,7 @@ func (f *Uint64Flag) String() string {
// Names returns the names of the flag
func (f *Uint64Flag) Names() []string {
return flagNames(f)
return flagNames(f.Name, f.Aliases)
}
// IsRequired returns whether or not the flag is required

View File

@ -24,9 +24,3 @@ func expect(t *testing.T, a interface{}, b interface{}) {
t.Errorf("(%s:%d) Expected %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func refute(t *testing.T, a interface{}, b interface{}) {
if reflect.DeepEqual(a, b) {
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}