Add more test coverage around unset input source applying
This commit is contained in:
parent
fe1468cc86
commit
ac641ffda5
@ -26,29 +26,48 @@ type testApplyInputSource struct {
|
|||||||
MapValue interface{}
|
MapValue interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type racyInputSource struct {
|
||||||
|
*MapInputSource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ris *racyInputSource) isSet(name string) bool {
|
||||||
|
if _, ok := ris.MapInputSource.valueMap[name]; ok {
|
||||||
|
ris.MapInputSource.valueMap[name] = bogus{0}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenericApplyInputSourceValue(t *testing.T) {
|
func TestGenericApplyInputSourceValue(t *testing.T) {
|
||||||
v := &Parser{"abc", "def"}
|
v := &Parser{"abc", "def"}
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: v,
|
MapValue: v,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, v, c.Generic("test"))
|
expect(t, v, c.Generic("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, v, c.Generic("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
p := &Parser{"abc", "def"}
|
p := &Parser{"abc", "def"}
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: &Parser{"efg", "hig"},
|
MapValue: &Parser{"efg", "hig"},
|
||||||
ContextValueString: p.String(),
|
ContextValueString: p.String(),
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, p, c.Generic("test"))
|
expect(t, p, c.Generic("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, p, c.Generic("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewGenericFlag(&cli.GenericFlag{
|
Flag: NewGenericFlag(&cli.GenericFlag{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Value: &Parser{},
|
Value: &Parser{},
|
||||||
@ -58,17 +77,25 @@ func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
MapValue: &Parser{"efg", "hij"},
|
MapValue: &Parser{"efg", "hij"},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "abc,def",
|
EnvVarValue: "abc,def",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, &Parser{"abc", "def"}, c.Generic("test"))
|
expect(t, &Parser{"abc", "def"}, c.Generic("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, &Parser{"abc", "def"}, c.Generic("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringSliceApplyInputSourceValue(t *testing.T) {
|
func TestStringSliceApplyInputSourceValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
|
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{"hello", "world"},
|
MapValue: []interface{}{"hello", "world"},
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, c.StringSlice("test"), []string{"hello", "world"})
|
expect(t, c.StringSlice("test"), []string{"hello", "world"})
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, c.StringSlice("test"), []string{"hello", "world"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
@ -82,112 +109,154 @@ func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{"hello", "world"},
|
MapValue: []interface{}{"hello", "world"},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "oh,no",
|
EnvVarValue: "oh,no",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, c.StringSlice("test"), []string{"oh", "no"})
|
expect(t, c.StringSlice("test"), []string{"oh", "no"})
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, c.StringSlice("test"), []string{"oh", "no"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntSliceApplyInputSourceValue(t *testing.T) {
|
func TestIntSliceApplyInputSourceValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, c.IntSlice("test"), []int{1, 2})
|
expect(t, c.IntSlice("test"), []int{1, 2})
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, c.IntSlice("test"), []int{1, 2})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
ContextValueString: "3",
|
ContextValueString: "3",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, c.IntSlice("test"), []int{3})
|
expect(t, c.IntSlice("test"), []int{3})
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, c.IntSlice("test"), []int{3})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "3,4",
|
EnvVarValue: "3,4",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, c.IntSlice("test"), []int{3, 4})
|
expect(t, c.IntSlice("test"), []int{3, 4})
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, c.IntSlice("test"), []int{3, 4})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBoolApplyInputSourceMethodSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: true,
|
MapValue: true,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, true, c.Bool("test"))
|
expect(t, true, c.Bool("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, true, c.Bool("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: false,
|
MapValue: false,
|
||||||
ContextValueString: "true",
|
ContextValueString: "true",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, true, c.Bool("test"))
|
expect(t, true, c.Bool("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, true, c.Bool("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: false,
|
MapValue: false,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "true",
|
EnvVarValue: "true",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, true, c.Bool("test"))
|
expect(t, true, c.Bool("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, true, c.Bool("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringApplyInputSourceMethodSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, "hello", c.String("test"))
|
expect(t, "hello", c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, "hello", c.String("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
ContextValueString: "goodbye",
|
ContextValueString: "goodbye",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, "goodbye", c.String("test"))
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, "goodbye", c.String("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewStringFlag(&cli.StringFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "goodbye",
|
EnvVarValue: "goodbye",
|
||||||
})
|
|
||||||
expect(t, "goodbye", c.String("test"))
|
|
||||||
}
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, "goodbye", c.String("test"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPathApplyInputSourceMethodSet(t *testing.T) {
|
func TestPathApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
SourcePath: "/path/to/source/file",
|
SourcePath: "/path/to/source/file",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
|
|
||||||
expected := "/path/to/source/hello"
|
expected := "/path/to/source/hello"
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
@ -200,125 +269,176 @@ func TestPathApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect(t, expected, c.String("test"))
|
expect(t, expected, c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, expected, c.String("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPathApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestPathApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
ContextValueString: "goodbye",
|
ContextValueString: "goodbye",
|
||||||
SourcePath: "/path/to/source/file",
|
SourcePath: "/path/to/source/file",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, "goodbye", c.String("test"))
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, "goodbye", c.String("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPathApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestPathApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewPathFlag(&cli.PathFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "goodbye",
|
EnvVarValue: "goodbye",
|
||||||
SourcePath: "/path/to/source/file",
|
SourcePath: "/path/to/source/file",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, "goodbye", c.String("test"))
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, "goodbye", c.String("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntApplyInputSourceMethodSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 15, c.Int("test"))
|
expect(t, 15, c.Int("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 15, c.Int("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
func TestIntApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: -1,
|
MapValue: -1,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, -1, c.Int("test"))
|
expect(t, -1, c.Int("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, -1, c.Int("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
ContextValueString: "7",
|
ContextValueString: "7",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 7, c.Int("test"))
|
expect(t, 7, c.Int("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 7, c.Int("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: "12",
|
EnvVarValue: "12",
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 12, c.Int("test"))
|
expect(t, 12, c.Int("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 12, c.Int("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDurationApplyInputSourceMethodSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 30*time.Second, c.Duration("test"))
|
expect(t, 30*time.Second, c.Duration("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 30*time.Second, c.Duration("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDurationApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
func TestDurationApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: -30 * time.Second,
|
MapValue: -30 * time.Second,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, -30*time.Second, c.Duration("test"))
|
expect(t, -30*time.Second, c.Duration("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, -30*time.Second, c.Duration("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
ContextValueString: (15 * time.Second).String(),
|
ContextValueString: (15 * time.Second).String(),
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 15*time.Second, c.Duration("test"))
|
expect(t, 15*time.Second, c.Duration("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 15*time.Second, c.Duration("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: (15 * time.Second).String(),
|
EnvVarValue: (15 * time.Second).String(),
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 15*time.Second, c.Duration("test"))
|
expect(t, 15*time.Second, c.Duration("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 15*time.Second, c.Duration("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 1.3, c.Float64("test"))
|
expect(t, 1.3, c.Float64("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 1.3, c.Float64("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodSetNegativeValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: -1.3,
|
MapValue: -1.3,
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, -1.3, c.Float64("test"))
|
expect(t, -1.3, c.Float64("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, -1.3, c.Float64("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodSetNegativeValueNotSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodSetNegativeValueNotSet(t *testing.T) {
|
||||||
@ -331,24 +451,32 @@ func TestFloat64ApplyInputSourceMethodSetNegativeValueNotSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
ContextValueString: fmt.Sprintf("%v", 1.4),
|
ContextValueString: fmt.Sprintf("%v", 1.4),
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 1.4, c.Float64("test"))
|
expect(t, 1.4, c.Float64("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 1.4, c.Float64("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
tis := testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test", EnvVars: []string{"TEST"}}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
EnvVarValue: fmt.Sprintf("%v", 1.4),
|
EnvVarValue: fmt.Sprintf("%v", 1.4),
|
||||||
})
|
}
|
||||||
|
c := runTest(t, tis)
|
||||||
expect(t, 1.4, c.Float64("test"))
|
expect(t, 1.4, c.Float64("test"))
|
||||||
|
|
||||||
|
c = runRacyTest(t, tis)
|
||||||
|
refute(t, 1.4, c.Float64("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
||||||
@ -376,6 +504,19 @@ func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runRacyTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
||||||
|
set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError)
|
||||||
|
c := cli.NewContext(nil, set, nil)
|
||||||
|
_ = test.Flag.ApplyInputSourceValue(c, &racyInputSource{
|
||||||
|
MapInputSource: &MapInputSource{
|
||||||
|
file: test.SourcePath,
|
||||||
|
valueMap: map[interface{}]interface{}{test.FlagName: test.MapValue},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
type Parser [2]string
|
type Parser [2]string
|
||||||
|
|
||||||
func (p *Parser) Set(value string) error {
|
func (p *Parser) Set(value string) error {
|
||||||
@ -393,3 +534,5 @@ func (p *Parser) Set(value string) error {
|
|||||||
func (p *Parser) String() string {
|
func (p *Parser) String() string {
|
||||||
return fmt.Sprintf("%s,%s", p[0], p[1])
|
return fmt.Sprintf("%s,%s", p[0], p[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type bogus [1]uint
|
||||||
|
@ -22,7 +22,10 @@ func expect(t *testing.T, a interface{}, b interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func refute(t *testing.T, a interface{}, b interface{}) {
|
func refute(t *testing.T, a interface{}, b interface{}) {
|
||||||
if a == b {
|
_, fn, line, _ := runtime.Caller(1)
|
||||||
t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
fn = strings.Replace(fn, wd+"/", "", -1)
|
||||||
|
|
||||||
|
if reflect.DeepEqual(a, b) {
|
||||||
|
t.Errorf("(%s:%d) Did not expect %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user