-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ps1
More file actions
389 lines (344 loc) · 14.5 KB
/
test.ps1
File metadata and controls
389 lines (344 loc) · 14.5 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# shellfix - Test Suite
# Run from the repo root: .\test.ps1
param(
[string]$ShimPath,
[string]$WslDistro = "Ubuntu-24.04",
[switch]$Verbose
)
$ErrorActionPreference = "Continue"
$pass = 0
$fail = 0
$skip = 0
$originalShellfixWslDistro = $env:SHELLFIX_WSL_DISTRO
$env:SHELLFIX_WSL_DISTRO = $WslDistro
# --- Resolve shim path: repo-local build > installed > skip ---
if (-not $ShimPath) {
$repoLocal = Join-Path $PSScriptRoot "shim\out\powershell.exe"
$installed = Join-Path $env:USERPROFILE "bin\powershell.exe"
if (Test-Path $repoLocal) {
$ShimPath = $repoLocal
} elseif (Test-Path $installed) {
$ShimPath = $installed
}
}
$script:ShimPath = $ShimPath
function Test-Case {
param(
[string]$Name,
[string]$Command,
[string]$Expect,
[switch]$UseShim,
[switch]$SkipIfNoShim
)
if ($UseShim) {
if (-not $script:ShimPath -or -not (Test-Path $script:ShimPath)) {
if ($SkipIfNoShim) {
Write-Host " SKIP: $Name (shim not found)" -ForegroundColor Yellow
$script:skip++
return
}
}
$shimExe = $script:ShimPath
# Use Start-Process with -ArgumentList to preserve the raw command string.
# Direct invocation (& $shimPath -Command $Command) causes PS to re-parse
# the command, mangling quotes and special characters before the shim sees it.
$tempOut = [System.IO.Path]::GetTempFileName()
try {
$proc = Start-Process -FilePath $shimExe `
-ArgumentList "-NoProfile", "-Command", $Command `
-NoNewWindow -Wait -PassThru `
-RedirectStandardOutput $tempOut `
-RedirectStandardError ([System.IO.Path]::GetTempFileName()) 2>$null
$output = Get-Content $tempOut -Raw -ErrorAction SilentlyContinue
if (-not $output) { $output = "" }
} finally {
Remove-Item $tempOut -Force -ErrorAction SilentlyContinue
}
} else {
$output = Invoke-Expression $Command 2>&1 | Out-String
}
if ($output -match $Expect) {
Write-Host " PASS: $Name" -ForegroundColor Green
if ($Verbose) {
$trimmed = $output.Trim()
$len = [Math]::Min(60, $trimmed.Length)
if ($len -gt 0) { Write-Host " Output: $($trimmed.Substring(0, $len))" -ForegroundColor DarkGray }
}
$script:pass++
} else {
Write-Host " FAIL: $Name" -ForegroundColor Red
Write-Host " Expected: /$Expect/" -ForegroundColor DarkGray
$trimmed = $output.Trim()
$len = [Math]::Min(80, $trimmed.Length)
if ($len -gt 0) { Write-Host " Got: $($trimmed.Substring(0, $len))" -ForegroundColor DarkGray }
$script:fail++
}
}
Write-Host ""
Write-Host "=============================================="
Write-Host " shellfix - Test Suite"
Write-Host "=============================================="
# ================================================================
# Pre-flight
# ================================================================
Write-Host ""
Write-Host "--- Pre-flight ---"
$wslOk = $false
try {
$r = wsl.exe -d $WslDistro -e echo ok 2>$null
if ($r -match 'ok') { $wslOk = $true; Write-Host " OK: WSL is running" -ForegroundColor Green }
} catch {}
if (-not $wslOk) {
Write-Host " FATAL: WSL is not available. Cannot run tests." -ForegroundColor Red
exit 1
}
$shimInstalled = $script:ShimPath -and (Test-Path $script:ShimPath)
if ($shimInstalled) {
$shimItem = Get-Item $script:ShimPath
$shimHash = (Get-FileHash $script:ShimPath -Algorithm SHA256).Hash.Substring(0, 12)
Write-Host " OK: Shim under test: $script:ShimPath" -ForegroundColor Green
Write-Host " Built: $($shimItem.LastWriteTime) SHA256: $shimHash..." -ForegroundColor DarkGray
Write-Host " WSL distro: $WslDistro" -ForegroundColor DarkGray
} else {
Write-Host " WARN: Shim not found. Shim tests will be skipped." -ForegroundColor Yellow
}
$profileUnderTest = Join-Path $PSScriptRoot "profile\Microsoft.PowerShell_profile.ps1"
if (-not (Test-Path $profileUnderTest)) {
$profileUnderTest = $PROFILE
}
. $profileUnderTest 2>$null
if ($env:PS_PROFILE_LOADED -eq "yes") {
Write-Host " OK: Profile loaded: $profileUnderTest" -ForegroundColor Green
} else {
Write-Host " WARN: Profile not loaded. Profile tests may fail." -ForegroundColor Yellow
}
# Create test fixtures
wsl.exe -d $WslDistro -e bash -c "printf 'it'\''s a test\nhere'\''s another\n' > /tmp/shellfix_test.txt"
# ================================================================
# CLASS 1: Bash Commands Through PowerShell
# ================================================================
Write-Host ""
Write-Host "--- Class 1: Bash -> WSL Routing (Shim) ---"
Test-Case "grep" 'grep -c "root" /etc/passwd' '\d+' -UseShim -SkipIfNoShim
Test-Case "head" 'head -1 /etc/os-release' '.' -UseShim -SkipIfNoShim
Test-Case "tail" 'tail -1 /etc/os-release' '.' -UseShim -SkipIfNoShim
Test-Case "wc" 'wc -l /etc/passwd' '\d+' -UseShim -SkipIfNoShim
Test-Case "uname" 'uname -s' 'Linux' -UseShim -SkipIfNoShim
Test-Case "date" 'date +%Y' '\d{4}' -UseShim -SkipIfNoShim
Write-Host ""
Write-Host "--- Class 1: Control Flow ---"
# These contain bash syntax that PS 5.1 cannot have as literals.
# We build strings at runtime using char codes.
if ($shimInstalled) {
$a = [string][char]38 # &
$p = [string][char]124 # |
$cfTests = @(
@{ n = 'if/then/fi'; c = 'if [ 1 -eq 1 ]; then echo yes; fi'; e = 'yes' }
)
# Build commands containing && and || at runtime
$cfTests += @{ n = "$a$a operator"; c = "echo first $a$a echo second"; e = 'second' }
$cfTests += @{ n = "$p$p operator"; c = "false $p$p echo fallback"; e = 'fallback' }
foreach ($t in $cfTests) {
$r = & $script:ShimPath -Command $t.c 2>&1 | Out-String
if ($r -match $t.e) {
Write-Host " PASS: $($t.n)" -ForegroundColor Green; $pass++
} else {
Write-Host " FAIL: $($t.n)" -ForegroundColor Red; $fail++
}
}
} else {
1..3 | ForEach-Object {
Write-Host " SKIP: control flow (shim not installed)" -ForegroundColor Yellow; $skip++
}
}
Write-Host ""
Write-Host "--- Class 1: Quoting ---"
Test-Case "apostrophe" "grep `"it's`" /tmp/shellfix_test.txt" "it's a test" -UseShim -SkipIfNoShim
Test-Case "here's" "grep `"here's`" /tmp/shellfix_test.txt" "here's another" -UseShim -SkipIfNoShim
Write-Host ""
Write-Host "--- Class 1: Bash Wrappers (Profile) ---"
Test-Case "grep wrapper" 'grep -c "root" /etc/passwd' '\d+'
Test-Case "head wrapper" 'head -1 /etc/os-release' '.'
Test-Case "seq wrapper" 'seq 1 3' '2'
Test-Case "date wrapper" 'date +%Y' '\d{4}'
Write-Host ""
Write-Host "--- Class 1: Alias Deconfliction ---"
Test-Case "curl is Function" '(Get-Command curl).CommandType' 'Function'
Test-Case "diff is Function" '(Get-Command diff).CommandType' 'Function'
Test-Case "sort is Function" '(Get-Command sort).CommandType' 'Function'
Test-Case "cat not alias" '(Get-Command cat -ErrorAction SilentlyContinue).CommandType -ne "Alias"' 'True'
# ================================================================
# CLASS 2: Complex PS Quoting
# ================================================================
Write-Host ""
Write-Host "--- Class 2: PS Passthrough ---"
Test-Case "Write-Host" 'Write-Host "hello"' 'hello' -UseShim -SkipIfNoShim
Test-Case "Get-Date" 'Get-Date -Format yyyy' '\d{4}' -UseShim -SkipIfNoShim
Test-Case "PS variable" '$PSVersionTable.PSVersion.Major' '\d+' -UseShim -SkipIfNoShim
# ================================================================
# CLASS 3: NativeCommandError (stderr)
# ================================================================
Write-Host ""
Write-Host "--- Class 3: Native Tool Wrapping ---"
$nativeTests = @('git', 'gh', 'npm', 'dotnet')
foreach ($tool in $nativeTests) {
$cmd = Get-Command $tool -ErrorAction SilentlyContinue
if ($cmd) {
if ($cmd.CommandType -eq 'Function') {
Write-Host " PASS: $tool is wrapped Function" -ForegroundColor Green
$pass++
} else {
Write-Host " FAIL: $tool is $($cmd.CommandType) (should be Function)" -ForegroundColor Red
$fail++
}
} else {
Write-Host " SKIP: $tool not installed" -ForegroundColor Yellow
$skip++
}
}
Write-Host ""
Write-Host "--- Class 3: Clean Output ---"
$gitOut = git status 2>&1 | Out-String
if ($gitOut -notmatch 'NativeCommandError') {
Write-Host " PASS: git status - no NativeCommandError" -ForegroundColor Green
$pass++
} else {
Write-Host " FAIL: git status - NativeCommandError present" -ForegroundColor Red
$fail++
}
$ghOut = gh --version 2>&1 | Out-String
if ($ghOut -match 'gh version') {
Write-Host " PASS: gh --version - clean output" -ForegroundColor Green
$pass++
} else {
Write-Host " FAIL: gh --version" -ForegroundColor Red
$fail++
}
Write-Host ""
Write-Host "--- Class 3: Exit Code Propagation ---"
git log -1 --oneline 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " PASS: git success = exit 0" -ForegroundColor Green
$pass++
} else {
Write-Host " FAIL: git success = exit $LASTEXITCODE" -ForegroundColor Red
$fail++
}
git log --oneline nonexistent-branch-xyz 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host " PASS: git failure = exit $LASTEXITCODE (non-zero)" -ForegroundColor Green
$pass++
} else {
Write-Host " FAIL: git failure = exit 0 (expected non-zero)" -ForegroundColor Red
$fail++
}
# ================================================================
# TIER 1: ANSI, dotnet, formatting
# ================================================================
Write-Host ""
Write-Host "--- Tier 1: ANSI Suppression ---"
Test-Case "NO_COLOR" '$env:NO_COLOR' '1'
Test-Case "TERM=dumb" '$env:TERM' 'dumb'
Test-Case "DOTNET_NOLOGO" '$env:DOTNET_NOLOGO' '1'
# Test ANSI stripping function
$ansiInput = "$([char]27)[31mERROR$([char]27)[0m: something failed"
$stripped = _shellfix_strip_ansi $ansiInput
if ($stripped -eq 'ERROR: something failed') {
Write-Host " PASS: ANSI strip function" -ForegroundColor Green; $pass++
} else {
Write-Host " FAIL: ANSI strip function (got: $stripped)" -ForegroundColor Red; $fail++
}
Write-Host ""
Write-Host "--- Tier 1: Output Formatting ---"
Test-Case "FormatEnumerationLimit" '$FormatEnumerationLimit' '-1'
# ================================================================
# TIER 2: BOM-safe, LongPaths, Shell Integration
# ================================================================
Write-Host ""
Write-Host "--- Tier 2: BOM-Safe File Writing ---"
Test-Case "Set-Content default UTF8" '$PSDefaultParameterValues["Set-Content:Encoding"]' 'UTF8'
Test-Case "Out-File default UTF8" '$PSDefaultParameterValues["Out-File:Encoding"]' 'UTF8'
Test-Case "Add-Content default UTF8" '$PSDefaultParameterValues["Add-Content:Encoding"]' 'UTF8'
# Test Write-Utf8NoBom helper
$testFile = Join-Path $env:TEMP "shellfix_bom_test.txt"
Write-Utf8NoBom -Path $testFile -Content "hello world"
$bytes = [System.IO.File]::ReadAllBytes($testFile)
# BOM would be EF BB BF (239 187 191). Check first 3 bytes are NOT the BOM.
if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
Write-Host " FAIL: Write-Utf8NoBom has BOM" -ForegroundColor Red; $fail++
} else {
$content = [System.IO.File]::ReadAllText($testFile)
if ($content -eq "hello world") {
Write-Host " PASS: Write-Utf8NoBom (no BOM, correct content)" -ForegroundColor Green; $pass++
} else {
Write-Host " FAIL: Write-Utf8NoBom content mismatch" -ForegroundColor Red; $fail++
}
}
Remove-Item $testFile -Force -ErrorAction SilentlyContinue
Write-Host ""
Write-Host "--- Tier 2: Shell Integration ---"
# Verify prompt function exists and doesn't conflict
$promptCmd = Get-Command prompt -ErrorAction SilentlyContinue
if ($promptCmd) {
Write-Host " PASS: prompt function exists" -ForegroundColor Green; $pass++
} else {
Write-Host " FAIL: prompt function missing" -ForegroundColor Red; $fail++
}
# ================================================================
# Infrastructure
# ================================================================
Write-Host ""
Write-Host "--- Infrastructure ---"
Test-Case "WSL_UTF8" '$env:WSL_UTF8' '1'
Test-Case "WSLENV" '$env:WSLENV' 'PYTHONUTF8'
Test-Case "Profile loaded" '$env:PS_PROFILE_LOADED' 'yes'
$wh = Test-WslHealth
if ($wh) {
Write-Host " PASS: WSL healthy" -ForegroundColor Green; $pass++
} else {
Write-Host " FAIL: WSL unhealthy" -ForegroundColor Red; $fail++
}
# ================================================================
# Issue Regression Tests (Shim Layer)
# These tests invoke the shim binary directly via -UseShim,
# which reads raw command line and bypasses PS parsing.
# ================================================================
Write-Host "`nIssue Regression Tests (Shim):" -ForegroundColor Cyan
# Issue #1: && in wsl bash -c
Test-Case "Issue #1: && in bash -c" `
"wsl -d $WslDistro -- bash -c `"echo hello && echo world`"" `
'hello' -UseShim -SkipIfNoShim
# Issue #2: Python [1:-1] slice syntax
Test-Case "Issue #2: Python slice [1:-1]" `
('wsl -d {0} -- bash -c "python3 -c ''print(list(range(5))[1:-1])''"' -f $WslDistro) `
'\[1.*2.*3\]' -UseShim -SkipIfNoShim
# Issue #3: Nested single quotes inside a bash -c payload
Test-Case "Issue #3: Nested quotes python" `
('wsl -d {0} -- bash -c "python3 -c ''print(1)''"' -f $WslDistro) `
'1' -UseShim -SkipIfNoShim
# Issue #1 variant: multi-command chain
Test-Case "Issue #1 variant: triple &&" `
"wsl -d $WslDistro -- bash -c `"echo one && echo two && echo three`"" `
'three' -UseShim -SkipIfNoShim
# ================================================================
# Cleanup
# ================================================================
wsl.exe -d $WslDistro -e rm -f /tmp/shellfix_test.txt 2>$null
$total = $pass + $fail
Write-Host ""
Write-Host "=============================================="
if ($fail -eq 0) {
Write-Host " PASS: $pass / $total" -ForegroundColor Green
} else {
Write-Host " PASS: $pass / $total" -ForegroundColor Red
}
if ($skip -gt 0) { Write-Host " SKIP: $skip" -ForegroundColor Yellow }
if ($fail -gt 0) { Write-Host " FAIL: $fail" -ForegroundColor Red }
Write-Host "=============================================="
Write-Host ""
if ($null -eq $originalShellfixWslDistro) {
Remove-Item Env:SHELLFIX_WSL_DISTRO -ErrorAction SilentlyContinue
} else {
$env:SHELLFIX_WSL_DISTRO = $originalShellfixWslDistro
}
exit $fail