您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

162 行
8.7KB

  1. <#
  2. .SYNOPSIS
  3. Build each target, run it headless in VICE, and check that the expected screen appears.
  4. .DESCRIPTION
  5. For every target in project.json (resolved.targets) that has a "smoke" section:
  6. 1. builds the artifact (and disk image, if any);
  7. 2. starts VICE minimized, in warp mode, with sound off and a private config file,
  8. so your own VICE settings are never read or changed;
  9. 3. sets non-stopping monitor tracepoints on the program's "ready" point. When the
  10. program reaches it, VICE saves the listed memory ranges (screen RAM, a VDC probe,
  11. mode flags) to build/smoke/<target>/*.bin;
  12. 4. lets VICE quit after a fixed number of cycles (-limitcycles) and saves a screenshot;
  13. 5. compares the saved memory with the expected text and bytes.
  14. Exit codes: 0 = all automated checks passed, 1 = a check failed, 2 = a tool is missing,
  15. 3 = nothing automated to run (manual verification only).
  16. .EXAMPLE
  17. pwsh -File tools/smoke.ps1
  18. pwsh -File tools/smoke.ps1 -Video ntsc # run the same checks on an NTSC machine
  19. pwsh -File tools/smoke.ps1 -Target c64
  20. #>
  21. [CmdletBinding()]
  22. param(
  23. [string]$Target,
  24. [switch]$NoBuild,
  25. [ValidateSet('pal', 'ntsc')][string]$Video,
  26. [int]$TimeoutSeconds = 180
  27. )
  28. . (Join-Path $PSScriptRoot 'lib.ps1')
  29. function Resolve-SmokeAddress([hashtable]$Target, [string]$Symbol, [string]$Address) {
  30. if ($Address) { return [Convert]::ToInt32($Address, 16) }
  31. $labels = Get-RepoPath $Target.build.labels
  32. if (-not (Test-Path -LiteralPath $labels)) { throw "Label file $($Target.build.labels) not found; cannot resolve symbol '$Symbol'." }
  33. foreach ($line in Get-Content -LiteralPath $labels) {
  34. $parts = $line -split '\s+'
  35. if ($parts.Count -ge 3 -and $parts[2].TrimStart('.') -eq $Symbol.TrimStart('.')) { return [Convert]::ToInt32($parts[1], 16) }
  36. }
  37. throw "Symbol '$Symbol' not found in $($Target.build.labels). Keep the smoke hook in the source (see AGENTS.md)."
  38. }
  39. function ConvertFrom-ScreenCodes([byte[]]$Codes, [string]$Encoding) {
  40. $chars = foreach ($b in $Codes) {
  41. $c = $b -band 0x7F
  42. if ($c -eq 0) { '@' }
  43. elseif ($c -ge 1 -and $c -le 26) { if ($Encoding -eq 'screen-mixed') { [char](0x60 + $c) } else { [char](0x40 + $c) } }
  44. elseif ($c -ge 0x1B -and $c -le 0x1F) { [char](0x40 + $c) }
  45. elseif ($c -ge 0x20 -and $c -le 0x3F) { [char]$c }
  46. elseif ($c -ge 0x41 -and $c -le 0x5A -and $Encoding -eq 'screen-mixed') { [char]$c }
  47. else { '.' }
  48. }
  49. -join $chars
  50. }
  51. function Invoke-TargetSmoke([hashtable]$Project, [hashtable]$Target) {
  52. $smoke = $Target.smoke
  53. $dir = Get-RepoPath "build/smoke/$($Target.id)"
  54. if (Test-Path -LiteralPath $dir) { Remove-Item -LiteralPath $dir -Recurse -Force }
  55. New-Item -ItemType Directory -Force -Path $dir | Out-Null
  56. $readyAddr = Resolve-SmokeAddress $Target $smoke.ready.symbol $smoke.ready.address
  57. $mon = [System.Collections.Generic.List[string]]::new()
  58. $n = 0
  59. foreach ($d in @($smoke.dumps)) {
  60. $n++
  61. $start = Resolve-SmokeAddress $Target $d.symbol $d.address
  62. $end = $start + [int]$d.length - 1
  63. $mon.Add(('trace {0} ${1:x4}' -f $smoke.ready.kind, $readyAddr))
  64. $mon.Add(('command {0} "bsave \"{1}.bin\" 0 {2:x4} {3:x4}"' -f $n, $d.name, $start, $end))
  65. }
  66. Set-Content -LiteralPath (Join-Path $dir 'monitor.txt') -Value $mon
  67. Set-Content -LiteralPath (Join-Path $dir 'vicerc') -Value ''
  68. $emu = Get-ToolPath $Project $Target.emulator.binary
  69. $vargs = @('-config', 'vicerc', '-default', '-sounddev', 'dummy', '-warp', '-minimized') + (Get-VideoArgs $Target) + @($Target.emulator.args)
  70. $vargs += @('-limitcycles', [string]$smoke.limitCycles, '-exitscreenshot', 'screen.png')
  71. if ($Target.emulator.binary -eq 'x128') { $vargs += @('-exitscreenshotvicii', 'vicii.png') }
  72. $vargs += @('-moncommands', 'monitor.txt')
  73. if ($Target.emulator.autostart -eq 'disk') {
  74. $vargs += @('-autostart', (Get-AutostartFile $Target))
  75. } else {
  76. if ($Target.disk) { $vargs += @('-8', (Get-RepoPath $Target.disk.image)) }
  77. $vargs += @('-autostartprgmode', '1', '-autostart', (Get-AutostartFile $Target))
  78. }
  79. $quoted = $vargs | ForEach-Object { if ($_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ } }
  80. Set-Content -LiteralPath (Join-Path $dir 'command.txt') -Value ("`"$emu`" " + ($quoted -join ' '))
  81. Write-Host "[$($Target.id)] running $($Target.emulator.binary) headless (limit $($smoke.limitCycles) cycles)"
  82. $sw = [Diagnostics.Stopwatch]::StartNew()
  83. $p = Start-Process -FilePath $emu -ArgumentList $quoted -WorkingDirectory $dir -PassThru `
  84. -RedirectStandardOutput (Join-Path $dir 'vice.log') -RedirectStandardError (Join-Path $dir 'vice.err')
  85. if (-not $p.WaitForExit($TimeoutSeconds * 1000)) {
  86. $p.Kill($true)
  87. return @{ target = $Target.id; status = 'FAIL'; seconds = [int]$sw.Elapsed.TotalSeconds; checks = @(); message = "VICE did not exit within $TimeoutSeconds s (killed)." }
  88. }
  89. $checks = @()
  90. $reached = @($smoke.dumps | Where-Object { Test-Path -LiteralPath (Join-Path $dir "$($_.name).bin") }).Count -gt 0
  91. if (-not $reached) {
  92. $where = if ($smoke.ready.symbol) { "symbol $($smoke.ready.symbol)" } else { "store to `$$($smoke.ready.address)" }
  93. return @{ target = $Target.id; status = 'FAIL'; seconds = [int]$sw.Elapsed.TotalSeconds; checks = @(); message = "Program never reached its ready point ($where) within $($smoke.limitCycles) cycles. See build/smoke/$($Target.id)/vice.log and screen.png." }
  94. }
  95. foreach ($c in @($smoke.checks)) {
  96. $file = Join-Path $dir "$($c.dump).bin"
  97. $result = @{ description = $c.description; pass = $false; detail = '' }
  98. if (-not (Test-Path -LiteralPath $file)) { $result.detail = "dump '$($c.dump)' missing"; $checks += $result; continue }
  99. $data = [IO.File]::ReadAllBytes($file)
  100. $expected = if ($null -ne $c.text) { ConvertTo-ScreenCodes $c.text $c.encoding } else { [byte[]]($c.bytes -split '\s+' | Where-Object { $_ } | ForEach-Object { [Convert]::ToByte($_, 16) }) }
  101. $offset = [int]$c.offset
  102. if ($offset + $expected.Length -gt $data.Length) { $result.detail = "dump '$($c.dump)' too short"; $checks += $result; continue }
  103. $actual = [byte[]]$data[$offset..($offset + $expected.Length - 1)]
  104. $result.pass = ((Format-Hex $actual) -eq (Format-Hex $expected))
  105. if ($null -ne $c.text) {
  106. $result.detail = "expected '$($c.text)', found '$(ConvertFrom-ScreenCodes $actual $c.encoding)'"
  107. } else {
  108. $result.detail = "expected $(Format-Hex $expected), found $(Format-Hex $actual)"
  109. }
  110. $checks += $result
  111. }
  112. $status = if (@($checks | Where-Object { -not $_.pass }).Count -eq 0) { 'PASS' } else { 'FAIL' }
  113. $shots = @('screen.png', 'vicii.png') | Where-Object { Test-Path -LiteralPath (Join-Path $dir $_) } | ForEach-Object { "build/smoke/$($Target.id)/$_" }
  114. @{ target = $Target.id; status = $status; seconds = [int]$sw.Elapsed.TotalSeconds; checks = $checks; screenshots = @($shots); message = $smoke.expectedScreen }
  115. }
  116. $exitCode = 0
  117. try {
  118. $project = Get-ProjectConfig
  119. $targets = Get-ProjectTargets $project $Target
  120. $done = @{}
  121. $results = @()
  122. foreach ($t in $targets) {
  123. if ($Video) { $t.video = $Video }
  124. if (-not $NoBuild) { Invoke-TargetBuild $project $t $done }
  125. if (-not $t.smoke) {
  126. $results += @{ target = $t.id; status = 'MANUAL'; checks = @(); message = 'No automated smoke check for this target; follow docs/manual-tests.md (M0).' }
  127. continue
  128. }
  129. $results += Invoke-TargetSmoke $project $t
  130. }
  131. } catch [MissingToolException] {
  132. Write-Host ""
  133. Write-Host $_.Exception.Message -ForegroundColor Yellow
  134. exit 2
  135. }
  136. Write-Host ""
  137. Write-Host "Smoke results"
  138. foreach ($r in $results) {
  139. $color = switch ($r.status) { 'PASS' { 'Green' } 'FAIL' { 'Red' } default { 'Yellow' } }
  140. Write-Host (" [{0}] {1}" -f $r.status, $r.target) -ForegroundColor $color
  141. foreach ($c in @($r.checks)) { Write-Host (" {0} {1}: {2}" -f ($(if ($c.pass) { 'ok ' } else { 'FAIL' })), $c.description, $c.detail) }
  142. if ($r.ContainsKey('screenshots') -and $r.screenshots) { Write-Host (" screenshot: {0}" -f ($r.screenshots -join ', ')) }
  143. if ($r.message) { Write-Host " $($r.message)" }
  144. }
  145. New-Item -ItemType Directory -Force -Path (Get-RepoPath 'build/smoke') | Out-Null
  146. $results | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Get-RepoPath 'build/smoke/results.json')
  147. if (@($results | Where-Object { $_.status -eq 'FAIL' }).Count -gt 0) { $exitCode = 1 }
  148. elseif (@($results | Where-Object { $_.status -eq 'PASS' }).Count -eq 0) { $exitCode = 3 }
  149. exit $exitCode

Powered by TurnKey Linux.