-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
195 lines (151 loc) · 4.36 KB
/
main_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
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
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
)
var (
commandRegular = "./regular"
)
func createTempDir(t *testing.T) string {
t.Helper()
dir, err := os.MkdirTemp("", "regular-test-*")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}
err = os.Mkdir(filepath.Join(dir, "config"), dirPerms)
if err != nil {
t.Fatalf("Failed to create subdirectory: %v", err)
}
err = os.Mkdir(filepath.Join(dir, "status"), dirPerms)
if err != nil {
t.Fatalf("Failed to create subdirectory: %v", err)
}
t.Cleanup(func() {
os.RemoveAll(dir)
})
return dir
}
func command(args ...string) (string, string, error) {
fullArgs := append([]string{"--output", "-"}, args...)
cmd := exec.Command(commandRegular, fullArgs...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
func commandWithDirs(tempDir string, args ...string) (string, string, error) {
configDir := filepath.Join(tempDir, "config")
stateDir := filepath.Join(tempDir, "state")
allArgs := append(
[]string{"--config-dir", configDir, "--state-dir", stateDir},
args...,
)
return command(allArgs...)
}
func TestNoArgs(t *testing.T) {
_, stderr, _ := command()
if matched, _ := regexp.MatchString("expected one of", stderr); !matched {
t.Error("Expected 'expected one of' in stderr")
}
}
func TestVersion(t *testing.T) {
stdout, _, _ := command("--version")
if matched, _ := regexp.MatchString(`\d+\.\d+\.\d+`, stdout); !matched {
t.Error("Expected version format in stdout")
}
}
func TestListCommandHelp(t *testing.T) {
stdout, _, err := command("list", "--help")
if err != nil {
t.Errorf("Expected no error for 'list --help', got %v", err)
}
if !strings.Contains(stdout, "List available jobs") {
t.Error("Expected 'List available jobs' in stdout")
}
}
func TestListCommand(t *testing.T) {
tempDir := createTempDir(t)
_, _, err := commandWithDirs(tempDir, "list")
if err != nil {
t.Errorf("Expected no error for 'list', got %v", err)
}
}
func TestLogCommandHelp(t *testing.T) {
stdout, _, err := command("log", "--help")
if err != nil {
t.Errorf("Expected no error for 'log --help', got %v", err)
}
if !strings.Contains(stdout, "Show application log") {
t.Error("Expected 'Show application log' in stdout")
}
}
func TestLogCommand(t *testing.T) {
tempDir := createTempDir(t)
_, _, err := commandWithDirs(tempDir, "log")
if err != nil {
t.Errorf("Expected no error for 'log', got %v", err)
}
}
func TestRunCommandHelp(t *testing.T) {
stdout, _, err := command("run", "--help")
if err != nil {
t.Errorf("Expected no error for 'run --help', got %v", err)
}
if !strings.Contains(stdout, "Run jobs once") {
t.Error("Expected 'Run jobs once' in stdout")
}
}
func TestRunCommand(t *testing.T) {
tempDir := createTempDir(t)
_, _, err := commandWithDirs(tempDir, "run")
if err != nil {
t.Errorf("Expected no error for 'run', got %v", err)
}
}
func TestStartCommandHelp(t *testing.T) {
stdout, _, err := command("start", "--help")
if err != nil {
t.Errorf("Expected no error for 'start --help', got %v", err)
}
if !strings.Contains(stdout, "Start scheduler") {
t.Error("Expected 'Start scheduler' in stdout")
}
}
func TestStatusCommandHelp(t *testing.T) {
stdout, _, err := command("status", "--help")
if err != nil {
t.Errorf("Expected no error for 'status --help', got %v", err)
}
if !strings.Contains(stdout, "Show job status") {
t.Error("Expected 'Show job status' in stdout")
}
}
func TestStatusCommand(t *testing.T) {
tempDir := createTempDir(t)
_, _, err := commandWithDirs(tempDir, "status")
if err != nil {
t.Errorf("Expected no error for 'status', got %v", err)
}
}
func TestStatusLogLines(t *testing.T) {
tempDir := createTempDir(t)
_, _, err := commandWithDirs(tempDir, "status", "-l", "5")
if exitErr, ok := err.(*exec.ExitError); ok {
t.Errorf("Expected status command to run, got exit code %d", exitErr.ExitCode())
}
}
func TestStatusInvalidConfigDir(t *testing.T) {
stdout, _, err := command("status", "--config-dir", "/nonexistent/path")
if _, ok := err.(*exec.ExitError); !ok {
t.Error("Expected error for invalid config directory")
}
if !strings.Contains(stdout, "error looking for jobs in config dir") {
t.Error("Expected 'error looking for jobs in config dir' in stdout")
}
}