1
0
Fork 0
mirror of https://github.com/Luzifer/rconfig.git synced 2024-11-08 16:00:10 +00:00
rconfig/general_test.go

87 lines
1.7 KiB
Go
Raw Permalink Normal View History

2015-09-08 20:07:40 +00:00
package rconfig
import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2015-09-08 20:07:40 +00:00
)
func TestGeneralExecution(t *testing.T) {
type test struct {
2015-09-08 20:07:40 +00:00
Test string `default:"foo" env:"shell" flag:"shell" description:"Test"`
Test2 string `default:"blub" env:"testvar" flag:"testvar,t" description:"Test"`
DefaultFlag string `default:"goo"`
SadFlag string
}
var (
args []string
cfg test
2015-09-08 20:07:40 +00:00
)
exec := func(desc string, tests [][2]interface{}) {
require.NoError(t, parse(&cfg, args))
2015-09-08 20:07:40 +00:00
for _, test := range tests {
assert.Equal(t, test[1], reflect.ValueOf(test[0]).Elem().Interface(), desc)
}
}
2015-09-08 20:07:40 +00:00
cfg = test{}
args = []string{
"--shell=test23",
"-t", "bla",
}
exec("defined arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
2015-09-08 20:07:40 +00:00
})
cfg = test{}
args = []string{}
exec("no arguments", [][2]interface{}{
{&cfg.Test, "foo"},
2015-09-08 20:07:40 +00:00
})
cfg = test{}
args = []string{}
t.Setenv("shell", "test546")
exec("no arguments and set env", [][2]interface{}{
{&cfg.Test, "test546"},
2015-09-08 20:07:40 +00:00
})
require.NoError(t, os.Unsetenv("shell"))
2015-09-08 20:07:40 +00:00
cfg = test{}
args = []string{
"--shell=test23",
"-t", "bla",
"positional1", "positional2",
}
exec("additional arguments", [][2]interface{}{
{&cfg.Test, "test23"},
{&cfg.Test2, "bla"},
{&cfg.SadFlag, ""},
{&cfg.DefaultFlag, "goo"},
})
assert.Equal(t, []string{"positional1", "positional2"}, Args())
}
func TestValidationIntegration(t *testing.T) {
type tValidated struct {
Test string `flag:"test" default:"" validate:"nonzero"`
}
var (
cfgValidated = tValidated{}
args = []string{}
)
assert.Error(t, parseAndValidate(&cfgValidated, args))
}