Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / test.bat
test.bat1083 lines · 44.3 KiBRaw
| 1 | <# : |
| 2 | @echo off |
| 3 | setlocal |
| 4 | where pwsh >nul 2>nul |
| 5 | if %errorlevel%==0 ( set "PSBIN=pwsh" ) else ( set "PSBIN=powershell" ) |
| 6 | %PSBIN% -NoProfile -ExecutionPolicy Bypass -Command "$env:_BATROOT='%~dp0'; $env:_BATPATH='%~f0'; & ([scriptblock]::Create((Get-Content -Raw '%~f0'))) %*" |
| 7 | exit /b %errorlevel% |
| 8 | : end batch #> |
| 9 | # test.bat - Run Rust + unit + instrumented tests without installing apps. |
| 10 | # (PowerShell twin of ./test.) |
| 11 | # test.bat <module> [module...] # all three phases |
| 12 | # test.bat --rust-only <module> [module...] # cargo tests only |
| 13 | # test.bat --unit-only <module> [module...] # gradle unit tests only |
| 14 | # test.bat --instrumented-only [--device S] <module> # on-device tests only |
| 15 | # test.bat all [flags] # every app + library module |
| 16 | # test.bat --dry-run calculator # preview, runs nothing |
| 17 | # |
| 18 | # Module notation mirrors check.bat: slash == colon, games:/personal: shorthand, |
| 19 | # 'all' keyword, deduped. Tasks run with --continue so one broken module does not |
| 20 | # block the rest; exit code is nonzero if anything failed. |
| 21 | # |
| 22 | # RUST (skipped by --unit-only/--instrumented-only): workspace members together |
| 23 | # via -p from the repo root, standalone crates (own [workspace]) individually |
| 24 | # in their own dir. Coverage on (default) uses cargo llvm-cov; when coverage |
| 25 | # tools cannot be installed (for example offline) the step warns cleanly and |
| 26 | # falls back to plain cargo test. --no-coverage runs plain cargo test --locked. |
| 27 | # |
| 28 | # UNIT (skipped by --rust-only/--instrumented-only): app modules run |
| 29 | # :mod:createDevUnitTestCoverageReport, library modules run |
| 30 | # :mod:createDebugUnitTestCoverageReport (single gradlew invocation, |
| 31 | # --continue; app vs lib via common-conventions-app). --no-coverage runs |
| 32 | # :mod:testDevUnitTest / :mod:testDebugUnitTest instead. Afterwards each |
| 33 | # build/reports/coverage/test/*/report.xml is parsed as XML (counter type |
| 34 | # INSTRUCTION) printing per-module covered/missed plus a TOTAL. |
| 35 | # |
| 36 | # INSTRUMENTED (skipped by --rust-only/--unit-only): scope is the in-scope |
| 37 | # modules having any *.kt under src/androidTest. One gradlew invocation runs |
| 38 | # :mod:assembleDev :mod:assembleDevAndroidTest with --continue. Each app APK is |
| 39 | # session-installed (with .idsig sidecar, never plain adb install); each test |
| 40 | # APK uses plain adb install -r. Then am instrument runs the runner, with |
| 41 | # coverage pulled back to build/outputs/coverage-manual when enabled. |
| 42 | $ErrorActionPreference = 'Stop' |
| 43 | |
| 44 | $TestRoot = if ($PSScriptRoot) { $PSScriptRoot } else { $env:_BATROOT.TrimEnd('\') } |
| 45 | |
| 46 | function Show-TestHelp { |
| 47 | @' |
| 48 | Usage: |
| 49 | test.bat [--rust-only|--unit-only|--instrumented-only] [--no-coverage] [--device S] [--dry-run] [--help] <module> [module...] |
| 50 | test.bat all [flags] |
| 51 | test.bat calculator |
| 52 | test.bat --unit-only calculator maps |
| 53 | test.bat --rust-only games/voxels |
| 54 | test.bat --instrumented-only --device 9 calculator |
| 55 | test.bat --dry-run calculator |
| 56 | |
| 57 | Module notation mirrors check.bat: slash == colon, games:/personal: shorthand, |
| 58 | 'all' keyword, deduped. Tasks run with --continue so one broken module does not |
| 59 | block the rest; exit code is nonzero if anything failed. |
| 60 | |
| 61 | Phases (all run unless one scope flag selects a single phase): |
| 62 | RUST: workspace members together via -p from the repo root, standalone |
| 63 | crates individually in their own dir. Coverage on (default) runs |
| 64 | cargo llvm-cov --workspace --locked --exclude tilecodec --lcov |
| 65 | --output-path target/llvm-cov.lcov (with -p flags for in-scope crates when |
| 66 | not 'all') plus per standalone dir lcov. Missing coverage tools warn |
| 67 | cleanly and fall back to plain cargo test. --no-coverage runs plain |
| 68 | cargo test --locked equivalents. |
| 69 | UNIT: app modules -> :mod:createDevUnitTestCoverageReport, library modules |
| 70 | -> :mod:createDebugUnitTestCoverageReport (single gradlew invocation, |
| 71 | --continue). --no-coverage -> :mod:testDevUnitTest / :mod:testDebugUnitTest. |
| 72 | Afterwards each build/reports/coverage/test/*/report.xml is parsed as XML |
| 73 | (counter type INSTRUCTION) printing per-module covered/missed + TOTAL. |
| 74 | INSTRUMENTED: scope = in-scope modules having any *.kt under src/androidTest. |
| 75 | One invocation runs :mod:assembleDev :mod:assembleDevAndroidTest with |
| 76 | --continue, then each app APK is session-installed (with .idsig sidecar, |
| 77 | never plain adb install), each test APK uses plain adb install -r, then |
| 78 | am instrument runs <pkg>.test/androidx.test.runner.AndroidJUnitRunner with |
| 79 | coverage pulled back to build/outputs/coverage-manual. |
| 80 | |
| 81 | Device: --device S / --device=S selects a serial, or 8|9 for Pixel 8 / Pixel 9 |
| 82 | (bare 8|9 also accepted); default is the first non-emulator device. No device |
| 83 | is a hard error for instrumented runs unless --dry-run. |
| 84 | |
| 85 | When 'all' expands, Gradle uses the big-build heap (same as install.bat all). |
| 86 | --dry-run prints modules/tasks/commands and runs nothing. |
| 87 | '@ |
| 88 | } |
| 89 | |
| 90 | function Normalize-TestModule([string]$M) { |
| 91 | $M = $M.Trim() |
| 92 | $M = [regex]::Replace($M, '^[/:]+', '') |
| 93 | $M = [regex]::Replace($M, '[/:]+$', '') |
| 94 | $M = $M.Replace('/', ':') |
| 95 | while ($M.Contains('::')) { $M = $M.Replace('::', ':') } |
| 96 | return $M.ToLowerInvariant() |
| 97 | } |
| 98 | |
| 99 | function Get-TestAllModuleDirs { |
| 100 | $TestSettings = Join-Path $TestRoot 'settings.gradle.kts' |
| 101 | $TestFound = @() |
| 102 | if (-not (Test-Path -LiteralPath $TestSettings)) { return $TestFound } |
| 103 | foreach ($TestLine in [System.IO.File]::ReadLines($TestSettings)) { |
| 104 | if ($TestLine -match 'include\(":([^"]+)"\)') { |
| 105 | $TestP = $Matches[1].Replace(':', '/') |
| 106 | if (-not $TestP) { continue } |
| 107 | $TestBuild = Join-Path $TestRoot "$TestP/build.gradle.kts" |
| 108 | if ((Test-Path -LiteralPath $TestBuild) -and |
| 109 | (Select-String -LiteralPath $TestBuild -Pattern 'common-conventions-(app|library)' -Quiet)) { |
| 110 | $TestFound += $TestP |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return @($TestFound | Sort-Object -Unique) |
| 115 | } |
| 116 | |
| 117 | function Get-TestCargoBin { |
| 118 | $TestCargoCmd = Get-Command cargo -ErrorAction SilentlyContinue |
| 119 | if ($TestCargoCmd) { return $TestCargoCmd.Source } |
| 120 | $TestCargoHome = if ($env:CARGO_HOME) { $env:CARGO_HOME } else { Join-Path $HOME '.cargo' } |
| 121 | $TestCand = Join-Path $TestCargoHome 'bin/cargo' |
| 122 | if (Test-Path -LiteralPath $TestCand -PathType Leaf) { return $TestCand } |
| 123 | $TestCandExe = "$TestCand.exe" |
| 124 | if (Test-Path -LiteralPath $TestCandExe -PathType Leaf) { return $TestCandExe } |
| 125 | return '' |
| 126 | } |
| 127 | |
| 128 | function Get-TestAdb { |
| 129 | $TestAdbCmd = Get-Command adb -ErrorAction SilentlyContinue |
| 130 | if ($TestAdbCmd) { return $TestAdbCmd.Source } |
| 131 | $TestSdkBases = @() |
| 132 | if ($env:ANDROID_HOME) { $TestSdkBases += $env:ANDROID_HOME } |
| 133 | if ($env:ANDROID_SDK_ROOT) { $TestSdkBases += $env:ANDROID_SDK_ROOT } |
| 134 | $TestSdkBases += (Join-Path $HOME 'Android/Sdk') |
| 135 | $TestSdkBases += (Join-Path $HOME 'Library/Android/sdk') |
| 136 | if ($env:LOCALAPPDATA) { $TestSdkBases += (Join-Path $env:LOCALAPPDATA 'Android/Sdk') } |
| 137 | foreach ($TestBase in $TestSdkBases) { |
| 138 | if (-not $TestBase) { continue } |
| 139 | foreach ($TestName in @('platform-tools/adb', 'platform-tools/adb.exe')) { |
| 140 | $TestCand = Join-Path $TestBase $TestName |
| 141 | if (Test-Path -LiteralPath $TestCand -PathType Leaf) { return $TestCand } |
| 142 | } |
| 143 | } |
| 144 | return '' |
| 145 | } |
| 146 | |
| 147 | function Get-TestApplicationId([string]$Mod) { |
| 148 | $TestBuild = Join-Path $TestRoot "$($Mod.Replace(':', '/'))/build.gradle.kts" |
| 149 | if (-not (Test-Path -LiteralPath $TestBuild)) { return '' } |
| 150 | foreach ($TestLine in [System.IO.File]::ReadLines($TestBuild)) { |
| 151 | if ($TestLine -match '^\s*applicationId\s*=\s*"([^"]+)"') { return $Matches[1] } |
| 152 | } |
| 153 | return '' |
| 154 | } |
| 155 | |
| 156 | function Get-TestBuiltApk([string]$Mod, [string]$VariantLc) { |
| 157 | $TestFs = $Mod.Replace(':', '/') |
| 158 | $TestOutDir = Join-Path $TestRoot "$TestFs/build/outputs/apk/$VariantLc" |
| 159 | $TestMeta = Join-Path $TestOutDir 'output-metadata.json' |
| 160 | $TestBaseName = ($Mod -split ':')[-1] |
| 161 | $TestFileName = "$TestBaseName-$VariantLc.apk" |
| 162 | if (Test-Path -LiteralPath $TestMeta -PathType Leaf) { |
| 163 | try { |
| 164 | $TestMetaText = [System.IO.File]::ReadAllText($TestMeta) |
| 165 | $TestM = [regex]::Match($TestMetaText, '"outputFile"\s*:\s*"([^"]+)"') |
| 166 | if ($TestM.Success -and $TestM.Groups[1].Value) { $TestFileName = $TestM.Groups[1].Value } |
| 167 | } catch { |
| 168 | Write-Output "Warning: could not read output-metadata.json for $Mod; using fallback name." |
| 169 | } |
| 170 | } |
| 171 | $TestApk = Join-Path $TestOutDir $TestFileName |
| 172 | if (Test-Path -LiteralPath $TestApk -PathType Leaf) { return $TestApk } |
| 173 | return '' |
| 174 | } |
| 175 | |
| 176 | function Get-TestAndroidTestApk([string]$Mod) { |
| 177 | $TestFs = $Mod.Replace(':', '/') |
| 178 | $TestDir = Join-Path $TestRoot "$TestFs/build/outputs/apk/androidTest/dev" |
| 179 | if (-not (Test-Path -LiteralPath $TestDir)) { return '' } |
| 180 | try { |
| 181 | $TestApks = @(Get-ChildItem -LiteralPath $TestDir -Filter '*.apk' -File -ErrorAction SilentlyContinue | Sort-Object Name) |
| 182 | } catch { |
| 183 | return '' |
| 184 | } |
| 185 | if ($TestApks.Count -eq 0) { return '' } |
| 186 | return $TestApks[0].FullName |
| 187 | } |
| 188 | |
| 189 | function Test-ApkViaSession([string]$Apk, [string]$PackageId, [string]$AdbBin, [string]$Serial, [string]$Cur = '', [string]$Total = '') { |
| 190 | $TestApkName = Split-Path -Leaf $Apk |
| 191 | $TestApkSize = (Get-Item -LiteralPath $Apk).Length |
| 192 | $TestApkMb = '{0:N1}' -f ($TestApkSize / 1MB) |
| 193 | $TestDeviceApk = "/data/local/tmp/install_${PackageId}.apk" |
| 194 | $TestPrefix = '' |
| 195 | if ($Cur -and $Total) { $TestPrefix = "[$Cur/$Total] " } |
| 196 | Write-Output "$TestPrefix Installing $PackageId ($TestApkName, ${TestApkMb} MB)..." |
| 197 | Write-Output " $TestPrefix[1/4] Pushing APK to device..." |
| 198 | $TestPrevPref = $ErrorActionPreference |
| 199 | $ErrorActionPreference = 'Continue' |
| 200 | try { |
| 201 | & $AdbBin -s $Serial push $Apk $TestDeviceApk >$null 2>&1 |
| 202 | if ($LASTEXITCODE -ne 0) { |
| 203 | Write-Output " $TestPrefix Error: adb push failed for $TestApkName." |
| 204 | return $false |
| 205 | } |
| 206 | Write-Output " $TestPrefix[2/4] Creating install session..." |
| 207 | $TestCreateOut = & $AdbBin -s $Serial shell pm install-create -r 2>&1 |
| 208 | $TestSession = '' |
| 209 | $TestSm = [regex]::Match(($TestCreateOut -join "`n"), 'created install session \[([0-9]+)\]') |
| 210 | if ($TestSm.Success) { $TestSession = $TestSm.Groups[1].Value } |
| 211 | if (-not $TestSession) { |
| 212 | Write-Output " $TestPrefix Error: could not create install session." |
| 213 | & $AdbBin -s $Serial shell rm -f $TestDeviceApk >$null 2>&1 |
| 214 | return $false |
| 215 | } |
| 216 | Write-Output " $TestPrefix[3/4] Staging APK into session $TestSession..." |
| 217 | & $AdbBin -s $Serial shell pm install-write -S $TestApkSize $TestSession base.apk $TestDeviceApk >$null 2>&1 |
| 218 | if ($LASTEXITCODE -ne 0) { |
| 219 | Write-Output " $TestPrefix Error: failed to stage $TestApkName into session $TestSession." |
| 220 | & $AdbBin -s $Serial shell pm install-abandon $TestSession >$null 2>&1 |
| 221 | & $AdbBin -s $Serial shell rm -f $TestDeviceApk >$null 2>&1 |
| 222 | return $false |
| 223 | } |
| 224 | $TestIdsig = "$Apk.idsig" |
| 225 | if (Test-Path -LiteralPath $TestIdsig -PathType Leaf) { |
| 226 | $TestDeviceIdsig = "$TestDeviceApk.idsig" |
| 227 | Write-Output " $TestPrefix Staging .idsig sidecar..." |
| 228 | & $AdbBin -s $Serial push $TestIdsig $TestDeviceIdsig >$null 2>&1 |
| 229 | if ($LASTEXITCODE -eq 0) { |
| 230 | $TestIdsigSize = (Get-Item -LiteralPath $TestIdsig).Length |
| 231 | & $AdbBin -s $Serial shell pm install-write -S $TestIdsigSize $TestSession base.apk.idsig $TestDeviceIdsig >$null 2>&1 |
| 232 | if ($LASTEXITCODE -ne 0) { |
| 233 | Write-Output " $TestPrefix Warning: could not stage .idsig; system-app updates may fail fs-verity." |
| 234 | } |
| 235 | } |
| 236 | else { |
| 237 | Write-Output " $TestPrefix Warning: could not push .idsig; system-app updates may fail fs-verity." |
| 238 | } |
| 239 | & $AdbBin -s $Serial shell rm -f $TestDeviceIdsig >$null 2>&1 |
| 240 | } |
| 241 | else { |
| 242 | Write-Output " $TestPrefix Warning: no .idsig sidecar next to the APK; system-app updates may fail fs-verity." |
| 243 | } |
| 244 | Write-Output " $TestPrefix[4/4] Committing install session $TestSession..." |
| 245 | $TestCommitOut = & $AdbBin -s $Serial shell pm install-commit $TestSession 2>&1 |
| 246 | & $AdbBin -s $Serial shell rm -f $TestDeviceApk >$null 2>&1 |
| 247 | if (-not (($TestCommitOut -join "`n") -match '(?im)^Success')) { |
| 248 | Write-Output " $TestPrefix Error: install failed for ${PackageId}: $($TestCommitOut -join ' ')" |
| 249 | return $false |
| 250 | } |
| 251 | if ($Cur -and $Total) { |
| 252 | Write-Output " Installed $PackageId. [$Cur/$Total done]" |
| 253 | } |
| 254 | else { |
| 255 | Write-Output " Installed $PackageId." |
| 256 | } |
| 257 | return $true |
| 258 | } catch { |
| 259 | Write-Output " $TestPrefix Warning: session install hit an error for $PackageId : $($_.Exception.Message)" |
| 260 | return $false |
| 261 | } finally { |
| 262 | $ErrorActionPreference = $TestPrevPref |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if ($args.Count -eq 0) { |
| 267 | Show-TestHelp |
| 268 | Write-Output '' |
| 269 | Write-Output 'Error: no modules specified.' |
| 270 | exit 1 |
| 271 | } |
| 272 | foreach ($TestA in $args) { |
| 273 | if ($TestA -eq '--help' -or $TestA -eq '-h' -or $TestA -eq 'help') { |
| 274 | Show-TestHelp |
| 275 | exit 0 |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | $TestRustOnly = $false |
| 280 | $TestUnitOnly = $false |
| 281 | $TestInstrOnly = $false |
| 282 | $TestNoCoverage = $false |
| 283 | $TestDryRun = ($env:TEST_DRY_RUN -eq '1') |
| 284 | $TestDeviceHint = '' |
| 285 | $TestModulesRaw = @() |
| 286 | $TestIdx = 0 |
| 287 | while ($TestIdx -lt $args.Count) { |
| 288 | $TestArg = [string]$args[$TestIdx] |
| 289 | $TestLc = $TestArg.ToLowerInvariant() |
| 290 | if ($TestLc -eq '--rust-only') { $TestRustOnly = $true; $TestIdx++; continue } |
| 291 | elseif ($TestLc -eq '--unit-only') { $TestUnitOnly = $true; $TestIdx++; continue } |
| 292 | elseif ($TestLc -eq '--instrumented-only') { $TestInstrOnly = $true; $TestIdx++; continue } |
| 293 | elseif ($TestLc -eq '--no-coverage') { $TestNoCoverage = $true; $TestIdx++; continue } |
| 294 | elseif ($TestLc -eq '--dry-run') { $TestDryRun = $true; $TestIdx++; continue } |
| 295 | elseif ($TestLc -eq '--help' -or $TestLc -eq '-h' -or $TestLc -eq 'help') { |
| 296 | Show-TestHelp |
| 297 | exit 0 |
| 298 | } |
| 299 | elseif ($TestLc -eq '--device') { |
| 300 | if ($TestIdx + 1 -ge $args.Count) { |
| 301 | Write-Output 'Error: --device requires a value (serial, 8, or 9).' |
| 302 | exit 1 |
| 303 | } |
| 304 | $TestDeviceHint = [string]$args[$TestIdx + 1] |
| 305 | $TestIdx += 2 |
| 306 | continue |
| 307 | } |
| 308 | elseif ($TestLc -like '--device=*') { |
| 309 | $TestDeviceHint = $TestArg.Substring('--device='.Length) |
| 310 | if (-not $TestDeviceHint) { |
| 311 | Write-Output 'Error: --device requires a value (serial, 8, or 9).' |
| 312 | exit 1 |
| 313 | } |
| 314 | $TestIdx++ |
| 315 | continue |
| 316 | } |
| 317 | elseif ($TestLc -eq '8' -or $TestLc -eq '9') { |
| 318 | $TestDeviceHint = $TestLc |
| 319 | $TestIdx++ |
| 320 | continue |
| 321 | } |
| 322 | elseif ($TestLc -match '^[0-9]+$') { |
| 323 | Write-Output "Error: Invalid device '$TestArg'. Use a serial, 8 (Pixel 8), or 9 (Pixel 9)." |
| 324 | exit 1 |
| 325 | } |
| 326 | elseif ($TestLc.StartsWith('--')) { |
| 327 | Write-Output "Error: unknown flag '$TestArg'." |
| 328 | Show-TestHelp |
| 329 | exit 1 |
| 330 | } |
| 331 | else { |
| 332 | $TestModulesRaw += $TestArg |
| 333 | $TestIdx++ |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | $TestScopeCount = 0 |
| 338 | if ($TestRustOnly) { $TestScopeCount++ } |
| 339 | if ($TestUnitOnly) { $TestScopeCount++ } |
| 340 | if ($TestInstrOnly) { $TestScopeCount++ } |
| 341 | if ($TestScopeCount -gt 1) { |
| 342 | Write-Output 'Error: --rust-only, --unit-only, and --instrumented-only are mutually exclusive.' |
| 343 | exit 1 |
| 344 | } |
| 345 | |
| 346 | $TestMods = @() |
| 347 | $TestAllExpanded = $false |
| 348 | foreach ($TestRaw in $TestModulesRaw) { |
| 349 | $TestNorm = Normalize-TestModule ([string]$TestRaw) |
| 350 | if (-not $TestNorm) { continue } |
| 351 | if ($TestNorm -eq 'all') { |
| 352 | if (-not $TestAllExpanded) { |
| 353 | foreach ($TestSm in (Get-TestAllModuleDirs)) { |
| 354 | if ($TestSm -and ($TestMods -notcontains $TestSm)) { $TestMods += $TestSm } |
| 355 | } |
| 356 | $TestAllExpanded = $true |
| 357 | } |
| 358 | continue |
| 359 | } |
| 360 | if (-not $TestNorm.Contains(':')) { |
| 361 | foreach ($TestPfx in @('games', 'personal')) { |
| 362 | if (Test-Path -LiteralPath (Join-Path $TestRoot "$TestPfx/$TestNorm/build.gradle.kts")) { |
| 363 | $TestNorm = "$TestPfx`:$TestNorm" |
| 364 | break |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | $TestFs = $TestNorm.Replace(':', '/') |
| 369 | if (-not (Test-Path -LiteralPath (Join-Path $TestRoot "$TestFs/build.gradle.kts"))) { |
| 370 | [Console]::Error.WriteLine("Error: unknown module '$TestRaw' (-> $TestFs/, no build.gradle.kts).") |
| 371 | exit 1 |
| 372 | } |
| 373 | if ($TestMods -notcontains $TestFs) { $TestMods += $TestFs } |
| 374 | } |
| 375 | if ($TestMods.Count -eq 0) { |
| 376 | [Console]::Error.WriteLine('Error: no valid modules.') |
| 377 | exit 1 |
| 378 | } |
| 379 | |
| 380 | $RunRust = (-not $TestUnitOnly) -and (-not $TestInstrOnly) |
| 381 | $RunUnit = (-not $TestRustOnly) -and (-not $TestInstrOnly) |
| 382 | $RunInstr = (-not $TestRustOnly) -and (-not $TestUnitOnly) |
| 383 | |
| 384 | $TestRootCrates = @() |
| 385 | $TestStandaloneDirs = @() |
| 386 | if ($RunRust) { |
| 387 | foreach ($TestMod in $TestMods) { |
| 388 | try { |
| 389 | $TestRdir = Join-Path $TestRoot "$TestMod/src/main/rust" |
| 390 | $TestCt = Join-Path $TestRdir 'Cargo.toml' |
| 391 | if (Test-Path -LiteralPath $TestCt -PathType Leaf) { |
| 392 | if (Select-String -LiteralPath $TestCt -Pattern '^\[workspace\]\s*$' -Quiet) { |
| 393 | if ($TestStandaloneDirs -notcontains $TestRdir) { $TestStandaloneDirs += $TestRdir } |
| 394 | } |
| 395 | else { |
| 396 | $TestName = $null |
| 397 | foreach ($TestLine in [System.IO.File]::ReadLines($TestCt)) { |
| 398 | if ($TestLine -match '^\s*name\s*=\s*"([^"]+)"') { $TestName = $Matches[1]; break } |
| 399 | } |
| 400 | if ($TestName -and ($TestRootCrates -notcontains $TestName)) { $TestRootCrates += $TestName } |
| 401 | } |
| 402 | } |
| 403 | } catch { |
| 404 | Write-Output "Warning: could not inspect Rust crate for $TestMod : $($_.Exception.Message)" |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | $TestUnitTasks = @() |
| 410 | $TestIsApp = @{} |
| 411 | if ($RunUnit) { |
| 412 | foreach ($TestMod in $TestMods) { |
| 413 | $TestGmod = $TestMod.Replace('/', ':') |
| 414 | $TestBuildFile = Join-Path $TestRoot "$TestMod/build.gradle.kts" |
| 415 | $TestApp = $false |
| 416 | try { |
| 417 | if ((Test-Path -LiteralPath $TestBuildFile) -and (Select-String -LiteralPath $TestBuildFile -Pattern 'common-conventions-app' -Quiet)) { |
| 418 | $TestApp = $true |
| 419 | } |
| 420 | } catch { |
| 421 | $TestApp = $false |
| 422 | } |
| 423 | $TestIsApp[$TestMod] = $TestApp |
| 424 | if ($TestNoCoverage) { |
| 425 | if ($TestApp) { $TestUnitTasks += ":${TestGmod}:testDevUnitTest" } |
| 426 | else { $TestUnitTasks += ":${TestGmod}:testDebugUnitTest" } |
| 427 | } |
| 428 | else { |
| 429 | if ($TestApp) { $TestUnitTasks += ":${TestGmod}:createDevUnitTestCoverageReport" } |
| 430 | else { $TestUnitTasks += ":${TestGmod}:createDebugUnitTestCoverageReport" } |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | $TestInstrMods = @() |
| 436 | if ($RunInstr) { |
| 437 | foreach ($TestMod in $TestMods) { |
| 438 | try { |
| 439 | $TestAtDir = Join-Path $TestRoot "$TestMod/src/androidTest" |
| 440 | if (-not (Test-Path -LiteralPath $TestAtDir)) { continue } |
| 441 | $TestKt = Get-ChildItem -LiteralPath $TestAtDir -Filter '*.kt' -Recurse -File -ErrorAction SilentlyContinue | Select-Object -First 1 |
| 442 | if ($TestKt) { $TestInstrMods += $TestMod } |
| 443 | } catch { |
| 444 | Write-Output "Warning: could not inspect androidTest for $TestMod : $($_.Exception.Message)" |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | $TestInstrTasks = @() |
| 449 | foreach ($TestMod in $TestInstrMods) { |
| 450 | $TestGmod = $TestMod.Replace('/', ':') |
| 451 | $TestInstrTasks += ":${TestGmod}:assembleDev" |
| 452 | $TestInstrTasks += ":${TestGmod}:assembleDevAndroidTest" |
| 453 | } |
| 454 | |
| 455 | $TestAdbBin = Get-TestAdb |
| 456 | $TestTargetSerial = '' |
| 457 | if ($TestAdbBin) { |
| 458 | $TestPrevPref = $ErrorActionPreference |
| 459 | $ErrorActionPreference = 'Continue' |
| 460 | try { |
| 461 | $TestDevLines = & $TestAdbBin devices -l 2>$null |
| 462 | } catch { |
| 463 | $TestDevLines = @() |
| 464 | Write-Output 'Warning: adb devices query failed; continuing without a selected device.' |
| 465 | } |
| 466 | $ErrorActionPreference = $TestPrevPref |
| 467 | foreach ($TestDevLine in @($TestDevLines)) { |
| 468 | $TestParts = ([string]$TestDevLine -split '\s+') |
| 469 | if ($TestParts.Count -lt 2) { continue } |
| 470 | $TestDevSerial = $TestParts[0] |
| 471 | $TestDevState = $TestParts[1] |
| 472 | if ($TestDevState -ne 'device') { continue } |
| 473 | if ($TestDevSerial -like 'emulator*') { continue } |
| 474 | if ($TestDevSerial -eq 'List') { continue } |
| 475 | if (-not $TestDeviceHint) { |
| 476 | $TestTargetSerial = $TestDevSerial |
| 477 | break |
| 478 | } |
| 479 | $TestDevModel = '' |
| 480 | $TestModelM = [regex]::Match([string]$TestDevLine, 'model:([^\s]+)') |
| 481 | if ($TestModelM.Success) { $TestDevModel = $TestModelM.Groups[1].Value } |
| 482 | if (($TestDeviceHint -eq '8') -and ($TestDevModel -eq 'Pixel_8')) { |
| 483 | $TestTargetSerial = $TestDevSerial |
| 484 | break |
| 485 | } |
| 486 | if (($TestDeviceHint -eq '9') -and ($TestDevModel -like 'Pixel_9*')) { |
| 487 | $TestTargetSerial = $TestDevSerial |
| 488 | break |
| 489 | } |
| 490 | if (($TestDeviceHint -ne '8') -and ($TestDeviceHint -ne '9') -and ($TestDevSerial -eq $TestDeviceHint)) { |
| 491 | $TestTargetSerial = $TestDevSerial |
| 492 | break |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | $TestNeedsDevice = ($RunInstr -and ($TestInstrMods.Count -gt 0)) |
| 498 | if ($TestNeedsDevice) { |
| 499 | if (-not $TestAdbBin) { |
| 500 | if ($TestDryRun) { |
| 501 | Write-Output '[DRY-RUN] Warning: adb not found; a device could not be selected.' |
| 502 | } |
| 503 | else { |
| 504 | Write-Output 'Error: adb not found; cannot select a target device.' |
| 505 | Write-Output 'Install Android platform-tools and put adb on PATH (or set ANDROID_HOME).' |
| 506 | exit 1 |
| 507 | } |
| 508 | } |
| 509 | elseif ($TestTargetSerial) { |
| 510 | $env:ANDROID_SERIAL = $TestTargetSerial |
| 511 | if ($TestDeviceHint -eq '8' -or $TestDeviceHint -eq '9') { |
| 512 | Write-Output "Target device: $($env:ANDROID_SERIAL) (Pixel $TestDeviceHint)" |
| 513 | } |
| 514 | elseif ($TestDeviceHint) { |
| 515 | Write-Output "Target device: $($env:ANDROID_SERIAL) (selected via --device)" |
| 516 | } |
| 517 | else { |
| 518 | Write-Output "Target device: $($env:ANDROID_SERIAL) (first connected device not starting with 'emulator')" |
| 519 | } |
| 520 | } |
| 521 | elseif ($TestDeviceHint) { |
| 522 | Write-Output "Error: No connected device matching '$TestDeviceHint' found (check 'adb devices')." |
| 523 | exit 1 |
| 524 | } |
| 525 | elseif ($TestDryRun) { |
| 526 | Write-Output '[DRY-RUN] Warning: no non-emulator device connected; ANDROID_SERIAL not set.' |
| 527 | } |
| 528 | else { |
| 529 | Write-Output 'Error: No non-emulator device connected.' |
| 530 | Write-Output "Connect a physical device (check 'adb devices'); refusing to test on an emulator." |
| 531 | exit 1 |
| 532 | } |
| 533 | } |
| 534 | elseif ($TestTargetSerial) { |
| 535 | $env:ANDROID_SERIAL = $TestTargetSerial |
| 536 | Write-Output "Target device: $($env:ANDROID_SERIAL) (first connected device not starting with 'emulator')" |
| 537 | } |
| 538 | |
| 539 | $TestGradleBase = @('--continue') |
| 540 | $TestGradleBaseStr = ' --continue' |
| 541 | if ($TestAllExpanded) { |
| 542 | $TestGradleBase += '-Dorg.gradle.jvmargs=-Xmx46144m -Xms4512m -XX:MaxMetaspaceSize=1024m -XX:+UseG1GC -Dfile.encoding=UTF-8' |
| 543 | $TestGradleBase += '--max-workers=32' |
| 544 | $TestGradleBaseStr += ' -Dorg.gradle.jvmargs=<46g-big-build-heap> --max-workers=32' |
| 545 | } |
| 546 | |
| 547 | $TestRepoVersion = 0 |
| 548 | $TestNeedBump = 0 |
| 549 | $TestVersionOverrideArg = '' |
| 550 | if ($TestNeedsDevice -and $TestTargetSerial -and (-not $TestDryRun)) { |
| 551 | try { |
| 552 | $TestRepoVersion = [int]([System.IO.File]::ReadLines((Join-Path $TestRoot 'version.txt')) | Select-Object -First 1).Trim() |
| 553 | } catch { |
| 554 | Write-Output 'Warning: could not read version.txt; skipping versionCodeOverride computation.' |
| 555 | $TestRepoVersion = 0 |
| 556 | } |
| 557 | if ($TestRepoVersion -gt 0) { |
| 558 | $TestNeedBump = $TestRepoVersion |
| 559 | $TestPrevPref = $ErrorActionPreference |
| 560 | $ErrorActionPreference = 'Continue' |
| 561 | try { |
| 562 | foreach ($TestMod in $TestInstrMods) { |
| 563 | try { |
| 564 | $TestPkg = Get-TestApplicationId $TestMod |
| 565 | if (-not $TestPkg) { continue } |
| 566 | $TestListOut = & $TestAdbBin -s $TestTargetSerial shell pm list packages --show-versioncode $TestPkg 2>$null |
| 567 | $TestDevV = 0 |
| 568 | foreach ($TestListLine in @($TestListOut)) { |
| 569 | $TestVm = [regex]::Match([string]$TestListLine, 'versionCode:([0-9]+)') |
| 570 | if ($TestVm.Success) { $TestDevV = [int]$TestVm.Groups[1].Value; break } |
| 571 | } |
| 572 | if (($TestDevV -gt 0) -and ($TestDevV -ge $TestNeedBump)) { |
| 573 | $TestSysOut = & $TestAdbBin -s $TestTargetSerial shell pm list packages -s $TestPkg 2>$null |
| 574 | $TestIsSystem = $false |
| 575 | foreach ($TestSysLine in @($TestSysOut)) { |
| 576 | if ([string]$TestSysLine.Trim() -eq "package:$TestPkg") { $TestIsSystem = $true; break } |
| 577 | } |
| 578 | if ($TestIsSystem) { $TestTarget = $TestDevV + 1; $TestKind = 'system app' } |
| 579 | else { $TestTarget = $TestDevV; $TestKind = 'user app' } |
| 580 | if ($TestTarget -gt $TestNeedBump) { |
| 581 | $TestNeedBump = $TestTarget |
| 582 | Write-Output "Info: $TestPkg is a $TestKind at versionCode $TestDevV on-device (>= repo $TestRepoVersion); will build with -PversionCodeOverride=$TestTarget." |
| 583 | } |
| 584 | } |
| 585 | } catch { |
| 586 | Write-Output "Warning: versionCode check failed for $TestMod; continuing without override for it." |
| 587 | } |
| 588 | } |
| 589 | } finally { |
| 590 | $ErrorActionPreference = $TestPrevPref |
| 591 | } |
| 592 | if ($TestNeedBump -gt $TestRepoVersion) { |
| 593 | $TestVersionOverrideArg = "-PversionCodeOverride=$TestNeedBump" |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | $TestUnitArgs = @() + $TestGradleBase |
| 599 | $TestUnitArgsStr = $TestGradleBaseStr |
| 600 | $TestInstrArgs = @() + $TestGradleBase |
| 601 | $TestInstrArgsStr = $TestGradleBaseStr |
| 602 | if ($TestVersionOverrideArg) { |
| 603 | $TestInstrArgs += $TestVersionOverrideArg |
| 604 | $TestInstrArgsStr += " $TestVersionOverrideArg" |
| 605 | } |
| 606 | |
| 607 | $TestGradlewBat = Join-Path $TestRoot 'gradlew.bat' |
| 608 | $TestGradlewSh = Join-Path $TestRoot 'gradlew' |
| 609 | $TestGradlewDisp = if (Test-Path -LiteralPath $TestGradlewBat) { $TestGradlewBat } else { $TestGradlewSh } |
| 610 | |
| 611 | if ($TestDryRun) { |
| 612 | Write-Output "Modules ($($TestMods.Count)): $($TestMods -join ' ')" |
| 613 | if ($TestAllExpanded) { Write-Output '[DRY-RUN] all expanded: big-build heap args will be used.' } |
| 614 | if ($TestDeviceHint) { Write-Output "[DRY-RUN] Device hint: $TestDeviceHint" } |
| 615 | if ($TestTargetSerial) { Write-Output "[DRY-RUN] Target device: $TestTargetSerial" } |
| 616 | Write-Output "[DRY-RUN] Coverage: $(if ($TestNoCoverage) { 'off (--no-coverage)' } else { 'on' })" |
| 617 | Write-Output '' |
| 618 | if ($RunRust) { |
| 619 | if (($TestRootCrates.Count -eq 0) -and ($TestStandaloneDirs.Count -eq 0)) { |
| 620 | Write-Output '[DRY-RUN] Rust: no crates in scope; cargo step skipped.' |
| 621 | } |
| 622 | else { |
| 623 | if ($TestRootCrates.Count -gt 0) { |
| 624 | $TestPFlags = @() |
| 625 | foreach ($TestC in $TestRootCrates) { $TestPFlags += @('-p', $TestC) } |
| 626 | if ($TestNoCoverage) { |
| 627 | if ($TestAllExpanded) { Write-Output "[DRY-RUN] Would execute: cargo test --locked --workspace (in repo root)" } |
| 628 | else { Write-Output "[DRY-RUN] Would execute: cargo test --locked $($TestPFlags -join ' ') (in repo root)" } |
| 629 | } |
| 630 | else { |
| 631 | if ($TestAllExpanded) { Write-Output '[DRY-RUN] Would execute: cargo llvm-cov --workspace --locked --exclude tilecodec --lcov --output-path target/llvm-cov.lcov (in repo root)' } |
| 632 | else { Write-Output "[DRY-RUN] Would execute: cargo llvm-cov --workspace --locked --exclude tilecodec $($TestPFlags -join ' ') --lcov --output-path target/llvm-cov.lcov (in repo root)" } |
| 633 | } |
| 634 | } |
| 635 | foreach ($TestRdir in $TestStandaloneDirs) { |
| 636 | if ($TestNoCoverage) { Write-Output "[DRY-RUN] Would execute: cargo test --locked (in $TestRdir)" } |
| 637 | else { Write-Output "[DRY-RUN] Would execute: cargo llvm-cov --locked --lcov --output-path target/llvm-cov.lcov (in $TestRdir)" } |
| 638 | } |
| 639 | } |
| 640 | Write-Output '' |
| 641 | } |
| 642 | if ($RunUnit) { |
| 643 | Write-Output "[DRY-RUN] Unit tasks: $($TestUnitTasks -join ' ')" |
| 644 | Write-Output "[DRY-RUN] Would execute: $TestGradlewDisp $($TestUnitTasks -join ' ')$TestUnitArgsStr" |
| 645 | Write-Output '[DRY-RUN] Would parse: <module>/build/reports/coverage/test/*/report.xml (INSTRUCTION counters)' |
| 646 | Write-Output '' |
| 647 | } |
| 648 | if ($RunInstr) { |
| 649 | if ($TestInstrMods.Count -eq 0) { |
| 650 | Write-Output '[DRY-RUN] Instrumented: no modules with src/androidTest *.kt in scope; step skipped.' |
| 651 | } |
| 652 | else { |
| 653 | Write-Output "[DRY-RUN] Instrumented modules ($($TestInstrMods.Count)): $($TestInstrMods -join ' ')" |
| 654 | Write-Output "[DRY-RUN] Would execute: $TestGradlewDisp $($TestInstrTasks -join ' ')$TestInstrArgsStr" |
| 655 | foreach ($TestMod in $TestInstrMods) { |
| 656 | $TestPkg = Get-TestApplicationId $TestMod |
| 657 | $TestFs = $TestMod.Replace(':', '/') |
| 658 | $TestAppApk = Get-TestBuiltApk $TestMod 'dev' |
| 659 | $TestTApk = Get-TestAndroidTestApk $TestMod |
| 660 | if ($TestAppApk) { Write-Output "[DRY-RUN] Would session-install $TestPkg from $TestAppApk (with .idsig sidecar)" } |
| 661 | else { Write-Output "[DRY-RUN] Would session-install $TestPkg (APK expected after assembleDev in $TestFs/build/outputs/apk/dev)" } |
| 662 | if ($TestTApk) { Write-Output "[DRY-RUN] Would run: adb install -r $TestTApk" } |
| 663 | else { Write-Output "[DRY-RUN] Would run: adb install -r $TestFs/build/outputs/apk/androidTest/dev/<test-apk> (expected after assembleDevAndroidTest)" } |
| 664 | if ($TestPkg) { |
| 665 | if ($TestNoCoverage) { Write-Output "[DRY-RUN] Would run: adb -s <serial> shell am instrument -w -r $TestPkg.test/androidx.test.runner.AndroidJUnitRunner" } |
| 666 | else { |
| 667 | Write-Output "[DRY-RUN] Would run: adb -s <serial> shell am instrument -w -r -e coverage true -e coverageFilePath /data/data/$TestPkg/files/coverage.ec $TestPkg.test/androidx.test.runner.AndroidJUnitRunner" |
| 668 | Write-Output "[DRY-RUN] Would pull: adb -s <serial> exec-out run-as $TestPkg cat files/coverage.ec -> $TestFs/build/outputs/coverage-manual/$TestPkg.ec" |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | Write-Output '' |
| 674 | } |
| 675 | exit 0 |
| 676 | } |
| 677 | |
| 678 | Write-Output "Modules ($($TestMods.Count)): $($TestMods -join ' ')" |
| 679 | if ($RunUnit -and ($TestUnitTasks.Count -gt 0)) { Write-Output "Unit tasks: $($TestUnitTasks -join ' ')" } |
| 680 | if ($RunInstr -and ($TestInstrTasks.Count -gt 0)) { Write-Output "Instrumented tasks: $($TestInstrTasks -join ' ')" } |
| 681 | Write-Output '' |
| 682 | |
| 683 | $TestFailures = 0 |
| 684 | |
| 685 | if ($RunRust) { |
| 686 | if (($TestRootCrates.Count -eq 0) -and ($TestStandaloneDirs.Count -eq 0)) { |
| 687 | Write-Output 'No Rust crates in scope; cargo step skipped.' |
| 688 | } |
| 689 | else { |
| 690 | foreach ($TestRc in $TestRootCrates) { Write-Output "Rust: workspace crate $TestRc" } |
| 691 | foreach ($TestRdir in $TestStandaloneDirs) { Write-Output "Rust: standalone crate ($TestRdir)" } |
| 692 | $TestCargoBin = Get-TestCargoBin |
| 693 | if (-not $TestCargoBin) { |
| 694 | Write-Output 'Warning: cargo not found (install Rust or set CARGO_HOME); Rust step skipped.' |
| 695 | $TestFailures++ |
| 696 | } |
| 697 | else { |
| 698 | $TestCovOk = (-not $TestNoCoverage) |
| 699 | if ($TestCovOk) { |
| 700 | try { |
| 701 | $TestPrevPref = $ErrorActionPreference |
| 702 | $ErrorActionPreference = 'Continue' |
| 703 | try { |
| 704 | & rustup component add llvm-tools-preview 2>&1 | Out-Null |
| 705 | $TestRustupRc = $LASTEXITCODE |
| 706 | } catch { |
| 707 | $TestRustupRc = 1 |
| 708 | } |
| 709 | if ($TestRustupRc -ne 0) { throw "rustup component add failed (exit $TestRustupRc)" } |
| 710 | try { |
| 711 | & $TestCargoBin install cargo-llvm-cov --locked 2>&1 | Out-Null |
| 712 | $TestCovInstallRc = $LASTEXITCODE |
| 713 | } catch { |
| 714 | $TestCovInstallRc = 1 |
| 715 | } |
| 716 | if ($TestCovInstallRc -ne 0) { throw "cargo install cargo-llvm-cov failed (exit $TestCovInstallRc)" } |
| 717 | $ErrorActionPreference = $TestPrevPref |
| 718 | } catch { |
| 719 | Write-Output "Warning: coverage tools unavailable ($($_.Exception.Message)); running plain cargo test instead." |
| 720 | $TestCovOk = $false |
| 721 | $ErrorActionPreference = $TestPrevPref |
| 722 | } |
| 723 | } |
| 724 | if ($TestCovOk) { |
| 725 | if ($TestRootCrates.Count -gt 0) { |
| 726 | Write-Output '' |
| 727 | $TestPFlags = @() |
| 728 | foreach ($TestC in $TestRootCrates) { $TestPFlags += @('-p', $TestC) } |
| 729 | try { |
| 730 | $TestPrevPref = $ErrorActionPreference |
| 731 | $ErrorActionPreference = 'Continue' |
| 732 | Push-Location -LiteralPath $TestRoot |
| 733 | try { |
| 734 | if ($TestAllExpanded) { |
| 735 | Write-Output 'Running: cargo llvm-cov --workspace --locked --exclude tilecodec --lcov --output-path target/llvm-cov.lcov' |
| 736 | & $TestCargoBin llvm-cov --workspace --locked --exclude tilecodec --lcov --output-path target/llvm-cov.lcov |
| 737 | } |
| 738 | else { |
| 739 | Write-Output "Running: cargo llvm-cov --workspace --locked --exclude tilecodec $($TestPFlags -join ' ') --lcov --output-path target/llvm-cov.lcov" |
| 740 | & $TestCargoBin llvm-cov --workspace --locked --exclude tilecodec @TestPFlags --lcov --output-path target/llvm-cov.lcov |
| 741 | } |
| 742 | $TestRc2 = $LASTEXITCODE |
| 743 | } finally { |
| 744 | Pop-Location |
| 745 | $ErrorActionPreference = $TestPrevPref |
| 746 | } |
| 747 | if ($TestRc2 -ne 0) { |
| 748 | Write-Output "Warning: cargo llvm-cov (workspace) reported failures (exit $TestRc2)." |
| 749 | $TestFailures++ |
| 750 | } |
| 751 | } catch { |
| 752 | Write-Output "Warning: cargo llvm-cov (workspace) hit an error : $($_.Exception.Message)" |
| 753 | $TestFailures++ |
| 754 | } |
| 755 | } |
| 756 | foreach ($TestRdir in $TestStandaloneDirs) { |
| 757 | Write-Output '' |
| 758 | Write-Output "Running: cargo llvm-cov --locked --lcov --output-path target/llvm-cov.lcov (in $TestRdir)" |
| 759 | try { |
| 760 | $TestPrevPref = $ErrorActionPreference |
| 761 | $ErrorActionPreference = 'Continue' |
| 762 | Push-Location -LiteralPath $TestRdir |
| 763 | try { |
| 764 | & $TestCargoBin llvm-cov --locked --lcov --output-path target/llvm-cov.lcov |
| 765 | $TestRc2 = $LASTEXITCODE |
| 766 | } finally { |
| 767 | Pop-Location |
| 768 | $ErrorActionPreference = $TestPrevPref |
| 769 | } |
| 770 | if ($TestRc2 -ne 0) { |
| 771 | Write-Output "Warning: cargo llvm-cov ($TestRdir) reported failures (exit $TestRc2)." |
| 772 | $TestFailures++ |
| 773 | } |
| 774 | } catch { |
| 775 | Write-Output "Warning: cargo llvm-cov ($TestRdir) hit an error : $($_.Exception.Message)" |
| 776 | $TestFailures++ |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | else { |
| 781 | if ($TestRootCrates.Count -gt 0) { |
| 782 | Write-Output '' |
| 783 | $TestPFlags = @() |
| 784 | foreach ($TestC in $TestRootCrates) { $TestPFlags += @('-p', $TestC) } |
| 785 | try { |
| 786 | $TestPrevPref = $ErrorActionPreference |
| 787 | $ErrorActionPreference = 'Continue' |
| 788 | Push-Location -LiteralPath $TestRoot |
| 789 | try { |
| 790 | if ($TestAllExpanded) { |
| 791 | Write-Output 'Running: cargo test --locked --workspace' |
| 792 | & $TestCargoBin test --locked --workspace |
| 793 | } |
| 794 | else { |
| 795 | Write-Output "Running: cargo test --locked $($TestPFlags -join ' ')" |
| 796 | & $TestCargoBin test --locked @TestPFlags |
| 797 | } |
| 798 | $TestRc2 = $LASTEXITCODE |
| 799 | } finally { |
| 800 | Pop-Location |
| 801 | $ErrorActionPreference = $TestPrevPref |
| 802 | } |
| 803 | if ($TestRc2 -ne 0) { |
| 804 | Write-Output "Warning: cargo test (workspace) reported failures (exit $TestRc2)." |
| 805 | $TestFailures++ |
| 806 | } |
| 807 | } catch { |
| 808 | Write-Output "Warning: cargo test (workspace) hit an error : $($_.Exception.Message)" |
| 809 | $TestFailures++ |
| 810 | } |
| 811 | } |
| 812 | foreach ($TestRdir in $TestStandaloneDirs) { |
| 813 | Write-Output '' |
| 814 | Write-Output "Running: cargo test --locked (in $TestRdir)" |
| 815 | try { |
| 816 | $TestPrevPref = $ErrorActionPreference |
| 817 | $ErrorActionPreference = 'Continue' |
| 818 | Push-Location -LiteralPath $TestRdir |
| 819 | try { |
| 820 | & $TestCargoBin test --locked |
| 821 | $TestRc2 = $LASTEXITCODE |
| 822 | } finally { |
| 823 | Pop-Location |
| 824 | $ErrorActionPreference = $TestPrevPref |
| 825 | } |
| 826 | if ($TestRc2 -ne 0) { |
| 827 | Write-Output "Warning: cargo test ($TestRdir) reported failures (exit $TestRc2)." |
| 828 | $TestFailures++ |
| 829 | } |
| 830 | } catch { |
| 831 | Write-Output "Warning: cargo test ($TestRdir) hit an error : $($_.Exception.Message)" |
| 832 | $TestFailures++ |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | Write-Output '' |
| 839 | } |
| 840 | |
| 841 | if ($RunUnit) { |
| 842 | Write-Output "Running unit tests: $($TestUnitTasks -join ' ')" |
| 843 | $TestBashCmd = Get-Command bash -ErrorAction SilentlyContinue |
| 844 | $TestUnitExit = 0 |
| 845 | try { |
| 846 | $TestPrevPref = $ErrorActionPreference |
| 847 | $ErrorActionPreference = 'Continue' |
| 848 | try { |
| 849 | if (Test-Path -LiteralPath $TestGradlewBat) { |
| 850 | Write-Output "Running: $TestGradlewBat $($TestUnitTasks -join ' ')$TestUnitArgsStr" |
| 851 | Write-Output '' |
| 852 | & $TestGradlewBat @TestUnitTasks @TestUnitArgs |
| 853 | $TestUnitExit = $LASTEXITCODE |
| 854 | } |
| 855 | elseif ($TestBashCmd -and (Test-Path -LiteralPath $TestGradlewSh)) { |
| 856 | Write-Output "Running: $TestGradlewSh $($TestUnitTasks -join ' ')$TestUnitArgsStr" |
| 857 | Write-Output '' |
| 858 | & $TestBashCmd.Source $TestGradlewSh @TestUnitTasks @TestUnitArgs |
| 859 | $TestUnitExit = $LASTEXITCODE |
| 860 | } |
| 861 | else { |
| 862 | Write-Output 'Error: no Gradle wrapper found (gradlew.bat / gradlew).' |
| 863 | $TestUnitExit = 1 |
| 864 | } |
| 865 | } finally { |
| 866 | $ErrorActionPreference = $TestPrevPref |
| 867 | } |
| 868 | } catch { |
| 869 | Write-Output "Warning: unit-test invocation hit an error : $($_.Exception.Message)" |
| 870 | $TestUnitExit = 1 |
| 871 | } |
| 872 | Write-Output '' |
| 873 | if ($TestUnitExit -ne 0) { |
| 874 | Write-Output "Warning: unit tests reported failures (exit $TestUnitExit)." |
| 875 | $TestFailures++ |
| 876 | } |
| 877 | if (-not $TestNoCoverage) { |
| 878 | $TestTotalCovered = 0 |
| 879 | $TestTotalMissed = 0 |
| 880 | foreach ($TestMod in $TestMods) { |
| 881 | try { |
| 882 | $TestCovBase = Join-Path $TestRoot "$TestMod/build/reports/coverage/test" |
| 883 | if (-not (Test-Path -LiteralPath $TestCovBase)) { |
| 884 | Write-Output "Warning: $TestMod no coverage data (no tests?)" |
| 885 | continue |
| 886 | } |
| 887 | $TestReports = @(Get-ChildItem -Path $TestCovBase -Filter 'report.xml' -Recurse -File -ErrorAction SilentlyContinue) |
| 888 | if ($TestReports.Count -eq 0) { |
| 889 | Write-Output "Warning: $TestMod no coverage data (no tests?)" |
| 890 | continue |
| 891 | } |
| 892 | $TestModCovered = 0 |
| 893 | $TestModMissed = 0 |
| 894 | foreach ($TestRep in $TestReports) { |
| 895 | try { |
| 896 | [xml]$TestXml = Get-Content -LiteralPath $TestRep.FullName |
| 897 | $TestCounters = @($TestXml.report.counter | Where-Object { $_.type -eq 'INSTRUCTION' }) |
| 898 | if ($TestCounters.Count -gt 0) { |
| 899 | $TestModCovered += [int]$TestCounters[0].covered |
| 900 | $TestModMissed += [int]$TestCounters[0].missed |
| 901 | } |
| 902 | else { |
| 903 | $TestNode = $TestXml.SelectSingleNode("//counter[@type='INSTRUCTION']") |
| 904 | if ($TestNode) { |
| 905 | $TestModCovered += [int]$TestNode.covered |
| 906 | $TestModMissed += [int]$TestNode.missed |
| 907 | } |
| 908 | } |
| 909 | } catch { |
| 910 | Write-Output "Warning: could not parse $($TestRep.FullName); skipping it." |
| 911 | } |
| 912 | } |
| 913 | Write-Output "Coverage $TestMod : covered $TestModCovered missed $TestModMissed" |
| 914 | $TestTotalCovered += $TestModCovered |
| 915 | $TestTotalMissed += $TestModMissed |
| 916 | } catch { |
| 917 | Write-Output "Warning: coverage parse failed for $TestMod; continuing." |
| 918 | } |
| 919 | } |
| 920 | Write-Output "Coverage TOTAL : covered $TestTotalCovered missed $TestTotalMissed" |
| 921 | } |
| 922 | Write-Output '' |
| 923 | } |
| 924 | |
| 925 | if ($RunInstr) { |
| 926 | if ($TestInstrMods.Count -eq 0) { |
| 927 | Write-Output 'No instrumented tests in scope (no src/androidTest *.kt); instrumented step skipped.' |
| 928 | } |
| 929 | else { |
| 930 | Write-Output "Running instrumented assemble: $($TestInstrTasks -join ' ')" |
| 931 | $TestBashCmd2 = Get-Command bash -ErrorAction SilentlyContinue |
| 932 | $TestInstrBuildExit = 0 |
| 933 | try { |
| 934 | $TestPrevPref = $ErrorActionPreference |
| 935 | $ErrorActionPreference = 'Continue' |
| 936 | try { |
| 937 | if (Test-Path -LiteralPath $TestGradlewBat) { |
| 938 | Write-Output "Running: $TestGradlewBat $($TestInstrTasks -join ' ')$TestInstrArgsStr" |
| 939 | Write-Output '' |
| 940 | & $TestGradlewBat @TestInstrTasks @TestInstrArgs |
| 941 | $TestInstrBuildExit = $LASTEXITCODE |
| 942 | } |
| 943 | elseif ($TestBashCmd2 -and (Test-Path -LiteralPath $TestGradlewSh)) { |
| 944 | Write-Output "Running: $TestGradlewSh $($TestInstrTasks -join ' ')$TestInstrArgsStr" |
| 945 | Write-Output '' |
| 946 | & $TestBashCmd2.Source $TestGradlewSh @TestInstrTasks @TestInstrArgs |
| 947 | $TestInstrBuildExit = $LASTEXITCODE |
| 948 | } |
| 949 | else { |
| 950 | Write-Output 'Error: no Gradle wrapper found (gradlew.bat / gradlew).' |
| 951 | $TestInstrBuildExit = 1 |
| 952 | } |
| 953 | } finally { |
| 954 | $ErrorActionPreference = $TestPrevPref |
| 955 | } |
| 956 | } catch { |
| 957 | Write-Output "Warning: instrumented assemble hit an error : $($_.Exception.Message)" |
| 958 | $TestInstrBuildExit = 1 |
| 959 | } |
| 960 | Write-Output '' |
| 961 | if ($TestInstrBuildExit -ne 0) { |
| 962 | Write-Output 'Warning: assemble reported failures; running instrumented tests for whatever APKs exist.' |
| 963 | $TestFailures++ |
| 964 | } |
| 965 | $TestTotal = $TestInstrMods.Count |
| 966 | Write-Output '' |
| 967 | Write-Output "Running instrumented tests for $TestTotal module(s) on $TestTargetSerial..." |
| 968 | $TestCur = 0 |
| 969 | foreach ($TestMod in $TestInstrMods) { |
| 970 | $TestCur++ |
| 971 | try { |
| 972 | $TestPkg = Get-TestApplicationId $TestMod |
| 973 | if (-not $TestPkg) { |
| 974 | Write-Output "[$TestCur/$TestTotal] Error: no applicationId found for $TestMod." |
| 975 | $TestFailures++ |
| 976 | continue |
| 977 | } |
| 978 | $TestAppApk = Get-TestBuiltApk $TestMod 'dev' |
| 979 | if (-not $TestAppApk) { |
| 980 | Write-Output "[$TestCur/$TestTotal] Error: no app APK found for $TestMod (assemble failed?)." |
| 981 | $TestFailures++ |
| 982 | continue |
| 983 | } |
| 984 | $TestTApk = Get-TestAndroidTestApk $TestMod |
| 985 | if (-not $TestTApk) { |
| 986 | Write-Output "[$TestCur/$TestTotal] Error: no test APK found for $TestMod (assembleDevAndroidTest failed?)." |
| 987 | $TestFailures++ |
| 988 | continue |
| 989 | } |
| 990 | if (-not (Test-ApkViaSession $TestAppApk $TestPkg $TestAdbBin $TestTargetSerial "$TestCur" "$TestTotal")) { |
| 991 | $TestFailures++ |
| 992 | continue |
| 993 | } |
| 994 | Write-Output "[$TestCur/$TestTotal] Installing test APK for $TestPkg..." |
| 995 | $TestPrevPref = $ErrorActionPreference |
| 996 | $ErrorActionPreference = 'Continue' |
| 997 | try { |
| 998 | & $TestAdbBin -s $TestTargetSerial install -r $TestTApk 2>&1 | Out-Null |
| 999 | $TestTestInstallRc = $LASTEXITCODE |
| 1000 | } catch { |
| 1001 | $TestTestInstallRc = 1 |
| 1002 | } finally { |
| 1003 | $ErrorActionPreference = $TestPrevPref |
| 1004 | } |
| 1005 | if ($TestTestInstallRc -ne 0) { |
| 1006 | Write-Output "[$TestCur/$TestTotal] Error: test APK install failed for $TestPkg." |
| 1007 | $TestFailures++ |
| 1008 | continue |
| 1009 | } |
| 1010 | $TestRunner = "$TestPkg.test/androidx.test.runner.AndroidJUnitRunner" |
| 1011 | $TestPrevPref = $ErrorActionPreference |
| 1012 | $ErrorActionPreference = 'Continue' |
| 1013 | try { |
| 1014 | if ($TestNoCoverage) { |
| 1015 | Write-Output "[$TestCur/$TestTotal] Running: am instrument -w -r $TestRunner" |
| 1016 | $TestInstrOut = & $TestAdbBin -s $TestTargetSerial shell am instrument -w -r $TestRunner 2>&1 |
| 1017 | } |
| 1018 | else { |
| 1019 | $TestCovDevicePath = "/data/data/$TestPkg/files/coverage.ec" |
| 1020 | Write-Output "[$TestCur/$TestTotal] Running: am instrument -w -r -e coverage true $TestRunner" |
| 1021 | $TestInstrOut = & $TestAdbBin -s $TestTargetSerial shell am instrument -w -r -e coverage true -e coverageFilePath $TestCovDevicePath $TestRunner 2>&1 |
| 1022 | } |
| 1023 | $TestInstrRc = $LASTEXITCODE |
| 1024 | $TestInstrText = ($TestInstrOut -join "`n") |
| 1025 | } catch { |
| 1026 | $TestInstrOut = @("$($_.Exception.Message)") |
| 1027 | $TestInstrText = ($TestInstrOut -join "`n") |
| 1028 | $TestInstrRc = 1 |
| 1029 | } finally { |
| 1030 | $ErrorActionPreference = $TestPrevPref |
| 1031 | } |
| 1032 | $TestInstrFailed = $false |
| 1033 | if ($TestInstrRc -ne 0) { $TestInstrFailed = $true } |
| 1034 | if ($TestInstrText -match '(?im)^(FAILURES|INSTRUMENTATION_FAILED|INSTRUMENTATION_CODE:\s*0)') { $TestInstrFailed = $true } |
| 1035 | if ($TestInstrText -match '(?im)FAILURES!!!') { $TestInstrFailed = $true } |
| 1036 | Write-Output $TestInstrText |
| 1037 | if ($TestInstrFailed) { |
| 1038 | Write-Output "[$TestCur/$TestTotal] Error: instrumented tests failed for $TestPkg." |
| 1039 | $TestFailures++ |
| 1040 | continue |
| 1041 | } |
| 1042 | if (-not $TestNoCoverage) { |
| 1043 | try { |
| 1044 | $TestFsPath = $TestMod.Replace(':', '/') |
| 1045 | $TestCovOutDir = Join-Path $TestRoot "$TestFsPath/build/outputs/coverage-manual" |
| 1046 | try { New-Item -ItemType Directory -Force -Path $TestCovOutDir >$null 2>&1 } catch { } |
| 1047 | $TestCovOutFile = Join-Path $TestCovOutDir "$TestPkg.ec" |
| 1048 | $TestPrevPref = $ErrorActionPreference |
| 1049 | $ErrorActionPreference = 'Continue' |
| 1050 | try { |
| 1051 | & $TestAdbBin -s $TestTargetSerial exec-out run-as $TestPkg cat files/coverage.ec > $TestCovOutFile 2>$null |
| 1052 | $TestPullRc = $LASTEXITCODE |
| 1053 | } catch { |
| 1054 | $TestPullRc = 1 |
| 1055 | } finally { |
| 1056 | $ErrorActionPreference = $TestPrevPref |
| 1057 | } |
| 1058 | if (($TestPullRc -eq 0) -and (Test-Path -LiteralPath $TestCovOutFile -PathType Leaf)) { |
| 1059 | Write-Output "Coverage pulled: $TestCovOutFile" |
| 1060 | } |
| 1061 | else { |
| 1062 | Write-Output "Warning: could not pull coverage.ec for $TestPkg." |
| 1063 | } |
| 1064 | } catch { |
| 1065 | Write-Output "Warning: coverage pull failed for $TestPkg; continuing." |
| 1066 | } |
| 1067 | } |
| 1068 | Write-Output "[$TestCur/$TestTotal] Instrumented tests passed for $TestPkg." |
| 1069 | } catch { |
| 1070 | Write-Output "[$TestCur/$TestTotal] Warning: instrumented run hit an error for $TestMod : $($_.Exception.Message)" |
| 1071 | $TestFailures++ |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | Write-Output '' |
| 1076 | } |
| 1077 | |
| 1078 | if ($TestFailures -gt 0) { |
| 1079 | Write-Output "Test complete: failures in $TestFailures step(s)." |
| 1080 | exit 1 |
| 1081 | } |
| 1082 | Write-Output 'Test complete: all executed steps succeeded.' |
| 1083 | exit 0 |