package altsrc
import (
"flag"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"testing"
)
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.App{}
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) {
_ = os.Setenv("THE_TEST", "10")
defer os.Setenv("THE_TEST", "")
expect(t, val, 10)
NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) {
cleanup := writeTempFile(t, fileName, nestedJSON)
val := c.Int("top.test")
NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) {
test := []string{"test-cmd", "--load", fileName, "--test", "7"}
expect(t, val, 7)
func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) {
test := []string{"test-cmd", "--load", fileName, "--top.test", "7"}
NewIntFlag(&cli.IntFlag{Name: "top.test"}),
func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) {
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) {
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T) {
_ = os.Setenv("THE_TEST", "11")
expect(t, val, 11)
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *testing.T) {
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
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)