-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #598 from urfave/backport-json-support
Backport JSON InputSource to v1
- Loading branch information
Showing
3 changed files
with
535 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,324 @@ | ||
package altsrc | ||
|
||
import ( | ||
"flag" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"gopkg.in/urfave/cli.v1" | ||
) | ||
|
||
const ( | ||
fileName = "current.json" | ||
simpleJSON = `{"test": 15}` | ||
nestedJSON = `{"top": {"test": 15}}` | ||
) | ||
|
||
func TestCommandJSONFileTest(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, simpleJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("test") | ||
expect(t, val, 15) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "test"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, simpleJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
os.Setenv("THE_TEST", "10") | ||
defer os.Setenv("THE_TEST", "") | ||
|
||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("test") | ||
expect(t, val, 10) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, nestedJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
os.Setenv("THE_TEST", "10") | ||
defer os.Setenv("THE_TEST", "") | ||
|
||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("top.test") | ||
expect(t, val, 10) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, simpleJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
test := []string{"test-cmd", "--load", fileName, "--test", "7"} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("test") | ||
expect(t, val, 7) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "test"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, nestedJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
test := []string{"test-cmd", "--load", fileName, "--top.test", "7"} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("top.test") | ||
expect(t, val, 7) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "top.test"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, simpleJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("test") | ||
expect(t, val, 15) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, nestedJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("top.test") | ||
expect(t, val, 15) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
|
||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, simpleJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
os.Setenv("THE_TEST", "11") | ||
defer os.Setenv("THE_TEST", "") | ||
|
||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("test") | ||
expect(t, val, 11) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *testing.T) { | ||
cleanup := writeTempFile(t, fileName, nestedJSON) | ||
defer cleanup() | ||
|
||
app := cli.NewApp() | ||
set := flag.NewFlagSet("test", 0) | ||
os.Setenv("THE_TEST", "11") | ||
defer os.Setenv("THE_TEST", "") | ||
|
||
test := []string{"test-cmd", "--load", fileName} | ||
set.Parse(test) | ||
|
||
c := cli.NewContext(app, set, nil) | ||
|
||
command := &cli.Command{ | ||
Name: "test-cmd", | ||
Aliases: []string{"tc"}, | ||
Usage: "this is for testing", | ||
Description: "testing", | ||
Action: func(c *cli.Context) error { | ||
val := c.Int("top.test") | ||
expect(t, val, 11) | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}), | ||
&cli.StringFlag{Name: "load"}}, | ||
} | ||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load")) | ||
err := command.Run(c) | ||
|
||
expect(t, err, nil) | ||
} | ||
|
||
func writeTempFile(t *testing.T, name string, content string) func() { | ||
if err := ioutil.WriteFile(name, []byte(content), 0666); err != nil { | ||
t.Fatalf("cannot write %q: %v", name, err) | ||
} | ||
return func() { | ||
if err := os.Remove(name); err != nil { | ||
t.Errorf("cannot remove %q: %v", name, err) | ||
} | ||
} | ||
} |
Oops, something went wrong.