Switch if block logic, assert err string

This commit is contained in:
Jose Garcia 2021-09-17 15:04:55 -04:00
parent da58313358
commit 5890d6ad66
2 changed files with 25 additions and 19 deletions

View file

@ -70,21 +70,20 @@ func parseSSHArgs(args []string) (cmdArgs, command []string, err error) {
for i := 0; i < len(args); i++ {
arg := args[i]
if strings.HasPrefix(arg, "-") {
cmdArgs = append(cmdArgs, arg)
if len(arg) == 2 && strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
if i++; i == len(args) {
return nil, nil, fmt.Errorf("ssh flag: %s requires an argument", arg)
}
cmdArgs = append(cmdArgs, args[i])
}
continue
// if we've started parsing the command, set it to the rest of the args
if !strings.HasPrefix(arg, "-") {
command = args[i:]
break
}
// if we've started parsing the command, set it to the rest of the args
command = args[i:]
break
cmdArgs = append(cmdArgs, arg)
if len(arg) == 2 && strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
if i++; i == len(args) {
return nil, nil, fmt.Errorf("ssh flag: %s requires an argument", arg)
}
cmdArgs = append(cmdArgs, args[i])
}
}
return cmdArgs, command, nil

View file

@ -10,7 +10,7 @@ func TestParseSSHArgs(t *testing.T) {
Args []string
ParsedArgs []string
Command []string
Error bool
Error string
}
testCases := []testCase{
@ -69,19 +69,26 @@ func TestParseSSHArgs(t *testing.T) {
Args: []string{"-b"},
ParsedArgs: nil,
Command: nil,
Error: true,
Error: "ssh flag: -b requires an argument",
},
}
for _, tcase := range testCases {
args, command, err := parseSSHArgs(tcase.Args)
if !tcase.Error && err != nil {
t.Errorf("unexpected error: %v on test case: %#v", err, tcase)
if tcase.Error != "" {
if err == nil {
t.Errorf("expected error and got nil: %#v", tcase)
}
if err.Error() != tcase.Error {
t.Errorf("error does not match expected error, got: '%s', expected: '%s'", err.Error(), tcase.Error)
}
continue
}
if tcase.Error && err == nil {
t.Errorf("expected error and got nil: %#v", tcase)
if err != nil {
t.Errorf("unexpected error: %v on test case: %#v", err, tcase)
continue
}