2015-09-08 20:07:40 +00:00
|
|
|
package rconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-09-06 16:43:24 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-09-08 20:07:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-06 16:43:24 +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
|
2021-09-06 16:43:24 +00:00
|
|
|
cfg test
|
2015-09-08 20:07:40 +00:00
|
|
|
)
|
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
exec := func(desc string, tests [][2]interface{}) {
|
|
|
|
if err := parse(&cfg, args); err != nil {
|
|
|
|
t.Fatalf("Parsing options caused error: %s", err)
|
|
|
|
}
|
2015-09-08 20:07:40 +00:00
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
if !reflect.DeepEqual(reflect.ValueOf(test[0]).Elem().Interface(), test[1]) {
|
|
|
|
t.Errorf("%q expected value does not match: %#v != %#v", desc, test[0], test[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 20:07:40 +00:00
|
|
|
|
2021-09-06 16:43:24 +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
|
|
|
})
|
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
cfg = test{}
|
|
|
|
args = []string{}
|
|
|
|
exec("no arguments", [][2]interface{}{
|
|
|
|
{&cfg.Test, "foo"},
|
2015-09-08 20:07:40 +00:00
|
|
|
})
|
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
cfg = test{}
|
|
|
|
args = []string{}
|
|
|
|
os.Setenv("shell", "test546")
|
|
|
|
exec("no arguments and set env", [][2]interface{}{
|
|
|
|
{&cfg.Test, "test546"},
|
2015-09-08 20:07:40 +00:00
|
|
|
})
|
2021-09-06 16:43:24 +00:00
|
|
|
os.Unsetenv("shell")
|
2015-09-08 20:07:40 +00:00
|
|
|
|
2021-09-06 16:43:24 +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"},
|
2015-12-14 01:04:12 +00:00
|
|
|
})
|
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
if !reflect.DeepEqual(Args(), []string{"positional1", "positional2"}) {
|
|
|
|
t.Errorf("expected positional arguments to match")
|
|
|
|
}
|
|
|
|
}
|
2017-06-19 12:22:33 +00:00
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
func TestValidationIntegration(t *testing.T) {
|
|
|
|
type tValidated struct {
|
|
|
|
Test string `flag:"test" default:"" validate:"nonzero"`
|
|
|
|
}
|
2017-06-19 12:22:33 +00:00
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
var (
|
|
|
|
cfgValidated = tValidated{}
|
|
|
|
args = []string{}
|
|
|
|
)
|
2017-06-19 12:22:33 +00:00
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
if err := parseAndValidate(&cfgValidated, args); err == nil {
|
|
|
|
t.Errorf("Expected error, got none")
|
|
|
|
}
|
|
|
|
}
|