forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
stmps_test.go
121 lines (100 loc) · 3.08 KB
/
stmps_test.go
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
package main
import (
"bytes"
"flag"
"log"
"os"
"runtime"
"testing"
"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/mpvplayer"
"github.com/stretchr/testify/assert"
)
// Test initialization of the player
func TestPlayerInitialization(t *testing.T) {
logger := logger.Init()
player, err := mpvplayer.NewPlayer(logger)
assert.NoError(t, err, "Player initialization should not return an error")
assert.NotNil(t, player, "Player should be initialized")
}
func TestMainWithoutTUI(t *testing.T) {
// Reset flags before each test, needed for flag usage in main()
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Mock osExit to prevent actual exit during test
exitCalled := false
osExit = func(code int) {
exitCalled = true
if code != 0x23420001 {
// Capture and print the stack trace
stackBuf := make([]byte, 1024)
stackSize := runtime.Stack(stackBuf, false)
stackTrace := string(stackBuf[:stackSize])
// Print the stack trace with new lines only
t.Fatalf("Unexpected exit with code: %d\nStack trace:\n%s\n", code, stackTrace)
}
// Since we don't abort execution here, we will run main() until the end or a panic.
}
headlessMode = true
testMode = true
// Restore patches after the test
defer func() {
osExit = os.Exit
headlessMode = false
testMode = false
}()
// Set command-line arguments to trigger the help flag
os.Args = []string{"doesntmatter", "--config=stmp-example.toml"}
main()
if !exitCalled {
t.Fatalf("osExit was not called")
}
}
// Regression test for https://github.com/spezifisch/stmps/issues/70
func TestMainWithConfigFileEmptyString(t *testing.T) {
// Reset flags before each test
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// Mock osExit to prevent actual exit during test
exitCalled := false
osExit = func(code int) {
exitCalled = true
if code != 0x23420001 && code != 2 {
// Capture and print the stack trace
stackBuf := make([]byte, 1024)
stackSize := runtime.Stack(stackBuf, false)
stackTrace := string(stackBuf[:stackSize])
// Print the stack trace with new lines only
t.Fatalf("Unexpected exit with code: %d\nStack trace:\n%s\n", code, stackTrace)
}
// Since we don't abort execution here, we will run main() until the end or a panic.
}
headlessMode = true
testMode = true
// Restore patches after the test
defer func() {
osExit = os.Exit
headlessMode = false
testMode = false
}()
// Set command-line arguments to trigger the help flag
os.Args = []string{"stmps"}
// Capture output of the main function
output := captureOutput(func() {
main()
})
// Check for the expected conditions
if !exitCalled {
t.Fatalf("osExit was not called")
}
// Either no error or a specific error message should pass the test
expectedErrorPrefix := "Config file error: Config File \"stmp\" Not Found"
if output != "" && !assert.Contains(t, output, expectedErrorPrefix) {
t.Fatalf("Unexpected error output: %s", output)
}
}
func captureOutput(f func()) string {
var buf bytes.Buffer
log.SetOutput(&buf)
f()
log.SetOutput(os.Stderr)
return buf.String()
}