Skip to content

Commit 617328a

Browse files
authored
Merge pull request #8 from GustavoCaso/simplify-server-test-setup
use actual server instance instead of creating subcommand processes to test commands
2 parents ae59174 + edb3310 commit 617328a

4 files changed

Lines changed: 27 additions & 97 deletions

File tree

cmd/meeseeks/logs_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
)
66

77
func TestLogsCommand_Validation(t *testing.T) {
8-
ensureNoDaemonRunning(t)
9-
108
tests := []commandTestCase{
119
{
1210
name: "logs without program name",
@@ -33,7 +31,7 @@ func TestLogsCommand(t *testing.T) {
3331
args: ["-c", "echo 'test log message'; sleep 30"]
3432
`
3533

36-
newTestDetachedDaemon(t, configContent)
34+
newTestServer(t, configContent)
3735

3836
tests := []commandTestCase{
3937
{

cmd/meeseeks/status_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestStatusCommand(t *testing.T) {
1010
command: "sleep"
1111
args: ["30"]
1212
`
13-
newTestDetachedDaemon(t, configContent)
13+
newTestServer(t, configContent)
1414

1515
tests := []commandTestCase{
1616
{
@@ -51,8 +51,6 @@ test-echo-detached 0 0 no running`,
5151
}
5252

5353
func TestStatusCommand_Validation(t *testing.T) {
54-
ensureNoDaemonRunning(t)
55-
5654
tests := []commandTestCase{
5755
{
5856
name: "status with no daemon running",

cmd/meeseeks/stop_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
)
66

77
func TestStopCommand_Validation(t *testing.T) {
8-
ensureNoDaemonRunning(t)
9-
108
tests := []commandTestCase{
119
{
1210
name: "stop without program",
@@ -29,7 +27,7 @@ func TestStopCommand_MissingProgramName(t *testing.T) {
2927
args: ["30"]
3028
`
3129

32-
newTestDetachedDaemon(t, configContent)
30+
newTestServer(t, configContent)
3331

3432
tests := []commandTestCase{
3533
{

cmd/meeseeks/test_helpers.go

Lines changed: 24 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nStdout: %s\nStderr: %s\nConfig: %s\nPID file: %s\nSocket: %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.
16092
func 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.
179113
func 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

Comments
 (0)