Skip to content

Commit 54265af

Browse files
committed
PR Feedback
- use named returns - handle command flags + test case - simplify tests
1 parent 60d066f commit 54265af

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

internal/codespaces/ssh.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,19 @@ func newSSHCommand(ctx context.Context, port int, dst string, cmdArgs []string)
6767
// parseSSHArgs parses SSH arguments into two distinct slices of flags
6868
// and command. It returns an error if flags are found after a command
6969
// or if a unary flag is provided without an argument.
70-
func parseSSHArgs(args []string) ([]string, []string, error) {
71-
var (
72-
cmdArgs []string
73-
command []string
74-
)
75-
70+
func parseSSHArgs(args []string) (cmdArgs []string, command []string, err error) {
7671
for i := 0; i < len(args); i++ {
7772
arg := args[i]
78-
if strings.HasPrefix(arg, "-") {
79-
if command != nil {
80-
return nil, nil, fmt.Errorf("invalid flag after command: %s", arg)
81-
}
73+
if command != nil {
74+
command = append(command, arg)
75+
continue
76+
}
8277

78+
if strings.HasPrefix(arg, "-") {
8379
cmdArgs = append(cmdArgs, arg)
84-
if strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
80+
if len(arg) == 2 && strings.Contains("bcDeFIiLlmOopRSWw", arg[1:2]) {
8581
if i++; i == len(args) {
86-
return nil, nil, fmt.Errorf("invalid unary flag without argument: %s", arg)
82+
return nil, nil, fmt.Errorf("ssh flag: %s requires an argument", arg)
8783
}
8884

8985
cmdArgs = append(cmdArgs, args[i])

internal/codespaces/ssh_test.go

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package codespaces
22

3-
import "testing"
3+
import (
4+
"fmt"
5+
"testing"
6+
)
47

58
func TestParseSSHArgs(t *testing.T) {
69
type testCase struct {
710
Args []string
811
ParsedArgs []string
912
Command []string
13+
Error bool
1014
}
1115

1216
testCases := []testCase{
@@ -50,37 +54,39 @@ func TestParseSSHArgs(t *testing.T) {
5054
ParsedArgs: []string{"-L", "-l"},
5155
Command: nil,
5256
},
57+
{
58+
Args: []string{"-v", "echo", "-n", "test"},
59+
ParsedArgs: []string{"-v"},
60+
Command: []string{"echo", "-n", "test"},
61+
},
62+
{
63+
Args: []string{"-b"},
64+
ParsedArgs: nil,
65+
Command: nil,
66+
Error: true,
67+
},
5368
}
5469

5570
for _, tcase := range testCases {
5671
args, command, err := parseSSHArgs(tcase.Args)
57-
if err != nil {
58-
t.Errorf("received unexpected error: %w", err)
72+
if err != nil && !tcase.Error {
73+
t.Errorf("unexpected error: %v on test case: %#v", err, tcase)
74+
continue
5975
}
6076

61-
if len(args) != len(tcase.ParsedArgs) {
62-
t.Fatalf("args do not match length of expected args. %#v, got '%d'", tcase, len(args))
63-
}
64-
if len(command) != len(tcase.Command) {
65-
t.Fatalf("command dooes not match length of expected command. %#v, got '%d'", tcase, len(command))
77+
if tcase.Error && err == nil {
78+
t.Errorf("expected error and got nil: %#v", tcase)
79+
continue
6680
}
6781

68-
for i, arg := range args {
69-
if arg != tcase.ParsedArgs[i] {
70-
t.Fatalf("arg does not match expected parsed arg. %v, got '%s'", tcase, arg)
71-
}
72-
}
73-
for i, c := range command {
74-
if c != tcase.Command[i] {
75-
t.Fatalf("command does not match expected command. %v, got: '%v'", tcase, command)
76-
}
82+
argsStr, parsedArgsStr := fmt.Sprintf("%s", args), fmt.Sprintf("%s", tcase.ParsedArgs)
83+
if argsStr != parsedArgsStr {
84+
t.Errorf("args do not match parsed args. got: '%s', expected: '%s'", argsStr, parsedArgsStr)
7785
}
78-
}
79-
}
8086

81-
func TestParseSSHArgsError(t *testing.T) {
82-
_, _, err := parseSSHArgs([]string{"-X", "test", "-Y"})
83-
if err == nil {
84-
t.Error("expected an error for invalid args")
87+
commandStr, parsedCommandStr := fmt.Sprintf("%s", command), fmt.Sprintf("%s", tcase.Command)
88+
if commandStr != parsedCommandStr {
89+
t.Errorf("command does not match parsed command. got: '%s', expected: '%s'", commandStr, parsedCommandStr)
90+
}
8591
}
8692
}

0 commit comments

Comments
 (0)