|
1 | 1 | package codespaces |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
4 | 7 |
|
5 | 8 | func TestParseSSHArgs(t *testing.T) { |
6 | 9 | type testCase struct { |
7 | 10 | Args []string |
8 | 11 | ParsedArgs []string |
9 | 12 | Command []string |
| 13 | + Error bool |
10 | 14 | } |
11 | 15 |
|
12 | 16 | testCases := []testCase{ |
@@ -50,37 +54,39 @@ func TestParseSSHArgs(t *testing.T) { |
50 | 54 | ParsedArgs: []string{"-L", "-l"}, |
51 | 55 | Command: nil, |
52 | 56 | }, |
| 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 | + }, |
53 | 68 | } |
54 | 69 |
|
55 | 70 | for _, tcase := range testCases { |
56 | 71 | 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 |
59 | 75 | } |
60 | 76 |
|
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 |
66 | 80 | } |
67 | 81 |
|
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) |
77 | 85 | } |
78 | | - } |
79 | | -} |
80 | 86 |
|
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 | + } |
85 | 91 | } |
86 | 92 | } |
0 commit comments