@@ -8,67 +8,35 @@ import (
88 "github.com/openshift-knative/deviate/internal/cmd"
99 "github.com/openshift-knative/deviate/pkg/metadata"
1010 "github.com/spf13/cobra"
11+ "github.com/wavesoftware/go-commandline"
1112 "gotest.tools/v3/assert"
1213)
1314
14- const notSetRetCode = - 1 * (2 ^ 63 )
15-
1615func TestMainFunc (t * testing.T ) {
17- o := output {}
18- a := args {[]string {"--version" }}
19- code := withCapturedRetCode (func () {
20- withOptions (func () {
21- main ()
22- }, o .configure , a .configure )
23- })
24-
25- assert .Equal (t , code , 0 )
16+ var o bytes.Buffer
17+ var retcode * int
18+ withOptions (func () {
19+ main ()
20+ },
21+ commandline .WithCommand (func (cmd * cobra.Command ) {
22+ cmd .SetArgs ([]string {"--version" })
23+ cmd .SetOut (& o )
24+ }),
25+ commandline .WithExit (func (code int ) {
26+ retcode = & code
27+ }),
28+ )
29+
30+ assert .Equal (t , retcode , (* int )(nil ))
2631 assert .Equal (t , o .String (), fmt .Sprintf ("%s version %s\n " ,
2732 metadata .Name , metadata .Version ))
2833}
2934
30- type args struct {
31- of []string
32- }
33-
34- func (a args ) configure (root * cobra.Command ) {
35- root .SetArgs (a .of )
36- }
37-
38- type output struct {
39- * bytes.Buffer
40- }
41-
42- func (o * output ) configure (root * cobra.Command ) {
43- root .SetOut (o .buff ())
44- root .SetErr (o .buff ())
45- }
46-
47- func (o * output ) buff () * bytes.Buffer {
48- if o .Buffer == nil {
49- o .Buffer = new (bytes.Buffer )
50- }
51- return o .Buffer
52- }
53-
54- func withCapturedRetCode (fn func ()) int {
55- retcode := notSetRetCode
56- old := exitFunc
57- exitFunc = func (code int ) {
58- retcode = code
59- }
60- defer func () {
61- exitFunc = old
62- }()
63- fn ()
64- return retcode
65- }
66-
67- func withOptions (fn func (), newOpts ... cmd.Option ) {
68- old := opts
69- opts = newOpts
35+ func withOptions (fn func (), newOpts ... commandline.Option ) {
36+ old := cmd .Options
37+ cmd .Options = newOpts
7038 defer func () {
71- opts = old
39+ cmd . Options = old
7240 }()
7341 fn ()
7442}
0 commit comments