[CmdletBinding()] param() $ErrorActionPreference = 'Stop' $testDirectory = $PSScriptRoot $requirements = Join-Path $testDirectory 'requirements.txt' $tempRoot = [IO.Path]::GetFullPath([IO.Path]::GetTempPath()) $workDirectory = Join-Path $tempRoot ("yovision-machine-identity-v1-{0}" -f [Guid]::NewGuid().ToString('N')) try { New-Item -ItemType Directory -Path $workDirectory | Out-Null $virtualEnvironment = Join-Path $workDirectory '.venv' python -m venv $virtualEnvironment if ($LASTEXITCODE -ne 0) { throw 'Failed to create the isolated Python environment.' } $python = Join-Path $virtualEnvironment 'Scripts\python.exe' $env:PIP_DISABLE_PIP_VERSION_CHECK = '1' $env:PYTHONDONTWRITEBYTECODE = '1' & $python -m pip install --quiet --requirement $requirements if ($LASTEXITCODE -ne 0) { throw 'Failed to install pinned machine-identity test dependencies.' } & $python $testDirectory\test_contract.py if ($LASTEXITCODE -ne 0) { throw 'Machine-identity v1 contract tests failed.' } } finally { $resolvedWorkDirectory = [IO.Path]::GetFullPath($workDirectory) if (-not $resolvedWorkDirectory.StartsWith($tempRoot, [StringComparison]::OrdinalIgnoreCase)) { throw "Refusing to remove a temporary directory outside $tempRoot" } if (Test-Path -LiteralPath $resolvedWorkDirectory) { Remove-Item -LiteralPath $resolvedWorkDirectory -Recurse -Force } }