diff --git a/docs/contributing/coding-style.md b/docs/contributing/coding-style.md index 2591662eb9..f5418b43b2 100644 --- a/docs/contributing/coding-style.md +++ b/docs/contributing/coding-style.md @@ -264,6 +264,33 @@ func (s *something) SetSignedTok(st string) { } ``` +### Unused Variables + +An unused variable may increase the complexity of the source code, it may confuse the developer hence introduce a new bug. +So please delete the unused variable. + +Generally, the unused variable should be reported during compilation, but in some cases, the compiler may not report an error. +This is an example of the unused variable declaration that does not cause a compilation error. + +```golang +// In this case, this example are not using `port` field, but dose not cause a compilation error. +// So please delete `port` field of `server`. + +type server struct { + addr string + port int // you have to delete this field. +} + +// The `port` field of `server` is not used. +srv := &server { + addr: "192.168.33.10:1234", +} + +if err := srv.Run(); err != nil { + log.Fatal(err) +} +``` + ### Error handling All errors should define in [internal/errors package](https://github.com/vdaas/vald/blob/master/internal/errors). All errors should be start with `Err` prefix, and all errors should be handle if possible. @@ -603,3 +630,41 @@ We do not suggest to modify the generated code other than the `tests` variable, } // generated test code ``` + +1. Unused fields + + By default, the template provides `fields` structure to initialize object of the test target. + But in some cases, not all `fields` are needed, so please delete the unnecessary fields. + For example, the following struct and the corresponding function: + + ```golang + type server struct { + addr string + port int + } + func (s *server) Addr() string { + return s.addr + } + ``` + + And the generated test code is: + ```golang + func Test_server_Addr(t *testing.T) { + type fields struct { + addr string + port int + } + type want struct { + // generated test code + ``` + + Since the `port` variable is not used in this test case, you can delete the `port` definition in the test case. + ```golang + func Test_server_Addr(t *testing.T) { + type fields struct { + addr string + // port int <-- this line should be deleted + } + type want struct { + // generated test code + ``` diff --git a/internal/params/option.go b/internal/params/option.go index dad65c7d62..c3bd8a9787 100644 --- a/internal/params/option.go +++ b/internal/params/option.go @@ -30,36 +30,42 @@ var ( } ) +// WithConfigFilePathKeys returns Option that sets filePath.keys. func WithConfigFilePathKeys(keys ...string) Option { return func(p *parser) { p.filePath.keys = append(p.filePath.keys, keys...) } } +// WithConfigFilePathDefault returns Option that sets filePath.defaultPath. func WithConfigFilePathDefault(path string) Option { return func(p *parser) { p.filePath.defaultPath = path } } +// WithConfigFileDescription returns Option that sets filePath.description. func WithConfigFileDescription(desc string) Option { return func(p *parser) { p.filePath.description = desc } } +// WithVersionKeys returns Option that sets version.keys. func WithVersionKeys(keys ...string) Option { return func(p *parser) { p.version.keys = append(p.version.keys, keys...) } } +// WithVersionFlagDefault returns Option that sets version.defaultFlag. func WithVersionFlagDefault(flag bool) Option { return func(p *parser) { p.version.defaultFlag = flag } } +// WithVersionDescription returns Option that sets version.description. func WithVersionDescription(desc string) Option { return func(p *parser) { p.version.description = desc diff --git a/internal/params/option_test.go b/internal/params/option_test.go index 03f3b809f6..86d7deb35f 100644 --- a/internal/params/option_test.go +++ b/internal/params/option_test.go @@ -18,88 +18,64 @@ package params import ( + "reflect" "testing" + "github.com/vdaas/vald/internal/errors" "go.uber.org/goleak" ) func TestWithConfigFilePathKeys(t *testing.T) { - type T = interface{} + type T = parser type args struct { keys []string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when keys is `key1`", + args: args{ + keys: []string{ + "key1", + }, + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "key1", + }, + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -107,112 +83,66 @@ func TestWithConfigFilePathKeys(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFilePathKeys(test.args.keys...) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFilePathKeys(test.args.keys...) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFilePathKeys(test.args.keys...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithConfigFilePathDefault(t *testing.T) { - type T = interface{} + type T = parser type args struct { path string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - path: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - path: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when path is `path`", + args: args{ + path: "path", + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + defaultPath: "path", + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -220,112 +150,66 @@ func TestWithConfigFilePathDefault(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFilePathDefault(test.args.path) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFilePathDefault(test.args.path) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFilePathDefault(test.args.path) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithConfigFileDescription(t *testing.T) { - type T = interface{} + type T = parser type args struct { desc string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when desc is `desc`", + args: args{ + desc: "desc", + }, + want: want{ + obj: &T{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + description: "desc", + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -333,112 +217,70 @@ func TestWithConfigFileDescription(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithConfigFileDescription(test.args.desc) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithConfigFileDescription(test.args.desc) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithConfigFileDescription(test.args.desc) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionKeys(t *testing.T) { - type T = interface{} + type T = parser type args struct { keys []string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - keys: nil, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when keys is `key1`", + args: args{ + keys: []string{ + "key1", + }, + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "key1", + }, + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -446,112 +288,66 @@ func TestWithVersionKeys(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionKeys(test.args.keys...) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionKeys(test.args.keys...) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionKeys(test.args.keys...) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionFlagDefault(t *testing.T) { - type T = interface{} + type T = parser type args struct { flag bool } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - flag: false, - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - flag: false, - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when flag is true", + args: args{ + flag: true, + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + defaultFlag: true, + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -559,112 +355,66 @@ func TestWithVersionFlagDefault(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionFlagDefault(test.args.flag) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionFlagDefault(test.args.flag) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionFlagDefault(test.args.flag) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } func TestWithVersionDescription(t *testing.T) { - type T = interface{} + type T = parser type args struct { desc string } type want struct { obj *T - // Uncomment this line if the option returns an error, otherwise delete it - // err error } type test struct { - name string - args args - want want - // Use the first line if the option returns an error. otherwise use the second line - // checkFunc func(want, *T, error) error - // checkFunc func(want, *T) error + name string + args args + want want + checkFunc func(want, *T) error beforeFunc func(args) afterFunc func(args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T, err error) error { - if !errors.Is(err, w.err) { - return errors.Errorf("got error = %v, want %v", err, w.err) - } - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.obj) - } - return nil - } - */ - - // Uncomment this block if the option do not returns an error, otherwise delete it - /* - defaultCheckFunc := func(w want, obj *T) error { - if !reflect.DeepEqual(obj, w.obj) { - return errors.Errorf("got = %v, want %v", obj, w.c) - } - return nil - } - */ + defaultCheckFunc := func(w want, obj *T) error { + if !reflect.DeepEqual(obj, w.obj) { + return errors.Errorf("got = %v, want %v", obj, w.obj) + } + return nil + } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - desc: "", - }, - want: want { - obj: new(T), - }, - } - }(), - */ + { + name: "set success when desc is true", + args: args{ + desc: "desc", + }, + want: want{ + obj: &T{ + version: struct { + keys []string + defaultFlag bool + description string + }{ + description: "desc", + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -672,31 +422,15 @@ func TestWithVersionDescription(t *testing.T) { defer test.afterFunc(test.args) } - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - - got := WithVersionDescription(test.args.desc) - obj := new(T) - if err := test.checkFunc(test.want, obj, got(obj)); err != nil { - tt.Errorf("error = %v", err) - } - */ - - // Uncomment this block if the option returns an error, otherwise delete it - /* - if test.checkFunc == nil { - test.checkFunc = defaultCheckFunc - } - got := WithVersionDescription(test.args.desc) - obj := new(T) - got(obj) - if err := test.checkFunc(tt.want, obj); err != nil { - tt.Errorf("error = %v", err) - } - */ + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + got := WithVersionDescription(test.args.desc) + obj := new(T) + got(obj) + if err := test.checkFunc(test.want, obj); err != nil { + tt.Errorf("error = %v", err) + } }) } } diff --git a/internal/params/params.go b/internal/params/params.go index d568e7927f..d58776c227 100644 --- a/internal/params/params.go +++ b/internal/params/params.go @@ -25,6 +25,7 @@ import ( "github.com/vdaas/vald/internal/errors" ) +// Data is an interface to get the configuration path and flag. type Data interface { ConfigFilePath() string ShowVersion() bool @@ -48,6 +49,7 @@ type parser struct { } } +// New returns parser object. func New(opts ...Option) *parser { p := new(parser) for _, opt := range append(defaultOpts, opts...) { @@ -56,6 +58,7 @@ func New(opts ...Option) *parser { return p } +// Parse parses command-line argument and returns parsed data and whether there is a help option or not and error. func (p *parser) Parse() (Data, bool, error) { f := flag.NewFlagSet(filepath.Base(os.Args[0]), flag.ContinueOnError) @@ -94,10 +97,12 @@ func (p *parser) Parse() (Data, bool, error) { return d, false, nil } +// ConfigFilePath returns configFilePath. func (d *data) ConfigFilePath() string { return d.configFilePath } +// ShowVersion returns showVersion. func (d *data) ShowVersion() bool { return d.showVersion } diff --git a/internal/params/params_test.go b/internal/params/params_test.go index 50b3e4ba94..ee54b96e15 100644 --- a/internal/params/params_test.go +++ b/internal/params/params_test.go @@ -18,7 +18,10 @@ package params import ( + stderrs "errors" + "os" "reflect" + "syscall" "testing" "github.com/vdaas/vald/internal/errors" @@ -48,36 +51,87 @@ func TestNew(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ + { + name: "returns *parser when opts is nil", + want: want{ + want: &parser{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "f", + "file", + "c", + "config", + }, + defaultPath: "/etc/server/config.yaml", + description: "config file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "v", + "ver", + "version", + }, + defaultFlag: false, + description: "show server version", + }, + }, + }, + }, - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - args: args { - opts: nil, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns *parser when opts is not nil", + args: args{ + opts: []Option{ + WithConfigFilePathKeys("t", "test"), + }, + }, + want: want{ + want: &parser{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "f", + "file", + "c", + "config", + "t", + "test", + }, + defaultPath: "/etc/server/config.yaml", + description: "config file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "v", + "ver", + "version", + }, + defaultFlag: false, + description: "show server version", + }, + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc(test.args) } @@ -92,7 +146,6 @@ func TestNew(t *testing.T) { if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } } @@ -136,38 +189,107 @@ func Test_parser_Parse(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - filePath: struct{keys []string; defaultPath string; description string}{}, - version: struct{keys []string; defaultFlag bool; description string}{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ + { + name: "returns (d, false, nil) when parse succeed", + fields: fields{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "path", "p", + }, + defaultPath: "./params.go", + description: "sets file path", + }, + version: struct { + keys []string + defaultFlag bool + description string + }{ + keys: []string{ + "version", "v", + }, + defaultFlag: true, + description: "show version", + }, + }, + beforeFunc: func() { + os.Args = []string{ + "test", "--path=./params.go", "--version=false", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want: &data{ + configFilePath: "./params.go", + showVersion: false, + }, + }, + }, + + { + name: "returns (nil, true, nil) When parse fails but the help option is set", + beforeFunc: func() { + os.Args = []string{ + "test", "--help", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: true, + }, + }, + + { + name: "returns (nil, true, nil) When parse fails but the help option is not set", + beforeFunc: func() { + os.Args = []string{ + "test", "--name", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: false, + err: errors.ErrArgumentParseFailed(stderrs.New("flag provided but not defined: -name")), + }, + }, - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - filePath: struct{keys []string; defaultPath string; description string}{}, - version: struct{keys []string; defaultFlag bool; description string}{}, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns (nil, true, error) When the configFilePath option is set but file dose not exist", + fields: fields{ + filePath: struct { + keys []string + defaultPath string + description string + }{ + keys: []string{ + "path", "p", + }, + description: "sets file path", + }, + }, + beforeFunc: func() { + os.Args = []string{ + "test", "--path=config.yml", + } + }, + afterFunc: func() { os.Args = nil }, + want: want{ + want1: true, + err: &os.PathError{ + Op: "stat", + Path: "config.yml", + Err: syscall.Errno(0x2), + }, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc() } @@ -186,7 +308,6 @@ func Test_parser_Parse(t *testing.T) { if err := test.checkFunc(test.want, got, got1, err); err != nil { tt.Errorf("error = %v", err) } - }) } } @@ -194,7 +315,6 @@ func Test_parser_Parse(t *testing.T) { func Test_data_ConfigFilePath(t *testing.T) { type fields struct { configFilePath string - showVersion bool } type want struct { want string @@ -214,38 +334,20 @@ func Test_data_ConfigFilePath(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns `./path` when d.configFilePath is `./path`", + fields: fields{ + configFilePath: "./path", + }, + want: want{ + want: "./path", + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc() } @@ -257,22 +359,19 @@ func Test_data_ConfigFilePath(t *testing.T) { } d := &data{ configFilePath: test.fields.configFilePath, - showVersion: test.fields.showVersion, } got := d.ConfigFilePath() if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } } func Test_data_ShowVersion(t *testing.T) { type fields struct { - configFilePath string - showVersion bool + showVersion bool } type want struct { want bool @@ -292,38 +391,20 @@ func Test_data_ShowVersion(t *testing.T) { return nil } tests := []test{ - // TODO test cases - /* - { - name: "test_case_1", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - }, - */ - - // TODO test cases - /* - func() test { - return test { - name: "test_case_2", - fields: fields { - configFilePath: "", - showVersion: false, - }, - want: want{}, - checkFunc: defaultCheckFunc, - } - }(), - */ + { + name: "returns true when d.showVersion is true", + fields: fields{ + showVersion: true, + }, + want: want{ + want: true, + }, + }, } for _, test := range tests { t.Run(test.name, func(tt *testing.T) { - defer goleak.VerifyNone(t) + defer goleak.VerifyNone(tt) if test.beforeFunc != nil { test.beforeFunc() } @@ -334,15 +415,13 @@ func Test_data_ShowVersion(t *testing.T) { test.checkFunc = defaultCheckFunc } d := &data{ - configFilePath: test.fields.configFilePath, - showVersion: test.fields.showVersion, + showVersion: test.fields.showVersion, } got := d.ShowVersion() if err := test.checkFunc(test.want, got); err != nil { tt.Errorf("error = %v", err) } - }) } }