Files
nyaterm/scripts/clean_test_temp_dirs.ps1
Kang 958b55905b refactor(tests): standardize test directory management across modules
- Introduced the `TestConfigDir` utility to streamline the creation and management of temporary test directories, ensuring consistent naming and cleanup across various test modules.
- Updated multiple test files to utilize `TestConfigDir`, enhancing clarity and reducing redundancy in test setup.
- Improved the robustness of test directory handling, particularly in scenarios involving concurrent test execution, to prevent conflicts and ensure isolation.
- Added assertions to verify the correct cleanup of test directories after execution, reinforcing the integrity of the testing environment.
2026-09-04 12:15:16 +08:00

49 lines
1.5 KiB
PowerShell

[CmdletBinding()]
param(
[switch]$Apply
)
$ErrorActionPreference = "Stop"
$tempRoot = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar
)
$uuid = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
$testNamePattern = "^nyaterm-.+-(?<pid>[0-9]+)-$uuid$"
$candidates = @()
foreach ($directory in Get-ChildItem -LiteralPath $tempRoot -Directory -Filter "nyaterm-*") {
if ($directory.Name -notmatch $testNamePattern) {
continue
}
$resolved = [System.IO.Path]::GetFullPath($directory.FullName)
$parent = [System.IO.Path]::GetFullPath($directory.Parent.FullName).TrimEnd(
[System.IO.Path]::DirectorySeparatorChar
)
if ($parent -ne $tempRoot) {
throw "Refusing test directory outside the system Temp root: $resolved"
}
$ownerPid = [int]$Matches.pid
if ($null -ne (Get-Process -Id $ownerPid -ErrorAction SilentlyContinue)) {
Write-Warning "Skipping active test process $ownerPid directory: $resolved"
continue
}
$candidates += $resolved
}
if (-not $Apply) {
$candidates
Write-Host "Found $($candidates.Count) stale NyaTerm test director$(if ($candidates.Count -eq 1) { 'y' } else { 'ies' })."
Write-Host "Run again with -Apply to remove them."
exit 0
}
foreach ($candidate in $candidates) {
Remove-Item -LiteralPath $candidate -Recurse -Force
}
Write-Host "Removed $($candidates.Count) stale NyaTerm test director$(if ($candidates.Count -eq 1) { 'y' } else { 'ies' })."