@@ -10,30 +10,14 @@ import (
1010 "strings"
1111 "testing"
1212
13- "github.com/alecthomas/kong"
1413 "github.com/microsoft/go-mssqldb/azuread"
1514 "github.com/microsoft/go-sqlcmd/pkg/sqlcmd"
15+ "github.com/spf13/cobra"
1616 "github.com/stretchr/testify/assert"
17- "github.com/stretchr/testify/require"
1817)
1918
2019const oneRowAffected = "(1 row affected)"
2120
22- func newKong (t * testing.T , cli interface {}, options ... kong.Option ) * kong.Kong {
23- t .Helper ()
24- options = append ([]kong.Option {
25- kong .Name ("test" ),
26- kong .NoDefaultHelp (),
27- kong .Exit (func (int ) {
28- t .Helper ()
29- assert .Fail (t , "unexpected exit()" )
30- }),
31- }, options ... )
32- parser , err := kong .New (cli , options ... )
33- require .NoError (t , err )
34- return parser
35- }
36-
3721func TestValidCommandLineToArgsConversion (t * testing.T ) {
3822 type cmdLineTest struct {
3923 commandLine []string
@@ -90,15 +74,34 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
9074 {[]string {"--version" }, func (args SQLCmdArguments ) bool {
9175 return args .Version
9276 }},
77+ {[]string {}, func (args SQLCmdArguments ) bool {
78+ return args .ScreenWidth == nil
79+ }},
80+ {[]string {"-w" , "10" }, func (args SQLCmdArguments ) bool {
81+ return args .ScreenWidth != nil && * args .ScreenWidth == 10
82+ }},
9383 {[]string {"-s" , "|" , "-w" , "10" , "-W" }, func (args SQLCmdArguments ) bool {
9484 return args .TrimSpaces && args .ColumnSeparator == "|" && * args .ScreenWidth == 10
9585 }},
9686 }
9787
9888 for _ , test := range commands {
9989 arguments := & SQLCmdArguments {}
100- parser := newKong (t , arguments )
101- _ , err := parser .Parse (test .commandLine )
90+ cmd := & cobra.Command {
91+ Use : "testCommand" ,
92+ Short : "A brief description of my command" ,
93+ Long : "A long description of my command" ,
94+ PreRunE : func (cmd * cobra.Command , argss []string ) error {
95+ SetScreenWidthFlag (arguments , cmd )
96+ return arguments .Validate ()
97+ },
98+ Run : func (cmd * cobra.Command , argss []string ) {
99+ // Command logic goes here
100+ },
101+ }
102+ setFlags (cmd , arguments )
103+ cmd .SetArgs (test .commandLine )
104+ err := cmd .Execute ()
102105 msg := ""
103106 if err != nil {
104107 msg = err .Error ()
@@ -116,19 +119,61 @@ func TestInvalidCommandLine(t *testing.T) {
116119 }
117120
118121 commands := []cmdLineTest {
119- {[]string {"-E" , "-U" , "someuser" }, "--use-trusted-connection and --user-name can't be used together" },
120- // the test prefix is a kong artifact https://github.com/alecthomas/kong/issues/221
121- {[]string {"-a" , "100" }, "test: '-a 100': Packet size has to be a number between 512 and 32767." },
122+ // Issue:341 https://github.com/microsoft/go-sqlcmd/issues/341
123+ //{[]string{"-E", "-U", "someuser"}, "--use-trusted-connection and --user-name can't be used together"},
122124 {[]string {"-F" , "what" }, "--format must be one of \" horiz\" ,\" horizontal\" ,\" vert\" ,\" vertical\" but got \" what\" " },
123- {[]string {"-r" , "5" }, `--errors-to-stderr must be one of "-1","0","1" but got '\x05'` },
124- {[]string {"-h-4" }, "test: '-h -4': header value must be either -1 or a value between 1 and 2147483647" },
125- {[]string {"-w" , "6" }, "test: '-w 6': value must be greater than 8 and less than 65536." },
125+ {[]string {"-r" , "5" }, `--errors-to-stderr must be one of "-1","0","1" but got "5"` },
126126 }
127127
128128 for _ , test := range commands {
129129 arguments := & SQLCmdArguments {}
130- parser := newKong (t , arguments )
131- _ , err := parser .Parse (test .commandLine )
130+ cmd := & cobra.Command {
131+ Use : "testCommand" ,
132+ Short : "A brief description of my command" ,
133+ Long : "A long description of my command" ,
134+ PreRunE : func (cmd * cobra.Command , argss []string ) error {
135+ return normalizeFlags (cmd )
136+ },
137+ Run : func (cmd * cobra.Command , argss []string ) {
138+ },
139+ }
140+ setFlags (cmd , arguments )
141+ cmd .SetArgs (test .commandLine )
142+ err := cmd .Execute ()
143+ assert .EqualError (t , err , test .errorMessage , "Command line:%v" , test .commandLine )
144+ }
145+ }
146+
147+ func TestValidateFlags (t * testing.T ) {
148+ type cmdLineTest struct {
149+ commandLine []string
150+ errorMessage string
151+ }
152+
153+ commands := []cmdLineTest {
154+ {[]string {"-a" , "100" }, "'-a 100': Packet size has to be a number between 512 and 32767." },
155+ {[]string {"-h-4" }, "'-h -4': header value must be either -1 or a value between 1 and 2147483647" },
156+ {[]string {"-w" , "6" }, "'-w 6': value must be greater than 8 and less than 65536." },
157+ }
158+
159+ for _ , test := range commands {
160+ arguments := & SQLCmdArguments {}
161+ //var screenWidth *int
162+ cmd := & cobra.Command {
163+ Use : "testCommand" ,
164+ Short : "A brief description of my command" ,
165+ Long : "A long description of my command" ,
166+ PreRunE : func (cmd * cobra.Command , argss []string ) error {
167+ SetScreenWidthFlag (arguments , cmd )
168+ return arguments .Validate ()
169+ },
170+ Run : func (cmd * cobra.Command , argss []string ) {
171+ },
172+ }
173+
174+ setFlags (cmd , arguments )
175+ cmd .SetArgs (test .commandLine )
176+ err := cmd .Execute ()
132177 assert .EqualError (t , err , test .errorMessage , "Command line:%v" , test .commandLine )
133178 }
134179}
0 commit comments