@@ -5,15 +5,15 @@ import (
55 "testing"
66)
77
8- func TestParseSSHArgs (t * testing.T ) {
9- type testCase struct {
10- Args []string
11- ParsedArgs []string
12- Command []string
13- Error string
14- }
8+ type parseTestCase struct {
9+ Args []string
10+ ParsedArgs []string
11+ Command []string
12+ Error string
13+ }
1514
16- testCases := []testCase {
15+ func TestParseSSHArgs (t * testing.T ) {
16+ testCases := []parseTestCase {
1717 {}, // empty test case
1818 {
1919 Args : []string {"-X" , "-Y" },
@@ -69,37 +69,85 @@ func TestParseSSHArgs(t *testing.T) {
6969 Args : []string {"-b" },
7070 ParsedArgs : nil ,
7171 Command : nil ,
72- Error : "ssh flag: -b requires an argument" ,
72+ Error : "flag: -b requires an argument" ,
7373 },
7474 }
7575
7676 for _ , tcase := range testCases {
7777 args , command , err := parseSSHArgs (tcase .Args )
78- if tcase .Error != "" {
79- if err == nil {
80- t .Errorf ("expected error and got nil: %#v" , tcase )
81- }
8278
83- if err . Error () != tcase . Error {
84- t . Errorf ( "error does not match expected error, got: '%s', expected: '%s'" , err . Error (), tcase . Error )
85- }
79+ checkParseResult ( t , tcase , args , command , err )
80+ }
81+ }
8682
87- continue
88- }
83+ func TestParseSCPArgs (t * testing.T ) {
84+ testCases := []parseTestCase {
85+ {}, // empty test case
86+ {
87+ Args : []string {"-X" , "-Y" },
88+ ParsedArgs : []string {"-X" , "-Y" },
89+ Command : nil ,
90+ },
91+ {
92+ Args : []string {"-X" , "-Y" , "-o" , "someoption=test" },
93+ ParsedArgs : []string {"-X" , "-Y" , "-o" , "someoption=test" },
94+ Command : nil ,
95+ },
96+ {
97+ Args : []string {"-X" , "-Y" , "-o" , "someoption=test" , "local/file" , "remote:file" },
98+ ParsedArgs : []string {"-X" , "-Y" , "-o" , "someoption=test" },
99+ Command : []string {"local/file" , "remote:file" },
100+ },
101+ {
102+ Args : []string {"-X" , "-Y" , "-o" , "someoption=test" , "local/file" , "remote:file" },
103+ ParsedArgs : []string {"-X" , "-Y" , "-o" , "someoption=test" },
104+ Command : []string {"local/file" , "remote:file" },
105+ },
106+ {
107+ Args : []string {"local/file" , "remote:file" },
108+ ParsedArgs : []string {},
109+ Command : []string {"local/file" , "remote:file" },
110+ },
111+ {
112+ Args : []string {"-c" },
113+ ParsedArgs : nil ,
114+ Command : nil ,
115+ Error : "flag: -c requires an argument" ,
116+ },
117+ }
89118
90- if err != nil {
91- t .Errorf ("unexpected error: %v on test case: %#v" , err , tcase )
92- continue
93- }
119+ for _ , tcase := range testCases {
120+ args , command , err := parseSCPArgs (tcase .Args )
121+
122+ checkParseResult (t , tcase , args , command , err )
123+ }
124+ }
94125
95- argsStr , parsedArgsStr := fmt .Sprintf ("%s" , args ), fmt .Sprintf ("%s" , tcase .ParsedArgs )
96- if argsStr != parsedArgsStr {
97- t .Errorf ("args do not match parsed args. got: '%s', expected: '%s'" , argsStr , parsedArgsStr )
126+ func checkParseResult (t * testing.T , tcase parseTestCase , gotArgs , gotCmd []string , gotErr error ) {
127+ if tcase .Error != "" {
128+ if gotErr == nil {
129+ t .Errorf ("expected error and got nil: %#v" , tcase )
98130 }
99131
100- commandStr , parsedCommandStr := fmt .Sprintf ("%s" , command ), fmt .Sprintf ("%s" , tcase .Command )
101- if commandStr != parsedCommandStr {
102- t .Errorf ("command does not match parsed command. got: '%s', expected: '%s'" , commandStr , parsedCommandStr )
132+ if gotErr .Error () != tcase .Error {
133+ t .Errorf ("error does not match expected error, got: '%s', expected: '%s'" , gotErr .Error (), tcase .Error )
103134 }
135+
136+ return
137+ }
138+
139+ if gotErr != nil {
140+ t .Errorf ("unexpected error: %v on test case: %#v" , gotErr , tcase )
141+ return
142+ }
143+
144+ argsStr , parsedArgsStr := fmt .Sprintf ("%s" , gotArgs ), fmt .Sprintf ("%s" , tcase .ParsedArgs )
145+ if argsStr != parsedArgsStr {
146+ t .Errorf ("args do not match parsed args. got: '%s', expected: '%s'" , argsStr , parsedArgsStr )
147+ }
148+
149+ commandStr , parsedCommandStr := fmt .Sprintf ("%s" , gotCmd ), fmt .Sprintf ("%s" , tcase .Command )
150+ if commandStr != parsedCommandStr {
151+ t .Errorf ("command does not match parsed command. got: '%s', expected: '%s'" , commandStr , parsedCommandStr )
104152 }
105153}
0 commit comments