-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
275 lines (245 loc) · 5.94 KB
/
Copy pathmain_test.go
File metadata and controls
275 lines (245 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
// withTempStore redirects all store I/O to a temp file for the duration of fn.
func withTempStore(t *testing.T, fn func()) {
t.Helper()
dir := t.TempDir()
path := filepath.Join(dir, "mem.json")
orig := getStorePath
getStorePath = func() string { return path }
t.Cleanup(func() { getStorePath = orig })
fn()
}
// withStoreAt sets the store path to an explicit file path.
func withStoreAt(t *testing.T, path string, fn func()) {
t.Helper()
orig := getStorePath
getStorePath = func() string { return path }
t.Cleanup(func() { getStorePath = orig })
fn()
}
// captureStdout runs fn and returns everything written to os.Stdout.
func captureStdout(t *testing.T, fn func()) string {
t.Helper()
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
orig := os.Stdout
os.Stdout = w
fn()
w.Close()
os.Stdout = orig
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func TestLoadEmpty(t *testing.T) {
withTempStore(t, func() {
s := load()
if len(s) != 0 {
t.Fatalf("expected empty store, got %v", s)
}
})
}
func TestSaveAndLoad(t *testing.T) {
withTempStore(t, func() {
s := Store{"foo": "echo foo", "bar": "ls -la"}
save(s)
got := load()
for k, v := range s {
if got[k] != v {
t.Errorf("key %q: want %q got %q", k, v, got[k])
}
}
})
}
func TestCmdAddNew(t *testing.T) {
withTempStore(t, func() {
out := captureStdout(t, func() {
cmdAdd("greet", "echo hello")
})
if !strings.Contains(out, `saved "greet"`) {
t.Errorf("unexpected output: %q", out)
}
s := load()
if s["greet"] != "echo hello" {
t.Errorf("want %q got %q", "echo hello", s["greet"])
}
})
}
func TestCmdAddOverwriteAccepted(t *testing.T) {
withTempStore(t, func() {
cmdAdd("greet", "echo hello")
r, w, _ := os.Pipe()
fmt.Fprintln(w, "y")
w.Close()
origStdin := os.Stdin
os.Stdin = r
defer func() { os.Stdin = origStdin }()
captureStdout(t, func() {
cmdAdd("greet", "echo world")
})
s := load()
if s["greet"] != "echo world" {
t.Errorf("want overwritten value, got %q", s["greet"])
}
})
}
func TestCmdAddOverwriteRejected(t *testing.T) {
withTempStore(t, func() {
cmdAdd("greet", "echo hello")
r, w, _ := os.Pipe()
fmt.Fprintln(w, "n")
w.Close()
origStdin := os.Stdin
os.Stdin = r
defer func() { os.Stdin = origStdin }()
captureStdout(t, func() {
cmdAdd("greet", "echo world")
})
s := load()
if s["greet"] != "echo hello" {
t.Errorf("want original value, got %q", s["greet"])
}
})
}
func TestCmdShow(t *testing.T) {
withTempStore(t, func() {
cmdAdd("x", "echo x")
out := captureStdout(t, func() {
cmdShow("x")
})
if strings.TrimSpace(out) != "echo x" {
t.Errorf("want %q got %q", "echo x", out)
}
})
}
func TestCmdDelete(t *testing.T) {
withTempStore(t, func() {
cmdAdd("x", "echo x")
captureStdout(t, func() {
cmdDelete("x")
})
s := load()
if _, ok := s["x"]; ok {
t.Error("key should be deleted")
}
})
}
func TestCmdList(t *testing.T) {
withTempStore(t, func() {
cmdAdd("alpha", "echo a")
cmdAdd("beta", "echo b")
out := captureStdout(t, func() {
cmdList()
})
if !strings.Contains(out, "alpha") || !strings.Contains(out, "beta") {
t.Errorf("list missing entries: %q", out)
}
if strings.Index(out, "alpha") > strings.Index(out, "beta") {
t.Error("list not sorted")
}
})
}
func TestCmdListEmpty(t *testing.T) {
withTempStore(t, func() {
out := captureStdout(t, func() {
cmdList()
})
if !strings.Contains(out, "no saved commands") {
t.Errorf("unexpected output: %q", out)
}
})
}
func TestCmdRun(t *testing.T) {
withTempStore(t, func() {
cmdAdd("hi", "echo hi")
out := captureStdout(t, func() {
cmdRun("hi", nil)
})
if !strings.Contains(out, "hi") {
t.Errorf("unexpected output: %q", out)
}
})
}
func TestCmdRunExtraArgs(t *testing.T) {
withTempStore(t, func() {
cmdAdd("say", "echo")
out := captureStdout(t, func() {
cmdRun("say", []string{"hello", "world"})
})
if !strings.Contains(out, "hello world") {
t.Errorf("expected extra args in output, got: %q", out)
}
})
}
// --- local store / custom filename tests ---
func TestLocalStoreIsolatedFromHome(t *testing.T) {
dir := t.TempDir()
localPath := filepath.Join(dir, defaultStorageFile)
homePath := filepath.Join(t.TempDir(), defaultStorageFile)
// write something into "home" store
withStoreAt(t, homePath, func() {
cmdAdd("home-cmd", "echo home")
})
// local store starts empty — home entry not visible
withStoreAt(t, localPath, func() {
s := load()
if _, ok := s["home-cmd"]; ok {
t.Error("local store should not see home-cmd")
}
cmdAdd("local-cmd", "echo local")
})
// home store unchanged
withStoreAt(t, homePath, func() {
s := load()
if _, ok := s["local-cmd"]; ok {
t.Error("home store should not see local-cmd")
}
if s["home-cmd"] != "echo home" {
t.Errorf("home-cmd missing from home store")
}
})
}
func TestCustomFilename(t *testing.T) {
dir := t.TempDir()
customPath := filepath.Join(dir, ".project-cmds.json")
withStoreAt(t, customPath, func() {
cmdAdd("build", "go build ./...")
s := load()
if s["build"] != "go build ./..." {
t.Errorf("want %q got %q", "go build ./...", s["build"])
}
})
// file should exist at custom path
if _, err := os.Stat(customPath); os.IsNotExist(err) {
t.Error("custom file not created")
}
}
func TestLocalAndHomeStoresDontCollide(t *testing.T) {
dir1 := t.TempDir()
dir2 := t.TempDir()
path1 := filepath.Join(dir1, defaultStorageFile)
path2 := filepath.Join(dir2, defaultStorageFile)
withStoreAt(t, path1, func() { cmdAdd("cmd", "echo one") })
withStoreAt(t, path2, func() { cmdAdd("cmd", "echo two") })
withStoreAt(t, path1, func() {
if s := load(); s["cmd"] != "echo one" {
t.Errorf("store1 corrupted: got %q", s["cmd"])
}
})
withStoreAt(t, path2, func() {
if s := load(); s["cmd"] != "echo two" {
t.Errorf("store2 corrupted: got %q", s["cmd"])
}
})
}