2015-09-08 20:07:40 +00:00
|
|
|
package rconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-09-06 16:43:24 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2023-12-20 11:13:59 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
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{}) {
|
2023-12-20 11:13:59 +00:00
|
|
|
require.NoError(t, parse(&cfg, args))
|
2015-09-08 20:07:40 +00:00
|
|
|
|
2021-09-06 16:43:24 +00:00
|
|
|
for _, test := range tests {
|
2023-12-20 11:13:59 +00:00
|
|
|
assert.Equal(t, test[1], reflect.ValueOf(test[0]).Elem().Interface(), desc)
|
2021-09-06 16:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
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{}
|
2024-08-18 10:09:36 +00:00
|
|
|
t.Setenv("shell", "test546")
|
2021-09-06 16:43:24 +00:00
|
|
|
exec("no arguments and set env", [][2]interface{}{
|
|
|
|
{&cfg.Test, "test546"},
|
2015-09-08 20:07:40 +00:00
|
|
|
})
|
2023-12-20 11:13:59 +00:00
|
|
|
require.NoError(t, 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
|
|
|
})
|
|
|
|
|
2023-12-20 11:13:59 +00:00
|
|
|
assert.Equal(t, []string{"positional1", "positional2"}, Args())
|
2021-09-06 16:43:24 +00:00
|
|
|
}
|
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
|
|
|
|
2023-12-20 11:13:59 +00:00
|
|
|
assert.Error(t, parseAndValidate(&cfgValidated, args))
|
2021-09-06 16:43:24 +00:00
|
|
|
}
|