-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_test.go
More file actions
349 lines (318 loc) · 9.73 KB
/
ding_test.go
File metadata and controls
349 lines (318 loc) · 9.73 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"flag"
"io"
"os"
"os/exec"
"testing"
"time"
)
// TestGetSoundFilePath tests the default sound file path resolution
func TestGetSoundFilePath(t *testing.T) {
// Save the original home directory and restore it after the test
originalHome := os.Getenv("HOME")
defer os.Setenv("HOME", originalHome)
// Table of test cases
tests := []struct {
name string
homeDir string
expected string
}{
{
name: "normal case",
homeDir: "/Users/testuser",
expected: "/Users/testuser/code/ding/ding.mp3",
},
{
name: "empty home",
homeDir: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the mock home directory
os.Setenv("HOME", tt.homeDir)
// Call the function
result := getSoundFilePath()
// Check the result
if result != tt.expected {
t.Errorf("getSoundFilePath() = %v, want %v", result, tt.expected)
}
})
}
}
// Mock the os.Stat function for testing file existence checks
type mockFileInfo struct{}
func (m mockFileInfo) Name() string { return "ding.mp3" }
func (m mockFileInfo) Size() int64 { return 1024 }
func (m mockFileInfo) Mode() os.FileMode { return 0644 }
func (m mockFileInfo) ModTime() time.Time { return time.Now() }
func (m mockFileInfo) IsDir() bool { return false }
func (m mockFileInfo) Sys() interface{} { return nil }
// TestPlayWithExternalPlayer tests the sound player selection and execution
func TestPlayWithExternalPlayer(t *testing.T) {
// We'll use a mock exec.Command function
// Save the original and restore after test
originalExecCommand := execCommand
defer func() { execCommand = originalExecCommand }()
tests := []struct {
name string
filePath string
mockCmdSuccess bool
expectError bool
expectedCmd string
expectedArgs []string
}{
{
name: "successful play on macOS",
filePath: "/path/to/sound.mp3",
mockCmdSuccess: true,
expectError: false,
expectedCmd: "afplay",
expectedArgs: []string{"/path/to/sound.mp3"},
},
{
name: "failed play on macOS",
filePath: "/path/to/sound.mp3",
mockCmdSuccess: false,
expectError: true,
expectedCmd: "afplay",
expectedArgs: []string{"/path/to/sound.mp3"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set up the mock command
execCommand = func(cmd string, args ...string) *exec.Cmd {
if cmd != tt.expectedCmd {
t.Errorf("execCommand called with cmd = %v, want %v", cmd, tt.expectedCmd)
}
for i, arg := range args {
if i >= len(tt.expectedArgs) || arg != tt.expectedArgs[i] {
t.Errorf("execCommand called with unexpected args: got %v, want %v", args, tt.expectedArgs)
break
}
}
// Return a mock command that either succeeds or fails as specified
if tt.mockCmdSuccess {
return exec.Command("echo", "success") // Will always succeed
} else {
return exec.Command("false") // Will always fail
}
}
// Call the function
err := playWithExternalPlayer(tt.filePath)
// Check the result
if (err != nil) != tt.expectError {
t.Errorf("playWithExternalPlayer() error = %v, expectError %v", err, tt.expectError)
}
})
}
}
// TestFlagParsing tests the command line flag parsing
func TestFlagParsing(t *testing.T) {
// Save original os.Args and restore after test
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
tests := []struct {
name string
args []string
expectedMessage string
expectedNoMessage bool
expectedUseTerminalBell bool
expectedCustomSoundFile string
}{
{
name: "defaults",
args: []string{"ding"},
expectedMessage: defaultMessage,
expectedNoMessage: false,
expectedUseTerminalBell: false,
expectedCustomSoundFile: "",
},
{
name: "custom message",
args: []string{"ding", "-m", "Test completed"},
expectedMessage: "Test completed",
expectedNoMessage: false,
expectedUseTerminalBell: false,
expectedCustomSoundFile: "",
},
{
name: "silent mode",
args: []string{"ding", "-s"},
expectedMessage: defaultMessage,
expectedNoMessage: true,
expectedUseTerminalBell: false,
expectedCustomSoundFile: "",
},
{
name: "terminal bell",
args: []string{"ding", "-b"},
expectedMessage: defaultMessage,
expectedNoMessage: false,
expectedUseTerminalBell: true,
expectedCustomSoundFile: "",
},
{
name: "custom sound file",
args: []string{"ding", "-f", "/custom/sound.mp3"},
expectedMessage: defaultMessage,
expectedNoMessage: false,
expectedUseTerminalBell: false,
expectedCustomSoundFile: "/custom/sound.mp3",
},
{
name: "multiple flags",
args: []string{"ding", "-m", "All done", "-f", "/custom/sound.mp3", "-s"},
expectedMessage: "All done",
expectedNoMessage: true,
expectedUseTerminalBell: false,
expectedCustomSoundFile: "/custom/sound.mp3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset flags for each test
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Set up the flags
message := flag.String("m", defaultMessage, "Message to display")
noMessage := flag.Bool("s", false, "Silent mode (no text output)")
useTerminalBell := flag.Bool("b", false, "Use terminal bell instead of custom sound")
customSoundFile := flag.String("f", "", "Path to custom sound file (overrides default)")
// Parse with the test args
os.Args = tt.args
flag.Parse()
// Check results
if *message != tt.expectedMessage {
t.Errorf("message flag = %v, want %v", *message, tt.expectedMessage)
}
if *noMessage != tt.expectedNoMessage {
t.Errorf("noMessage flag = %v, want %v", *noMessage, tt.expectedNoMessage)
}
if *useTerminalBell != tt.expectedUseTerminalBell {
t.Errorf("useTerminalBell flag = %v, want %v", *useTerminalBell, tt.expectedUseTerminalBell)
}
if *customSoundFile != tt.expectedCustomSoundFile {
t.Errorf("customSoundFile flag = %v, want %v", *customSoundFile, tt.expectedCustomSoundFile)
}
})
}
}
// TestMainIntegration to test the core logic
func TestMainIntegration(t *testing.T) {
// This will be a more integrated test that mocks several components
// Save originals
originalExecCommand := execCommand
originalOsStat := osStat
originalFmtPrint := fmtPrint
originalFmtPrintln := fmtPrintln
originalArgs := os.Args
defer func() {
execCommand = originalExecCommand
osStat = originalOsStat
fmtPrint = originalFmtPrint
fmtPrintln = originalFmtPrintln
os.Args = originalArgs
}()
tests := []struct {
name string
args []string
fileExists bool
shouldPlaySound bool
shouldPrintBell bool
shouldPrintMessage bool
expectedMessage string
}{
{
name: "default behavior - sound file exists",
args: []string{"ding"},
fileExists: true,
shouldPlaySound: true,
shouldPrintBell: false,
shouldPrintMessage: true,
expectedMessage: defaultMessage,
},
{
name: "sound file doesn't exist - fallback to bell",
args: []string{"ding"},
fileExists: false,
shouldPlaySound: false,
shouldPrintBell: true,
shouldPrintMessage: true,
expectedMessage: defaultMessage,
},
{
name: "terminal bell flag",
args: []string{"ding", "-b"},
fileExists: true, // Doesn't matter
shouldPlaySound: false,
shouldPrintBell: true,
shouldPrintMessage: true,
expectedMessage: defaultMessage,
},
{
name: "silent mode",
args: []string{"ding", "-s"},
fileExists: true,
shouldPlaySound: true,
shouldPrintBell: false,
shouldPrintMessage: false,
expectedMessage: "", // Not used in silent mode
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Mocks
playedSound := false
printedBell := false
printedMessage := false
var actualMessage string
// Mock exec.Command
execCommand = func(cmd string, args ...string) *exec.Cmd {
playedSound = true
return exec.Command("echo", "success") // Always succeeds
}
// Mock os.Stat
osStat = func(name string) (os.FileInfo, error) {
if tt.fileExists {
return mockFileInfo{}, nil
}
return nil, os.ErrNotExist
}
// Mock fmt.Fprint - note the correct io.Writer type
fmtPrint = func(w io.Writer, a ...any) (n int, err error) {
if len(a) > 0 && a[0] == bellChar {
printedBell = true
}
return 0, nil
}
// Mock fmt.Println
fmtPrintln = func(a ...any) (n int, err error) {
printedMessage = true
if len(a) > 0 {
actualMessage = a[0].(string)
}
return 0, nil
}
// Set up and run main with test args
os.Args = tt.args
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
main()
// Verify results
if playedSound != tt.shouldPlaySound {
t.Errorf("playedSound = %v, want %v", playedSound, tt.shouldPlaySound)
}
if printedBell != tt.shouldPrintBell {
t.Errorf("printedBell = %v, want %v", printedBell, tt.shouldPrintBell)
}
if printedMessage != tt.shouldPrintMessage {
t.Errorf("printedMessage = %v, want %v", printedMessage, tt.shouldPrintMessage)
}
if printedMessage && actualMessage != tt.expectedMessage {
t.Errorf("message = %v, want %v", actualMessage, tt.expectedMessage)
}
})
}
}