Skip to content

Commit b0edf56

Browse files
Simplify Windows debug scripts based on working solution
- Add setup-environment.ps1 to configure Git for Windows tee in PATH - Simplify all scripts to use working -o addopts="" approach - Add tee availability checks to all scripts - Update README with clear setup instructions and troubleshooting - Remove complex xdist workarounds in favor of simple override
1 parent 781367c commit b0edf56

File tree

7 files changed

+220
-74
lines changed

7 files changed

+220
-74
lines changed

scripts/windows-debug/README.md

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,64 @@ This folder contains PowerShell scripts to help debug the flaky `test_stdio_cont
88
- Python 3.11 or 3.12
99
- `uv` installed and configured
1010
- Virtual environment activated
11+
- Git for Windows installed (provides the `tee` command)
12+
13+
## Quick Start
14+
15+
1. First, set up your environment to make `tee` available:
16+
```powershell
17+
. .\setup-environment.ps1
18+
```
19+
**Note:** The dot (.) at the beginning is important - it sources the script.
20+
21+
2. Verify the test runs:
22+
```powershell
23+
.\test-stdio-simple.ps1
24+
```
25+
26+
3. Run the flakiness tests:
27+
```powershell
28+
.\test-stdio-flakiness-200-runs.ps1
29+
```
1130

1231
## Scripts
1332

14-
### 1. `check-tee-command.ps1`
33+
### 1. `setup-environment.ps1`
34+
Sets up the environment by adding Git for Windows tools to PATH. **Must be dot-sourced.**
35+
36+
```powershell
37+
. .\setup-environment.ps1
38+
```
39+
40+
### 2. `check-tee-command.ps1`
1541
Checks if the `tee` command is available on your Windows system. The test requires `tee` to run.
1642

1743
```powershell
1844
.\check-tee-command.ps1
1945
```
2046

21-
### 2. `test-stdio-flakiness-200-runs.ps1`
47+
### 3. `test-stdio-simple.ps1`
48+
Runs the test once to verify it works.
49+
50+
```powershell
51+
.\test-stdio-simple.ps1
52+
```
53+
54+
### 4. `test-stdio-flakiness-200-runs.ps1`
2255
Runs the test 200 times and reports the failure rate.
2356

2457
```powershell
2558
.\test-stdio-flakiness-200-runs.ps1
2659
```
2760

28-
### 3. `test-stdio-flakiness-until-failure.ps1`
61+
### 5. `test-stdio-flakiness-until-failure.ps1`
2962
Runs the test in a loop until it fails, useful for catching intermittent failures.
3063

3164
```powershell
3265
.\test-stdio-flakiness-until-failure.ps1
3366
```
3467

35-
### 4. `test-stdio-verbose-debug.ps1`
68+
### 6. `test-stdio-verbose-debug.ps1`
3669
Runs the test once with maximum debugging output enabled.
3770

3871
```powershell
@@ -42,8 +75,9 @@ Runs the test once with maximum debugging output enabled.
4275
## Usage Notes
4376

4477
1. Make sure your virtual environment is activated before running these scripts
45-
2. Run scripts from the repository root directory
46-
3. If scripts fail with execution policy errors, run:
78+
2. Always run `setup-environment.ps1` first in each new PowerShell session
79+
3. Run scripts from the `scripts\windows-debug` directory
80+
4. If scripts fail with execution policy errors, run:
4781
```powershell
4882
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
4983
```
@@ -52,4 +86,20 @@ Runs the test once with maximum debugging output enabled.
5286

5387
- If `tee` is not available, the test will be skipped
5488
- Flaky failures typically show up as timeout errors or process cleanup issues
55-
- Look for patterns in failure output, especially around process termination timing
89+
- Look for patterns in failure output, especially around process termination timing
90+
91+
## Troubleshooting
92+
93+
### "tee command not found"
94+
1. Install Git for Windows from https://gitforwindows.org/
95+
2. Run `. .\setup-environment.ps1` to add Git tools to PATH
96+
3. Verify with `python -c "import shutil; print(shutil.which('tee'))"`
97+
98+
### "no tests ran" with xdist
99+
The scripts now use `-o addopts=""` to override the pytest configuration that enables xdist by default.
100+
101+
### Making PATH changes permanent
102+
Add this line to your PowerShell profile:
103+
```powershell
104+
$env:PATH = "C:\Program Files\Git\usr\bin;$env:PATH"
105+
```

scripts/windows-debug/check-tee-command.ps1

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
Write-Host "Checking for 'tee' command availability on Windows..." -ForegroundColor Cyan
66
Write-Host ""
77

8+
# Store original PATH
9+
$originalPath = $env:PATH
10+
811
# Method 1: Using where.exe
912
Write-Host "Method 1: Using where.exe" -ForegroundColor Yellow
1013
try {
@@ -82,12 +85,47 @@ if ($pythonCheck -and $pythonCheck -ne "None") {
8285
Write-Host " Python shutil.which() did not find tee" -ForegroundColor Red
8386
}
8487

88+
Write-Host ""
89+
90+
# Method 6: Try adding Git for Windows to PATH if it exists
91+
Write-Host "Method 6: Adding Git for Windows to PATH temporarily" -ForegroundColor Yellow
92+
$gitPaths = @(
93+
"C:\Program Files\Git\usr\bin",
94+
"C:\Program Files (x86)\Git\usr\bin"
95+
)
96+
97+
$addedToPath = $false
98+
foreach ($gitPath in $gitPaths) {
99+
if (Test-Path $gitPath) {
100+
Write-Host " Found Git directory: $gitPath" -ForegroundColor Green
101+
$env:PATH = "$gitPath;$env:PATH"
102+
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
103+
if ($teeCheck -and $teeCheck -ne "None") {
104+
Write-Host " tee is now available at: $teeCheck" -ForegroundColor Green
105+
$addedToPath = $true
106+
break
107+
}
108+
}
109+
}
110+
111+
if (-not $addedToPath) {
112+
# Restore original PATH if we didn't find tee
113+
$env:PATH = $originalPath
114+
Write-Host " Could not add Git for Windows tee to PATH" -ForegroundColor Red
115+
}
116+
85117
Write-Host ""
86118
Write-Host "========== SUMMARY ==========" -ForegroundColor Cyan
87-
if ($whereResult -or $getCommandResult -or $found -or ($pythonCheck -and $pythonCheck -ne "None")) {
119+
if ($whereResult -or $getCommandResult -or $found -or ($pythonCheck -and $pythonCheck -ne "None") -or $addedToPath) {
88120
Write-Host "tee command is available" -ForegroundColor Green
89121
Write-Host ""
90122
Write-Host "The test_stdio_context_manager_exiting test should run." -ForegroundColor Green
123+
if ($addedToPath) {
124+
Write-Host ""
125+
Write-Host "Note: Git for Windows tee was added to PATH for this session." -ForegroundColor Yellow
126+
Write-Host "To make this permanent, add this to your PowerShell profile:" -ForegroundColor Yellow
127+
Write-Host " `$env:PATH = `"C:\Program Files\Git\usr\bin;`$env:PATH`"" -ForegroundColor Cyan
128+
}
91129
} else {
92130
Write-Host "tee command is NOT available" -ForegroundColor Red
93131
Write-Host ""
@@ -98,4 +136,7 @@ if ($whereResult -or $getCommandResult -or $found -or ($pythonCheck -and $python
98136
Write-Host " 2. Install WSL (Windows Subsystem for Linux)"
99137
Write-Host " 3. Install MSYS2 or Cygwin"
100138
Write-Host " 4. Use PowerShell's Tee-Object cmdlet (different syntax)"
101-
}
139+
}
140+
141+
# Restore original PATH
142+
$env:PATH = $originalPath
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env pwsh
2+
# Script to set up the environment for running stdio tests on Windows
3+
# This adds Git for Windows tools to PATH if available
4+
# Usage: . .\setup-environment.ps1 (note the dot-sourcing)
5+
6+
Write-Host "Setting up environment for stdio tests..." -ForegroundColor Cyan
7+
Write-Host ""
8+
9+
# Check if Git for Windows is installed
10+
$gitPaths = @(
11+
"C:\Program Files\Git\usr\bin",
12+
"C:\Program Files (x86)\Git\usr\bin"
13+
)
14+
15+
$gitFound = $false
16+
$gitPath = ""
17+
18+
foreach ($path in $gitPaths) {
19+
if (Test-Path $path) {
20+
$gitPath = $path
21+
$gitFound = $true
22+
break
23+
}
24+
}
25+
26+
if ($gitFound) {
27+
Write-Host "Found Git for Windows at: $gitPath" -ForegroundColor Green
28+
29+
# Add to PATH
30+
$env:PATH = "$gitPath;$env:PATH"
31+
32+
# Verify tee is available
33+
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
34+
if ($teeCheck -and $teeCheck -ne "None") {
35+
Write-Host "Successfully added tee to PATH: $teeCheck" -ForegroundColor Green
36+
Write-Host ""
37+
Write-Host "Environment is ready for stdio tests!" -ForegroundColor Green
38+
Write-Host ""
39+
Write-Host "You can now run the test scripts or individual tests:" -ForegroundColor Cyan
40+
Write-Host " .\test-stdio-flakiness-200-runs.ps1"
41+
Write-Host " .\test-stdio-flakiness-until-failure.ps1"
42+
Write-Host " .\test-stdio-verbose-debug.ps1"
43+
Write-Host ""
44+
Write-Host "Or run individual tests with:" -ForegroundColor Cyan
45+
Write-Host " uv run pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -v -o addopts="""""
46+
} else {
47+
Write-Host "WARNING: Git path was added but tee is still not available" -ForegroundColor Yellow
48+
}
49+
} else {
50+
Write-Host "Git for Windows not found in standard locations." -ForegroundColor Red
51+
Write-Host ""
52+
Write-Host "Please install Git for Windows from: https://gitforwindows.org/" -ForegroundColor Yellow
53+
Write-Host "Or manually add the Git usr\bin directory to your PATH." -ForegroundColor Yellow
54+
}
55+
56+
Write-Host ""
57+
Write-Host "Note: This only affects the current PowerShell session." -ForegroundColor Gray
58+
Write-Host "To make changes permanent, add to your PowerShell profile:" -ForegroundColor Gray
59+
Write-Host ' $env:PATH = "C:\Program Files\Git\usr\bin;$env:PATH"' -ForegroundColor Cyan

scripts/windows-debug/test-stdio-flakiness-200-runs.ps1

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#!/usr/bin/env pwsh
22
# Script to run test_stdio_context_manager_exiting 200 times to detect flakiness
33
# Usage: .\test-stdio-flakiness-200-runs.ps1
4+
#
5+
# Prerequisites: Run . .\setup-environment.ps1 first to ensure tee is available
46

57
Write-Host "Running test_stdio_context_manager_exiting 200 times to detect flakiness..." -ForegroundColor Cyan
68
Write-Host "Test: tests/client/test_stdio.py::test_stdio_context_manager_exiting" -ForegroundColor Yellow
79
Write-Host ""
810

9-
# Disable pytest plugin autoload to avoid xdist issues
10-
$env:PYTEST_DISABLE_PLUGIN_AUTOLOAD = ""
11+
# Check if tee is available
12+
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
13+
if (-not $teeCheck -or $teeCheck -eq "None") {
14+
Write-Host "ERROR: tee command not found!" -ForegroundColor Red
15+
Write-Host "Please run: . .\setup-environment.ps1" -ForegroundColor Yellow
16+
Write-Host "(Note the dot at the beginning to source the script)" -ForegroundColor Yellow
17+
exit 1
18+
}
1119

1220
$startTime = Get-Date
1321
$count = 0
@@ -17,10 +25,7 @@ $failedRuns = @()
1725
for ($i = 1; $i -le 200; $i++) {
1826
Write-Host "Run $i of 200..." -NoNewline
1927

20-
$output = & {
21-
$env:PYTEST_DISABLE_PLUGIN_AUTOLOAD = ""
22-
uv run --frozen pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -xvs --no-cov -p no:xdist 2>&1
23-
}
28+
$output = uv run --frozen pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -xvs -o addopts="" 2>&1
2429
$exitCode = $LASTEXITCODE
2530

2631
if ($exitCode -ne 0) {
@@ -47,7 +52,4 @@ if ($failures -gt 0) {
4752
Write-Host "Failed on runs: $($failedRuns -join ', ')" -ForegroundColor Red
4853
}
4954
Write-Host "Duration: $($duration.ToString())"
50-
Write-Host "Failure rate: $([math]::Round(($failures / 200) * 100, 2))%"
51-
52-
# Clean up environment variable
53-
Remove-Item Env:PYTEST_DISABLE_PLUGIN_AUTOLOAD -ErrorAction SilentlyContinue
55+
Write-Host "Failure rate: $([math]::Round(($failures / 200) * 100, 2))%"

scripts/windows-debug/test-stdio-flakiness-until-failure.ps1

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#!/usr/bin/env pwsh
22
# Script to run test_stdio_context_manager_exiting until it fails
33
# Usage: .\test-stdio-flakiness-until-failure.ps1
4+
#
5+
# Prerequisites: Run . .\setup-environment.ps1 first to ensure tee is available
46

57
Write-Host "Running test_stdio_context_manager_exiting until failure..." -ForegroundColor Cyan
68
Write-Host "Test: tests/client/test_stdio.py::test_stdio_context_manager_exiting" -ForegroundColor Yellow
79
Write-Host "Press Ctrl+C to stop" -ForegroundColor Gray
810
Write-Host ""
911

10-
# Disable pytest plugin autoload to avoid xdist issues
11-
$env:PYTEST_DISABLE_PLUGIN_AUTOLOAD = ""
12+
# Check if tee is available
13+
$teeCheck = python -c "import shutil; print(shutil.which('tee'))"
14+
if (-not $teeCheck -or $teeCheck -eq "None") {
15+
Write-Host "ERROR: tee command not found!" -ForegroundColor Red
16+
Write-Host "Please run: . .\setup-environment.ps1" -ForegroundColor Yellow
17+
Write-Host "(Note the dot at the beginning to source the script)" -ForegroundColor Yellow
18+
exit 1
19+
}
1220

1321
$startTime = Get-Date
1422
$i = 0
@@ -17,10 +25,7 @@ while ($true) {
1725
$i++
1826
Write-Host "Run $i..." -NoNewline
1927

20-
$output = & {
21-
$env:PYTEST_DISABLE_PLUGIN_AUTOLOAD = ""
22-
uv run --frozen pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -xvs --no-cov -p no:xdist 2>&1
23-
}
28+
$output = uv run --frozen pytest tests/client/test_stdio.py::test_stdio_context_manager_exiting -xvs -o addopts="" 2>&1
2429
$exitCode = $LASTEXITCODE
2530

2631
if ($exitCode -ne 0) {
@@ -45,7 +50,4 @@ while ($true) {
4550
}
4651

4752
Write-Host ""
48-
Write-Host "Exiting after failure detection." -ForegroundColor Cyan
49-
50-
# Clean up environment variable
51-
Remove-Item Env:PYTEST_DISABLE_PLUGIN_AUTOLOAD -ErrorAction SilentlyContinue
53+
Write-Host "Exiting after failure detection." -ForegroundColor Cyan

0 commit comments

Comments
 (0)