@@ -11,6 +11,9 @@ import (
1111 "strings"
1212 "testing"
1313 "time"
14+
15+ "github.com/GustavoCaso/meeseeks/internal/config"
16+ "github.com/GustavoCaso/meeseeks/internal/logger"
1417)
1518
1619// runCLICommand runs CLI commands as subprocess.
@@ -44,108 +47,37 @@ func runCLICommand(
4447 return exitCode
4548}
4649
47- // testDetachedDaemon manages a detached daemon for testing.
48- type testDetachedDaemon struct {
49- t * testing.T
50- configFile string
51- started bool
52- }
50+ // newTestServer creates and starts a server with the given config content.
51+ func newTestServer (t * testing.T , configContent string ) {
52+ t .Helper ()
5353
54- // newTestDetachedDaemon creates and starts a detached daemon with the given config content.
55- func newTestDetachedDaemon (t * testing.T , configContent string ) {
5654 tmpDir := t .TempDir ()
5755 configFile := filepath .Join (tmpDir , "test-config.yaml" )
5856
5957 if err := os .WriteFile (configFile , []byte (configContent ), 0600 ); err != nil {
6058 t .Fatalf ("Failed to create test config file: %v" , err )
6159 }
6260
63- daemon := & testDetachedDaemon {
64- t : t ,
65- configFile : configFile ,
66- }
67-
68- daemon .start ()
69- }
70-
71- // start starts the detached daemon.
72- func (d * testDetachedDaemon ) start () {
73- if d .started {
74- return
61+ cfg , err := config .LoadConfig (configFile )
62+ if err != nil {
63+ t .Fatalf ("failed to load test config: %v" , err )
7564 }
7665
77- expectedPidFile := getPidFile ()
78- expectedSocketPath := getSocketPath ()
79-
80- os .Remove (expectedPidFile )
81- os .Remove (expectedSocketPath )
66+ // Make sure running tests while having a production
67+ // instance of meeseeks running do not cause problems
68+ customDir := "/tmp/meeseeks"
69+ t .Setenv ("MEESEEKS_CONFIG_DIR" , customDir )
8270
83- var stdout , stderr bytes.Buffer
84- exitCode := runCLICommand (
85- []string {"run" , "-d" , "-config" , d .configFile },
86- & stdout ,
87- & stderr ,
88- 15 * time .Second ,
89- )
90-
91- if exitCode != 0 {
92- d .t .Fatalf (
93- "Failed to start daemon: exit code %d\n Stdout: %s\n Stderr: %s\n Config: %s\n PID file: %s\n Socket: %s" ,
94- exitCode ,
95- stdout .String (),
96- stderr .String (),
97- d .configFile ,
98- getPidFile (),
99- getSocketPath (),
100- )
71+ server , err := startServer (t .Context (), cfg , logger .New (), getSocketPath ())
72+ if err != nil {
73+ t .Fatalf ("failed to start test server: %v" , err )
10174 }
10275
103- d .started = true
104-
105- // Set up cleanup - use defer instead of Cleanup to ensure immediate cleanup
106- // after test function completes, not after all subtests
107- d .t .Cleanup (func () {
108- d .stop ()
76+ // Set up cleanup
77+ t .Cleanup (func () {
78+ os .RemoveAll (customDir )
79+ server .Stop ()
10980 })
110-
111- // Give daemon time to fully start
112- time .Sleep (500 * time .Millisecond )
113- }
114-
115- // stop stops the detached daemon.
116- func (d * testDetachedDaemon ) stop () {
117- if ! d .started {
118- return
119- }
120-
121- // Try to exit gracefully first
122- exitCode := runCLICommand ([]string {"exit" }, nil , nil , 5 * time .Second )
123- if exitCode != 0 {
124- d .t .Errorf ("Unexpected exit code during daemon cleanup: %d" , exitCode )
125- }
126-
127- // Force cleanup by removing PID and socket files
128- expectedPidFile := getPidFile ()
129- expectedSocketPath := getSocketPath ()
130-
131- os .Remove (expectedPidFile )
132- os .Remove (expectedSocketPath )
133-
134- d .started = false
135- }
136-
137- // ensureNoDaemonRunning ensures no daemon is running - useful for validation tests.
138- func ensureNoDaemonRunning (t * testing.T ) {
139- expectedPidFile := getPidFile ()
140- expectedSocketPath := getSocketPath ()
141-
142- if _ , err := os .Stat (expectedPidFile ); ! os .IsNotExist (err ) {
143- t .Fatal ("PID file still exists. Stoping meeseeks should remove the PID file" )
144- }
145-
146- if _ , err := os .Stat (expectedSocketPath ); ! os .IsNotExist (err ) {
147- t .Fatal ("Socket file still exists. Stoping meeseeks should remove the Socket file" )
148- }
14981}
15082
15183// commandTestCase represents a test case for command testing.
@@ -158,6 +90,8 @@ type commandTestCase struct {
15890
15991// runCommandTests runs a set of command test cases.
16092func runCommandTests (t * testing.T , tests []commandTestCase ) {
93+ t .Helper ()
94+
16195 for _ , tt := range tests {
16296 t .Run (tt .name , func (t * testing.T ) {
16397 var stdoutBuf , stderrBuf bytes.Buffer
@@ -177,6 +111,8 @@ func runCommandTests(t *testing.T, tests []commandTestCase) {
177111
178112// testCommandHelp tests the help functionality for a command.
179113func testCommandHelp (t * testing.T , command string , expectedMessages []string ) {
114+ t .Helper ()
115+
180116 var stdoutBuf , stderrBuf bytes.Buffer
181117 exitCode := runCLICommand ([]string {command , "-h" }, & stdoutBuf , & stderrBuf , 5 * time .Second )
182118 output := stdoutBuf .String () + stderrBuf .String ()
0 commit comments