|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | 4 | "os" |
| 5 | + "path/filepath" |
6 | 6 | "testing" |
7 | 7 | ) |
8 | 8 |
|
9 | 9 | func TestGetEditor(t *testing.T) { |
10 | 10 | tt := []struct { |
11 | | - Name string |
12 | | - EditorEnv string |
13 | | - Cmd string |
14 | | - Args []string |
| 11 | + name string |
| 12 | + visual string |
| 13 | + editor string |
| 14 | + expected string |
15 | 15 | }{ |
16 | 16 | { |
17 | | - Name: "default", |
18 | | - Cmd: "nano", |
| 17 | + name: "default", |
| 18 | + expected: "nano", |
19 | 19 | }, |
20 | 20 | { |
21 | | - Name: "vim", |
22 | | - EditorEnv: "vim", |
23 | | - Cmd: "vim", |
| 21 | + name: "$EDITOR only", |
| 22 | + editor: "vim", |
| 23 | + expected: "vim", |
24 | 24 | }, |
25 | 25 | { |
26 | | - Name: "vim with flag", |
27 | | - EditorEnv: "vim --foo", |
28 | | - Cmd: "vim", |
29 | | - Args: []string{"--foo"}, |
| 26 | + name: "$VISUAL only", |
| 27 | + visual: "code -w", |
| 28 | + expected: "code -w", |
30 | 29 | }, |
31 | 30 | { |
32 | | - Name: "code", |
33 | | - EditorEnv: "code -w", |
34 | | - Cmd: "code", |
35 | | - Args: []string{"-w"}, |
| 31 | + name: "both set - $VISIAL wins", |
| 32 | + visual: "code -w", |
| 33 | + editor: "vim", |
| 34 | + expected: "code -w", |
36 | 35 | }, |
37 | 36 | } |
38 | 37 |
|
39 | 38 | for _, tc := range tt { |
40 | | - t.Run(tc.Name, func(t *testing.T) { |
41 | | - var err error |
42 | | - switch tc.EditorEnv { |
43 | | - case "": |
44 | | - err = os.Unsetenv("EDITOR") |
45 | | - default: |
46 | | - err = os.Setenv("EDITOR", tc.EditorEnv) |
| 39 | + t.Run(tc.name, func(t *testing.T) { |
| 40 | + if tc.visual != "" { |
| 41 | + os.Setenv("VISUAL", tc.visual) |
| 42 | + } else { |
| 43 | + os.Unsetenv("VISUAL") |
47 | 44 | } |
48 | | - if err != nil { |
49 | | - t.Logf("could not (un)set env: %v", err) |
50 | | - t.FailNow() |
| 45 | + if tc.editor != "" { |
| 46 | + os.Setenv("EDITOR", tc.editor) |
| 47 | + } else { |
| 48 | + os.Unsetenv("EDITOR") |
51 | 49 | } |
52 | 50 |
|
53 | | - cmd, args := getEditor() |
| 51 | + got := getEditor() |
| 52 | + if got != tc.expected { |
| 53 | + t.Errorf("getEditor() = %v, want %v", got, tc.expected) |
| 54 | + } |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestEditorCmd(t *testing.T) { |
| 60 | + tt := []struct { |
| 61 | + name string |
| 62 | + editor string |
| 63 | + path string |
| 64 | + wantCmd string |
| 65 | + wantArgs []string |
| 66 | + }{ |
| 67 | + { |
| 68 | + name: "simple editor", |
| 69 | + editor: "nano", |
| 70 | + path: "test.txt", |
| 71 | + wantCmd: "nano", |
| 72 | + wantArgs: []string{"test.txt"}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "editor with flags", |
| 76 | + editor: "code --wait", |
| 77 | + path: "test.txt", |
| 78 | + wantCmd: "code", |
| 79 | + wantArgs: []string{"--wait", "test.txt"}, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "empty editor", |
| 83 | + editor: "", |
| 84 | + path: "test.txt", |
| 85 | + wantCmd: "", |
| 86 | + wantArgs: nil, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tc := range tt { |
| 91 | + t.Run(tc.name, func(t *testing.T) { |
| 92 | + cmd := editorCmd(tc.editor, tc.path) |
| 93 | + if tc.editor == "" { |
| 94 | + if cmd != nil { |
| 95 | + t.Errorf("editorCmd() = %v, want nil", cmd) |
| 96 | + } |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + if filepath.Base(cmd.Path) != tc.wantCmd { |
| 101 | + t.Errorf("cmd.Path = %v, want %v", cmd.Path, tc.wantCmd) |
| 102 | + } |
| 103 | + |
| 104 | + if len(cmd.Args) < 2 { |
| 105 | + t.Errorf("cmd.Args too short = %v", cmd.Args) |
| 106 | + return |
| 107 | + } |
54 | 108 |
|
55 | | - if cmd != tc.Cmd { |
56 | | - t.Logf("cmd is incorrect: want %q but got %q", tc.Cmd, cmd) |
57 | | - t.FailNow() |
| 109 | + gotArgs := cmd.Args[1:] |
| 110 | + if len(gotArgs) != len(tc.wantArgs) { |
| 111 | + t.Errorf("cmd.Args = %v, want %v", gotArgs, tc.wantArgs) |
58 | 112 | } |
59 | 113 |
|
60 | | - if argStr, tcArgStr := fmt.Sprint(args), fmt.Sprint(tc.Args); argStr != tcArgStr { |
61 | | - t.Logf("args are incorrect: want %q but got %q", tcArgStr, argStr) |
62 | | - t.FailNow() |
| 114 | + for i := range gotArgs { |
| 115 | + if gotArgs[i] != tc.wantArgs[i] { |
| 116 | + t.Errorf("cmd.Args[%d] = %v, want %v", i, gotArgs[i], tc.wantArgs[i]) |
| 117 | + } |
63 | 118 | } |
64 | 119 | }) |
65 | 120 | } |
|
0 commit comments