Skip to content

Commit

Permalink
Add test cases for json int64/uint64 slices
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Apr 28, 2022
1 parent 87ce263 commit b67ccef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
2 changes: 0 additions & 2 deletions altsrc/flag.go
Expand Up @@ -2,7 +2,6 @@ package altsrc

import (
"fmt"
"log"
"path/filepath"
"strconv"
"syscall"
Expand Down Expand Up @@ -125,7 +124,6 @@ func (f *Int64SliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourc
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.Int64SliceFlag.Name) {
value, err := isc.Int64Slice(f.Int64SliceFlag.Name)
if err != nil {
log.Print(err)
return err
}
if value != nil {
Expand Down
41 changes: 41 additions & 0 deletions altsrc/json_command_test.go
Expand Up @@ -12,6 +12,12 @@ import (
const (
fileName = "current.json"
simpleJSON = `{"test": 15}`
sliceJSON = `
{
"test1" : [214767949494, 23333495956],
"test2" : [1,2,3,-566]
}
`
nestedJSON = `{"top": {"test": 15}}`
)

Expand Down Expand Up @@ -46,6 +52,41 @@ func TestCommandJSONFileTest(t *testing.T) {
expect(t, err, nil)
}

func TestCommandJSONFileTestSlices(t *testing.T) {
cleanup := writeTempFile(t, fileName, sliceJSON)
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 {
val1 := c.Uint64Slice("test1")
expect(t, val1, []uint64{214767949494, 23333495956})

val2 := c.Int64Slice("test2")
expect(t, val2, []int64{1, 2, 3, -566})
return nil
},
Flags: []cli.Flag{
NewUint64SliceFlag(&cli.Uint64SliceFlag{Name: "test1"}),
NewInt64SliceFlag(&cli.Int64SliceFlag{Name: "test2"}),
&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()
Expand Down
4 changes: 4 additions & 0 deletions altsrc/json_source_context.go
Expand Up @@ -179,6 +179,8 @@ func (x *jsonSource) Int64Slice(name string) ([]int64, error) {
for _, s := range v {
if i2, ok := s.(int64); ok {
c = append(c, i2)
} else if f2, ok := s.(float64); ok {
c = append(c, int64(f2))
} else {
return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name)
}
Expand Down Expand Up @@ -206,6 +208,8 @@ func (x *jsonSource) Uint64Slice(name string) ([]uint64, error) {
for _, s := range v {
if i2, ok := s.(uint64); ok {
c = append(c, i2)
} else if f2, ok := s.(float64); ok {
c = append(c, uint64(f2))
} else {
return c, fmt.Errorf("unexpected item type %T in %T for %q", s, c, name)
}
Expand Down

0 comments on commit b67ccef

Please sign in to comment.