Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Prevent PowerShell script tests from creating a Microsoft folder (#1675)

Ali Khokharedc2a11cd272
diff --git a/tests/scripts/conftest.py b/tests/scripts/conftest.py
new file mode 100644
index 00000000..6bdaca41
--- /dev/null
+++ b/tests/scripts/conftest.py
@@ -0,0 +1,16 @@
+"""Isolate caches created by installer and lifecycle test subprocesses."""
+
+from pathlib import Path
+
+import pytest
+
+
[email protected](autouse=True)
+def _isolate_powershell_module_cache(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ # A redirected USERPROFILE can make Windows PowerShell's default cache path
+ # relative to the checkout, even when LOCALAPPDATA points at a temporary dir.
+ monkeypatch.setenv(
+ "PSModuleAnalysisCachePath", str(tmp_path / "powershell-module-cache")
+ )
diff --git a/tests/scripts/test_powershell_cache.py b/tests/scripts/test_powershell_cache.py
new file mode 100644
index 00000000..e7aaabcb
--- /dev/null
+++ b/tests/scripts/test_powershell_cache.py
@@ -0,0 +1,69 @@
+"""Keep PowerShell's background module cache inside each test's storage."""
+
+import os
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+
[email protected](os.name != "nt", reason="Windows PowerShell cache behavior")
[email protected]("shell_name", ["powershell", "pwsh"])
+def test_powershell_module_cache_does_not_leak_into_working_directory(
+ tmp_path: Path, shell_name: str
+) -> None:
+ shell = shutil.which(shell_name)
+ if shell is None:
+ pytest.skip(f"{shell_name} is not installed")
+
+ working_directory = tmp_path / "working-directory"
+ profile = tmp_path / "profile"
+ local_app_data = tmp_path / "local-app-data"
+ for directory in (working_directory, profile, local_app_data):
+ directory.mkdir()
+
+ cache_value = os.environ.get("PSMODULEANALYSISCACHEPATH")
+ if cache_value:
+ assert Path(cache_value).is_absolute()
+ assert Path(cache_value).is_relative_to(tmp_path)
+ leaked_cache = (
+ working_directory
+ / "Microsoft"
+ / "Windows"
+ / "PowerShell"
+ / "ModuleAnalysisCache"
+ )
+ watched_cache = Path(cache_value) if cache_value else leaked_cache
+ result = subprocess.run(
+ [
+ shell,
+ "-NoProfile",
+ "-NonInteractive",
+ "-Command",
+ """$ErrorActionPreference = 'Stop'
+Get-Command fcc_nonexistent_cache_probe_command -ErrorAction SilentlyContinue
+$deadline = [DateTime]::UtcNow.AddSeconds(20)
+while (-not (Test-Path -LiteralPath $env:FCC_TEST_WATCHED_CACHE) -and [DateTime]::UtcNow -lt $deadline) {
+ Start-Sleep -Milliseconds 100
+}
+""",
+ ],
+ cwd=working_directory,
+ env=os.environ
+ | {
+ "USERPROFILE": str(profile),
+ "LOCALAPPDATA": str(local_app_data),
+ "FCC_TEST_WATCHED_CACHE": str(watched_cache),
+ },
+ capture_output=True,
+ text=True,
+ check=False,
+ timeout=30,
+ )
+
+ assert result.returncode == 0, result.stderr
+ assert not (working_directory / "Microsoft").exists(), "PowerShell leaked its cache"
+ assert cache_value, "PowerShell test processes need an isolated cache path"
+ assert watched_cache.is_file(), "PowerShell did not exercise its cache writer"
+ assert watched_cache.stat().st_size > 0