Skip to content

Commit

Permalink
rollback package name from altsrc to altsrc_test
Browse files Browse the repository at this point in the history
  • Loading branch information
jack.jin committed Feb 23, 2023
1 parent a105e80 commit f8fe5e8
Showing 1 changed file with 20 additions and 54 deletions.
74 changes: 20 additions & 54 deletions altsrc/yaml_file_loader_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package altsrc
package altsrc_test

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"reflect"
"testing"
"time"

"github.com/urfave/cli/v2"
// "github.com/urfave/cli/v2/altsrc"
"github.com/urfave/cli/v2/altsrc"
)

func ExampleApp_Run_yamlFileLoaderDuration() {
Expand All @@ -36,11 +35,11 @@ func ExampleApp_Run_yamlFileLoaderDuration() {
} else if !context.IsSet(configFlag) && !fileExists(configFile) {
return nil
}
inputSource, err := NewYamlSourceFromFile(configFile)
inputSource, err := altsrc.NewYamlSourceFromFile(configFile)
if err != nil {
return err
}
return ApplyInputSourceValues(context, inputSource, flags)
return altsrc.ApplyInputSourceValues(context, inputSource, flags)
}
}

Expand All @@ -53,7 +52,7 @@ func ExampleApp_Run_yamlFileLoaderDuration() {
DefaultText: "../testdata/empty.yml",
Usage: "config file",
},
NewDurationFlag(
altsrc.NewDurationFlag(
&cli.DurationFlag{
Name: "keepalive-interval",
Aliases: []string{"k"},
Expand Down Expand Up @@ -96,11 +95,11 @@ func TestYamlFileInt64Slice(t *testing.T) {
defer os.Remove("current.yaml")

testFlag := []cli.Flag{
&StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&Int64SliceFlag{Int64SliceFlag: &cli.Int64SliceFlag{Name: "top.test"}},
&altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&altsrc.Int64SliceFlag{Int64SliceFlag: &cli.Int64SliceFlag{Name: "top.test"}},
}
app := &cli.App{}
app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf"))
app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf"))
app.Action = func(c *cli.Context) error { return nil }
app.Flags = append(app.Flags, testFlag...)

Expand All @@ -116,11 +115,11 @@ func TestYamlFileStringSlice(t *testing.T) {
defer os.Remove("current.yaml")

testFlag := []cli.Flag{
&StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&StringSliceFlag{StringSliceFlag: &cli.StringSliceFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}},
&altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&altsrc.StringSliceFlag{StringSliceFlag: &cli.StringSliceFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}},
}
app := &cli.App{}
app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf"))
app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf"))
app.Action = func(c *cli.Context) error {
if c.IsSet("top.test") {
return nil
Expand Down Expand Up @@ -185,12 +184,12 @@ func TestYamlFileUint64(t *testing.T) {
defer os.Remove("current.yaml")

testFlag := []cli.Flag{
&StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&Uint64Flag{Uint64Flag: &cli.Uint64Flag{Name: test.name}},
&altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&altsrc.Uint64Flag{Uint64Flag: &cli.Uint64Flag{Name: test.name}},
}
app := &cli.App{}
app.Flags = append(app.Flags, testFlag...)
app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf"))
app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf"))

appCmd := []string{"testApp", "--conf", "current.yaml"}
err := app.Run(appCmd)
Expand Down Expand Up @@ -249,12 +248,12 @@ func TestYamlFileUint(t *testing.T) {
defer os.Remove("current.yaml")

testFlag := []cli.Flag{
&StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&UintFlag{UintFlag: &cli.UintFlag{Name: test.name}},
&altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&altsrc.UintFlag{UintFlag: &cli.UintFlag{Name: test.name}},
}
app := &cli.App{}
app.Flags = append(app.Flags, testFlag...)
app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf"))
app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf"))

appCmd := []string{"testApp", "--conf", "current.yaml"}
err := app.Run(appCmd)
Expand Down Expand Up @@ -313,12 +312,12 @@ func TestYamlFileInt64(t *testing.T) {
defer os.Remove("current.yaml")

testFlag := []cli.Flag{
&StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&Int64Flag{Int64Flag: &cli.Int64Flag{Name: test.name}},
&altsrc.StringFlag{StringFlag: &cli.StringFlag{Name: "conf"}},
&altsrc.Int64Flag{Int64Flag: &cli.Int64Flag{Name: test.name}},
}
app := &cli.App{}
app.Flags = append(app.Flags, testFlag...)
app.Before = InitInputSourceWithContext(testFlag, NewYamlSourceFromFlagFunc("conf"))
app.Before = altsrc.InitInputSourceWithContext(testFlag, altsrc.NewYamlSourceFromFlagFunc("conf"))

appCmd := []string{"testApp", "--conf", "current.yaml"}
err := app.Run(appCmd)
Expand All @@ -327,36 +326,3 @@ func TestYamlFileInt64(t *testing.T) {
}
}
}

func TestCastToUint64(t *testing.T) {
tests := []struct {
value interface{}
expect bool
}{
{int64(100), true},
{uint(100), true},
}

for _, test := range tests {
v, isType := castToUint64(test.value)
if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 {
t.Fatalf("expect %v, but %v", test.expect, isType)
}
}
}

func TestCastToUint(t *testing.T) {
tests := []struct {
value interface{}
expect bool
}{
{int64(100), true},
}

for _, test := range tests {
v, isType := castToUint(test.value)
if isType != test.expect && reflect.TypeOf(v).Kind() != reflect.Uint64 {
t.Fatalf("expect %v, but %v", test.expect, isType)
}
}
}

0 comments on commit f8fe5e8

Please sign in to comment.