"""Run just the ADB startup functions with fake commands; never touch host ADB.""" import os from pathlib import Path import shutil import subprocess import pytest ROOT = Path(__file__).resolve().parents[2] @pytest.mark.skipif(not shutil.which("powershell.exe"), reason="Windows PowerShell required") @pytest.mark.parametrize("script_name", ["start.ps1", "install_deps.ps1"]) @pytest.mark.parametrize("outcome", ["success", "failure", "exception"]) def test_adb_startup_reports_failure_and_preserves_server_environment( tmp_path, script_name, outcome ): harness = tmp_path / "adb-startup.ps1" harness.write_text( """ $ErrorActionPreference = 'Stop' [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false) $tokens = $null $parseErrors = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile( $env:ADB_TEST_SCRIPT, [ref]$tokens, [ref]$parseErrors) if ($parseErrors.Count) { throw ($parseErrors | Out-String) } $function = $ast.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Start-LocalAdbServer' }, $true) Invoke-Expression $function.Extent.Text function Test-CommandExists { return $true } function adb { Write-Host ('ADB_ARGS=' + ($args -join '|')) if ($env:ADB_TEST_OUTCOME -eq 'exception') { throw 'fixture launch error' } Write-Error 'fixture stderr' -ErrorAction Continue $global:LASTEXITCODE = $(if ($env:ADB_TEST_OUTCOME -eq 'failure') { 7 } else { 0 }) } Start-LocalAdbServer if ($env:ADB_SERVER_SOCKET -ne 'tcp:remote.example:5040') { throw 'Socket was changed' } if ($env:ANDROID_ADB_SERVER_ADDRESS -ne 'remote.example') { throw 'Host was changed' } if ($env:ANDROID_ADB_SERVER_PORT -ne '5040') { throw 'Port was changed' } if ($ErrorActionPreference -ne 'Stop') { throw 'Error preference was changed' } Write-Host 'HARNESS_OK' """, encoding="utf-8-sig", ) result = subprocess.run( ["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(harness)], env={ **os.environ, "ADB_TEST_SCRIPT": str(ROOT / "scripts" / script_name), "ADB_TEST_OUTCOME": outcome, "ADB_SERVER_SOCKET": "tcp:remote.example:5040", "ANDROID_ADB_SERVER_ADDRESS": "remote.example", "ANDROID_ADB_SERVER_PORT": "5040", }, capture_output=True, encoding="utf-8", errors="replace", timeout=15, ) assert result.returncode == 0, result.stdout + result.stderr assert "ADB_ARGS=-H|localhost|-P|5037|start-server" in result.stdout assert "HARNESS_OK" in result.stdout if outcome == "success": assert "[警告]" not in result.stdout elif outcome == "failure": assert "退出码 7" in result.stdout assert "fixture stderr" in result.stdout else: assert "启动异常" in result.stdout assert "fixture launch error" in result.stdout