From 8b2f2ea660da6ddc5526cd1636445601c0a01d3d Mon Sep 17 00:00:00 2001 From: andrey Date: Sat, 3 Jan 2026 17:08:45 +0300 Subject: [PATCH] add test arg bind --- provider/arg/provider.go | 17 ++++---- provider/arg/provider_test.go | 73 ++++++++++++++++++++++++++++++----- variable.go | 13 ++++++- 3 files changed, 85 insertions(+), 18 deletions(-) diff --git a/provider/arg/provider.go b/provider/arg/provider.go index 9d34774..3e91338 100644 --- a/provider/arg/provider.go +++ b/provider/arg/provider.go @@ -19,6 +19,7 @@ const ( dash = `-` ) +// Deprecated: use WithArgs. func WithSkip(skip int) func(*Argv) { return func(ar *Argv) { res := 2 @@ -34,24 +35,27 @@ func WithSkip(skip int) func(*Argv) { res = 1 } - ar.skip = res + ar.args = os.Args[res:] + } +} + +func WithArgs(args []string) func(*Argv) { + return func(a *Argv) { + a.args = args } } func New(opts ...func(*Argv)) *Argv { arg := &Argv{ - args: nil, + args: os.Args[1:], pos: 0, Map: memory.Map{}, - skip: 1, } for _, opt := range opts { opt(arg) } - arg.args = os.Args[arg.skip:] - return arg } @@ -60,7 +64,6 @@ type Argv struct { args []string pos uint64 - skip int } func (i *Argv) Value(ctx context.Context, key ...string) (config.Value, error) { @@ -164,7 +167,7 @@ func (i *Argv) parseShortOption(arg string, def config.Variables) error { var value string if len(name) > 1 { - name, value = arg[0:1], arg[1:] + name, value = arg[0:1], strings.TrimSpace(arg[1:]) } opt, err := def.ByParam(option.HasShort(name)) diff --git a/provider/arg/provider_test.go b/provider/arg/provider_test.go index 179728c..7e63837 100644 --- a/provider/arg/provider_test.go +++ b/provider/arg/provider_test.go @@ -1,27 +1,26 @@ package arg_test import ( + "context" "fmt" - "os" "strings" "testing" "time" + "gitoa.ru/go-4devs/config" + "gitoa.ru/go-4devs/config/definition" + "gitoa.ru/go-4devs/config/definition/group" + "gitoa.ru/go-4devs/config/definition/option" + "gitoa.ru/go-4devs/config/definition/proto" "gitoa.ru/go-4devs/config/provider/arg" "gitoa.ru/go-4devs/config/test" + "gitoa.ru/go-4devs/config/test/require" ) func TestProvider(t *testing.T) { t.Parallel() - args := os.Args - - defer func() { - os.Args = args - }() - - os.Args = []string{ - "main.go", + args := []string{ "--listen=8080", "--config=config.hcl", "--url=http://4devs.io", @@ -44,11 +43,65 @@ func TestProvider(t *testing.T) { }, &[]time.Time{}, "end-after"), } - prov := arg.New() + prov := arg.New(arg.WithArgs(args)) test.Run(t, prov, read) } +func TestProviderBind(t *testing.T) { + t.Parallel() + + args := []string{ + "-p 8080", + "--config=config.hcl", + "-u http://4devs.io", + "--url=https://4devs.io", + "-t 1m", + "--timeout=1h", + "--start-at=2010-01-02T15:04:05Z", + "--end-after=2009-01-02T15:04:05Z", + "--end-after=2008-01-02T15:04:05+03:00", + } + + read := []test.Read{ + test.NewRead(8080, "listen"), + test.NewRead(test.Time("2010-01-02T15:04:05Z"), "start-at"), + test.NewReadUnmarshal(&[]string{"http://4devs.io", "https://4devs.io"}, &[]string{}, "url"), + test.NewReadUnmarshal(&[]Duration{{time.Minute}, {time.Hour}}, &[]Duration{}, "timeout"), + test.NewReadUnmarshal(&[]time.Time{ + test.Time("2009-01-02T15:04:05Z"), + test.Time("2008-01-02T15:04:05+03:00"), + }, &[]time.Time{}, "end", "after"), + } + + ctx := context.Background() + prov := arg.New(arg.WithArgs(args)) + require.NoError(t, prov.Bind(ctx, testVariables(t))) + + test.Run(t, prov, read) +} + +func testVariables(t *testing.T) config.Variables { + t.Helper() + + def := definition.New() + def.Add( + option.Int("listen", "listen", option.Short('p')), + option.String("config", "config"), + option.String("url", "url", option.Short('u'), option.Slice), + option.Duration("timeout", "timeout", option.Short('t'), option.Slice), + option.Time("start-at", "start at"), + group.New("end", "end at", + option.Time("after", "after", option.Slice), + proto.New("service", "service after", + option.Time("after", "after"), + ), + ), + ) + + return config.NewVars(def) +} + type Duration struct { time.Duration } diff --git a/variable.go b/variable.go index f4933bb..305e760 100644 --- a/variable.go +++ b/variable.go @@ -8,9 +8,14 @@ import ( ) func NewVar(opt Option) Variable { + names := make([]string, 0, 1) + if name := opt.Name(); name != "" { + names = append(names, name) + } + return Variable{ param: opt, - names: []string{opt.Name()}, + names: names, } } @@ -58,6 +63,12 @@ func newVars(opts ...Option) []Variable { one := NewVar(opt) switch data := opt.(type) { case Group: + if data.Name() == "" { + vars = append(vars, newVars(data.Options()...)...) + + continue + } + vars = append(vars, groupVars(one, data.Options()...)...) default: vars = append(vars, one)