Skip to content

Commit

Permalink
Fix(1199). Allow -ve values for int, float & duration
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Apr 3, 2021
1 parent 13ded1e commit 9b98846
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
20 changes: 7 additions & 13 deletions altsrc/flag.go
Expand Up @@ -197,10 +197,8 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon
if err != nil {
return err
}
if value > 0 {
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
}
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
}
}
}
Expand All @@ -215,10 +213,8 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
if err != nil {
return err
}
if value > 0 {
for _, name := range f.Names() {
_ = f.set.Set(name, value.String())
}
for _, name := range f.Names() {
_ = f.set.Set(name, value.String())
}
}
}
Expand All @@ -233,11 +229,9 @@ func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourc
if err != nil {
return err
}
if value > 0 {
floatStr := float64ToString(value)
for _, name := range f.Names() {
_ = f.set.Set(name, floatStr)
}
floatStr := float64ToString(value)
for _, name := range f.Names() {
_ = f.set.Set(name, floatStr)
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions altsrc/flag_test.go
Expand Up @@ -234,6 +234,15 @@ func TestIntApplyInputSourceMethodSet(t *testing.T) {
expect(t, 15, c.Int("test"))
}

func TestIntApplyInputSourceMethodSetNegativeValue(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
FlagName: "test",
MapValue: -1,
})
expect(t, -1, c.Int("test"))
}

func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
Expand Down Expand Up @@ -264,6 +273,15 @@ func TestDurationApplyInputSourceMethodSet(t *testing.T) {
expect(t, 30*time.Second, c.Duration("test"))
}

func TestDurationApplyInputSourceMethodSetNegativeValue(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
FlagName: "test",
MapValue: -30 * time.Second,
})
expect(t, -30*time.Second, c.Duration("test"))
}

func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
Expand Down Expand Up @@ -294,6 +312,15 @@ func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
expect(t, 1.3, c.Float64("test"))
}

func TestFloat64ApplyInputSourceMethodSetNegativeValue(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
FlagName: "test",
MapValue: -1.3,
})
expect(t, -1.3, c.Float64("test"))
}

func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
c := runTest(t, testApplyInputSource{
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
Expand Down

0 comments on commit 9b98846

Please sign in to comment.