-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxdebug_extra_test.go
More file actions
311 lines (265 loc) · 7.81 KB
/
Copy pathxdebug_extra_test.go
File metadata and controls
311 lines (265 loc) · 7.81 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
package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestXdebugDLLPath(t *testing.T) {
// Just verify it returns a non-empty path with the version in it
got := xdebugDLLPath("8.4")
if got == "" {
t.Error("expected non-empty path")
}
if !strings.Contains(got, "xdebug-8.4.dll") {
t.Errorf("expected path to contain 'xdebug-8.4.dll', got %q", got)
}
}
func TestXdebugStatus_Enabled(t *testing.T) {
lines := []string{
"[PHP]",
"zend_extension=C:\\xdebug\\xdebug-8.4.dll",
"xdebug.mode=coverage",
"xdebug.start_with_request=yes",
}
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
oldJsonOutput := jsonOutput
jsonOutput = true
xdebugStatus(lines, "8.4")
jsonOutput = oldJsonOutput
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("invalid JSON output: %v\noutput: %s", err, buf.String())
}
if result["enabled"] != true {
t.Error("expected enabled=true")
}
if result["mode"] != "coverage" {
t.Errorf("expected mode=coverage, got %v", result["mode"])
}
if result["phpVersion"] != "8.4" {
t.Errorf("expected phpVersion=8.4, got %v", result["phpVersion"])
}
}
func TestXdebugStatus_Disabled(t *testing.T) {
lines := []string{
"[PHP]",
";zend_extension=C:\\xdebug\\xdebug-8.4.dll",
"xdebug.mode=debug",
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
oldJsonOutput := jsonOutput
jsonOutput = true
xdebugStatus(lines, "8.4")
jsonOutput = oldJsonOutput
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("invalid JSON output: %v\noutput: %s", err, buf.String())
}
if result["enabled"] != false {
t.Error("expected enabled=false when zend_extension is commented")
}
}
func TestXdebugStatus_NoXdebugLines(t *testing.T) {
lines := []string{
"[PHP]",
"memory_limit=256M",
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
oldJsonOutput := jsonOutput
jsonOutput = true
xdebugStatus(lines, "8.4")
jsonOutput = oldJsonOutput
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
var result map[string]interface{}
_ = json.Unmarshal(buf.Bytes(), &result)
if result["enabled"] != false {
t.Error("expected enabled=false when no xdebug lines")
}
if result["mode"] != "" {
t.Errorf("expected empty mode, got %v", result["mode"])
}
}
func TestXdebugNeedsOutputDir(t *testing.T) {
tests := []struct {
mode string
want bool
}{
{"trace", true},
{"profile", true},
{"debug", false},
{"coverage", false},
{"off", false},
{"debug,coverage", false},
}
for _, tt := range tests {
if got := xdebugNeedsOutputDir(tt.mode); got != tt.want {
t.Errorf("xdebugNeedsOutputDir(%q) = %v, want %v", tt.mode, got, tt.want)
}
}
}
func TestXdebugRunHelp(t *testing.T) {
result := runShp(t, []string{"xdebug", "run", "--help"}, nil)
if result.ExitCode != 0 {
t.Fatalf("expected exit 0, got %d\nstderr: %s", result.ExitCode, result.Stderr)
}
if !strings.Contains(result.Stdout, "shp xdebug run <mode> -- <command...>") {
t.Errorf("expected usage line in help output, got: %s", result.Stdout)
}
}
func TestXdebugRunHelp_JSON(t *testing.T) {
result := runShp(t, []string{"--json", "xdebug", "run", "--help"}, nil)
if result.ExitCode != 0 {
t.Fatalf("expected exit 0, got %d\nstderr: %s", result.ExitCode, result.Stderr)
}
var obj map[string]interface{}
if err := json.Unmarshal([]byte(result.Stdout), &obj); err != nil {
t.Fatalf("invalid JSON: %v\noutput: %s", err, result.Stdout)
}
if obj["command"] != "xdebug run" {
t.Errorf("expected command='xdebug run', got %v", obj["command"])
}
}
func TestXdebugRunInvalidMode(t *testing.T) {
result := runShp(t, []string{"xdebug", "run", "invalid", "--", "php", "-v"}, nil)
if result.ExitCode == 0 {
t.Error("expected non-zero exit for invalid mode")
}
if !strings.Contains(result.Stderr, "invalid mode") {
t.Errorf("expected 'invalid mode' in stderr, got: %s", result.Stderr)
}
}
func TestXdebugRunMissingSeparator(t *testing.T) {
result := runShp(t, []string{"xdebug", "run", "trace", "php", "-v"}, nil)
if result.ExitCode == 0 {
t.Error("expected non-zero exit when -- is missing")
}
if !strings.Contains(result.Stderr, "missing command after '--'") {
t.Errorf("expected separator error in stderr, got: %s", result.Stderr)
}
}
func TestXdebugRunMissingCommand(t *testing.T) {
result := runShp(t, []string{"xdebug", "run", "trace", "--"}, nil)
if result.ExitCode == 0 {
t.Error("expected non-zero exit when command is missing after --")
}
if !strings.Contains(result.Stderr, "missing command after '--'") {
t.Errorf("expected missing command error in stderr, got: %s", result.Stderr)
}
}
func TestXdebugRunOffModeRejected(t *testing.T) {
result := runShp(t, []string{"xdebug", "run", "off", "--", "php", "-v"}, nil)
if result.ExitCode == 0 {
t.Error("expected non-zero exit for 'off' mode in run")
}
if !strings.Contains(result.Stderr, "invalid mode") {
t.Errorf("expected invalid mode error for 'off', got: %s", result.Stderr)
}
}
func TestXdebugActionResult_JSONIncludesOutputDir(t *testing.T) {
tests := []struct {
mode string
expectKey bool
expectedValue string
}{
{"trace", true, "."},
{"profile", true, "."},
{"debug", false, ""},
{"coverage", false, ""},
}
for _, tt := range tests {
t.Run(tt.mode, func(t *testing.T) {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
oldJsonOutput := jsonOutput
jsonOutput = true
xdebugActionResult("8.4", true, tt.mode)
jsonOutput = oldJsonOutput
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
var result map[string]interface{}
if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
t.Fatalf("invalid JSON: %v\noutput: %s", err, buf.String())
}
val, exists := result["outputDir"]
if tt.expectKey && !exists {
t.Errorf("expected outputDir key in JSON for mode %q", tt.mode)
}
if !tt.expectKey && exists {
t.Errorf("unexpected outputDir key in JSON for mode %q", tt.mode)
}
if tt.expectKey && val != tt.expectedValue {
t.Errorf("outputDir = %v, want %q", val, tt.expectedValue)
}
})
}
}
func TestXdebugActionResult_HumanOutputDir(t *testing.T) {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
oldJsonOutput := jsonOutput
jsonOutput = false
xdebugActionResult("8.4", true, "trace")
jsonOutput = oldJsonOutput
_ = w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
if !strings.Contains(output, "output_dir") {
t.Error("expected human output to mention output_dir for trace mode")
}
}
func TestDoctorCheckAliases(t *testing.T) {
root := t.TempDir()
t.Setenv("USERPROFILE", root)
t.Run("returns 0 when no config files exist", func(t *testing.T) {
issues := doctorCheckAliases()
if issues != 0 {
t.Errorf("expected 0 issues, got %d", issues)
}
})
t.Run("detects alias in bashrc", func(t *testing.T) {
bashrc := filepath.Join(root, ".bashrc")
_ = os.WriteFile(bashrc, []byte("alias php='/usr/bin/php'\n"), 0644)
issues := doctorCheckAliases()
if issues != 1 {
t.Errorf("expected 1 issue, got %d", issues)
}
_ = os.Remove(bashrc)
})
t.Run("detects alias in PowerShell profile", func(t *testing.T) {
profileDir := filepath.Join(root, "Documents", "PowerShell")
_ = os.MkdirAll(profileDir, 0755)
profile := filepath.Join(profileDir, "Microsoft.PowerShell_profile.ps1")
_ = os.WriteFile(profile, []byte("Set-Alias composer C:\\composer.bat\n"), 0644)
issues := doctorCheckAliases()
if issues < 1 {
t.Errorf("expected at least 1 issue, got %d", issues)
}
})
}