Skip to content

Commit 5890d6a

Browse files
committed
Switch if block logic, assert err string
1 parent da58313 commit 5890d6a

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

internal/codespaces/ssh.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,20 @@ func parseSSHArgs(args []string) (cmdArgs, command []string, err error) {
7070
for i := 0; i < len(args); i++ {
7171
arg := args[i]
7272

73-
if strings.HasPrefix(arg, "-") {
74-
cmdArgs = append(cmdArgs, arg)
75-
if len(arg) == 2 && strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
76-
if i++; i == len(args) {
77-
return nil, nil, fmt.Errorf("ssh flag: %s requires an argument", arg)
78-
}
79-
80-
cmdArgs = append(cmdArgs, args[i])
81-
}
82-
continue
73+
// if we've started parsing the command, set it to the rest of the args
74+
if !strings.HasPrefix(arg, "-") {
75+
command = args[i:]
76+
break
8377
}
8478

85-
// if we've started parsing the command, set it to the rest of the args
86-
command = args[i:]
87-
break
79+
cmdArgs = append(cmdArgs, arg)
80+
if len(arg) == 2 && strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
81+
if i++; i == len(args) {
82+
return nil, nil, fmt.Errorf("ssh flag: %s requires an argument", arg)
83+
}
84+
85+
cmdArgs = append(cmdArgs, args[i])
86+
}
8887
}
8988

9089
return cmdArgs, command, nil

internal/codespaces/ssh_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestParseSSHArgs(t *testing.T) {
1010
Args []string
1111
ParsedArgs []string
1212
Command []string
13-
Error bool
13+
Error string
1414
}
1515

1616
testCases := []testCase{
@@ -69,19 +69,26 @@ func TestParseSSHArgs(t *testing.T) {
6969
Args: []string{"-b"},
7070
ParsedArgs: nil,
7171
Command: nil,
72-
Error: true,
72+
Error: "ssh flag: -b requires an argument",
7373
},
7474
}
7575

7676
for _, tcase := range testCases {
7777
args, command, err := parseSSHArgs(tcase.Args)
78-
if !tcase.Error && err != nil {
79-
t.Errorf("unexpected error: %v on test case: %#v", err, tcase)
78+
if tcase.Error != "" {
79+
if err == nil {
80+
t.Errorf("expected error and got nil: %#v", tcase)
81+
}
82+
83+
if err.Error() != tcase.Error {
84+
t.Errorf("error does not match expected error, got: '%s', expected: '%s'", err.Error(), tcase.Error)
85+
}
86+
8087
continue
8188
}
8289

83-
if tcase.Error && err == nil {
84-
t.Errorf("expected error and got nil: %#v", tcase)
90+
if err != nil {
91+
t.Errorf("unexpected error: %v on test case: %#v", err, tcase)
8592
continue
8693
}
8794

0 commit comments

Comments
 (0)