From e0741832d30e063c47b5d7c8bde7e69810956c12 Mon Sep 17 00:00:00 2001 From: subtrack Date: Sun, 18 Oct 2026 19:46:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(test):=20=E6=B5=8B=E8=AF=95=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=94=B9=E4=B8=BA=E4=BD=8D=E7=BD=AE=E6=97=A0=E5=85=B3?= =?UTF-8?q?=EF=BC=8C=E5=8E=BB=E9=99=A4=E6=9C=AC=E5=9C=B0=E7=BB=9D=E5=AF=B9?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ scripts/acceptance.ps1 | 35 ++++++++++++++++++++++++++++------- scripts/config-test.ps1 | 14 ++++++++++---- scripts/portability-test.ps1 | 14 ++++++++++---- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index b23ac4c..c4e5f70 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ testrun2/ freshtest/ cfgtest/ cfgtest-run/ +config-run/ +portability-run/ portable-run/ # 截图辅助 diff --git a/scripts/acceptance.ps1 b/scripts/acceptance.ps1 index ef6b5c4..d1df9cf 100644 --- a/scripts/acceptance.ps1 +++ b/scripts/acceptance.ps1 @@ -2,10 +2,11 @@ # 用法: powershell -NoProfile -ExecutionPolicy Bypass -File scripts\acceptance.ps1 -Base http://127.0.0.1:18090 -Db <测试库路径> param( [string]$Base = 'http://127.0.0.1:18090', - [string]$Db = 'F:\hupeE的代肝平台\testrun\daili.db', - [string]$Python = 'E:\Miniconda3\python.exe', - [string]$Tools = 'F:\hupeE的代肝平台\scripts\dbtool.py', - [string]$LogFile = 'F:\hupeE的代肝平台\testrun\acceptance-result.txt' + [string]$Root = '', + [string]$Db = '', + [string]$Python = '', + [string]$Tools = '', + [string]$LogFile = '' ) try { Add-Type -AssemblyName System.Net.Http -ErrorAction Stop } catch { } @@ -13,6 +14,26 @@ try { Add-Type -AssemblyName System.Net.Http -ErrorAction Stop } catch { } $ErrorActionPreference = 'Continue' [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $env:PYTHONIOENCODING = 'utf-8' +# ---- 路径与外部程序:全部基于脚本所在目录,换机器/换目录都不用改 ---- +function Find-Python { + foreach ($c in @($env:PYTHON, 'python', 'py')) { + if (-not $c) { continue } + $cmd = Get-Command $c -ErrorAction SilentlyContinue + if ($cmd) { return $cmd.Source } + } + foreach ($p in @('E:\Miniconda3\python.exe', 'C:\Python312\python.exe', 'C:\Python311\python.exe')) { + if (Test-Path $p) { return $p } + } + throw '找不到 Python 解释器,请用 -Python 指定路径' +} +if (-not $Root) { $Root = Split-Path -Parent $PSScriptRoot } +if (-not $Db) { $Db = Join-Path $Root 'testrun\daili.db' } +if (-not $Tools) { $Tools = Join-Path $Root 'scripts\dbtool.py' } +if (-not $LogFile) { $LogFile = Join-Path $Root 'testrun\acceptance-result.txt' } +if (-not $Python) { $Python = Find-Python } +$BackupDir = Join-Path $Root 'testrun\backup' +$LogDir = Join-Path $Root 'testrun\logs' +$VerifyScript = Join-Path $Root 'scripts\verify-backup.py' $script:results = @() $script:fail = 0 $T = (Get-Date).Date @@ -444,7 +465,7 @@ Check 'R-06' '日志 CSV 含操作人/动作' ((Has $csv3 '操作人') -and (Has $r = Send $admin 'POST' '/settings/backup' Check 'R-07' '手动备份生成文件' ((Has $r.Content '已备份')) "flash=$(Flash $r.Content)" -$verify = (& $Python 'F:\hupeE的代肝平台\scripts\verify-backup.py' 'F:\hupeE的代肝平台\testrun\backup') -join ' ' +$verify = (& $Python $VerifyScript $BackupDir) -join ' ' Check 'R-07b' '备份文件可被标准 SQLite 打开且有数据' (($verify -match 'OK') -and ($verify -match 'customers\s+(\d+)')) $verify Send $admin 'POST' '/settings' @{ warn_days = '3'; screen_refresh = '30' } | Out-Null @@ -453,7 +474,7 @@ for ($i = 0; $i -lt 3; $i++) { Start-Sleep -Milliseconds 1200 Send $admin 'POST' '/settings/backup' | Out-Null } -$bk = @(Get-ChildItem 'F:\hupeE的代肝平台\testrun\backup' -Filter 'daili-*.db') +$bk = @(Get-ChildItem $BackupDir -Filter 'daili-*.db') $pruneOk = ($keepShown -ge 1) -and ($bk.Count -le $keepShown) -and (($keepShown -gt 3) -or ($bk.Count -eq $keepShown)) Check 'R-08' "备份保留份数生效(配置 $keepShown 份)" $pruneOk "count=$($bk.Count) keep=$keepShown" @@ -537,7 +558,7 @@ Check 'CF-02' '设置页展示备份目录与保留份数' ((Has $settingsHtml ' $anon2 = New-Sess $r = Send $anon2 'GET' '/screen' Check 'CF-03' '大屏免登录(screen_public = true)' ($r.Status -eq 200 -and $r.Path -eq '/screen') "status=$($r.Status) path=$($r.Path)" -$logs = @(Get-ChildItem 'F:\hupeE的代肝平台\testrun\logs' -Filter '*.log' -ErrorAction SilentlyContinue) +$logs = @(Get-ChildItem $LogDir -Filter '*.log' -ErrorAction SilentlyContinue) $logText = '' if ($logs.Count -ge 1) { $logText = Get-Content $logs[0].FullName -Raw -Encoding UTF8 } Check 'CF-04' '运行日志落盘' ($logs.Count -ge 1 -and $logText.Length -gt 20) "files=$($logs.Count) bytes=$($logText.Length)" diff --git a/scripts/config-test.ps1 b/scripts/config-test.ps1 index 77e9496..76f4763 100644 --- a/scripts/config-test.ps1 +++ b/scripts/config-test.ps1 @@ -1,11 +1,17 @@ # 配置文件专项测试:验证 daili.ini 的生成、解析、覆盖与各项生效 # 用法: powershell -NoProfile -ExecutionPolicy Bypass -File scripts\config-test.ps1 param( - [string]$Exe = 'F:\hupeE的代肝平台\daili.exe', - [string]$Work = 'F:\hupeE的代肝平台\cfgtest-run', + [string]$Exe = '', + [string]$Work = '', [string]$LanIp = '' ) +$Root = Split-Path -Parent $PSScriptRoot +if (-not $Exe) { $Exe = Join-Path $Root 'daili.exe' } +if (-not $Work) { $Work = Join-Path $Root 'config-run' } +$Evidence = Join-Path $Root 'test-evidence' +if (-not (Test-Path $Exe)) { throw "找不到被测程序:$Exe(先构建或已用 -Exe 指定)" } + $ErrorActionPreference = 'Continue' [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 Add-Type -AssemblyName System.Net.Http @@ -226,8 +232,8 @@ Stop-App $h $pass = @($script:results | Where-Object { $_ -like 'PASS|*' }).Count $fail = @($script:results | Where-Object { $_ -like 'FAIL|*' }).Count Write-Host "`n=== 汇总:通过 $pass 项,失败 $fail 项 ===" -ForegroundColor ($(if ($fail -eq 0) { 'Green' } else { 'Red' })) -New-Item -ItemType Directory -Force -Path 'F:\hupeE的代肝平台\test-evidence' | Out-Null -$script:results | Set-Content 'F:\hupeE的代肝平台\test-evidence\config-result.txt' -Encoding UTF8 +New-Item -ItemType Directory -Force -Path $Evidence | Out-Null +$script:results | Set-Content (Join-Path $Evidence 'config-result.txt') -Encoding UTF8 Remove-Item -Recurse -Force $Work -ErrorAction SilentlyContinue if ($fail -gt 0) { exit 1 } exit 0 diff --git a/scripts/portability-test.ps1 b/scripts/portability-test.ps1 index a97a7f1..3d4801a 100644 --- a/scripts/portability-test.ps1 +++ b/scripts/portability-test.ps1 @@ -1,12 +1,18 @@ # 数据可移植性测试:单文件、只拷 daili.db 无损迁移、删库即重置 # 用法: powershell -NoProfile -ExecutionPolicy Bypass -File scripts\portability-test.ps1 param( - [string]$Exe = 'F:\hupeE的代肝平台\daili.exe', - [string]$Work = 'F:\hupeE的代肝平台\portable-run', + [string]$Exe = '', + [string]$Work = '', [int]$PortA = 17781, [int]$PortB = 17782 ) +$Root = Split-Path -Parent $PSScriptRoot +if (-not $Exe) { $Exe = Join-Path $Root 'daili.exe' } +if (-not $Work) { $Work = Join-Path $Root 'portability-run' } +$Evidence = Join-Path $Root 'test-evidence' +if (-not (Test-Path $Exe)) { throw "找不到被测程序:$Exe(先构建或已用 -Exe 指定)" } + $ErrorActionPreference = 'Continue' [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 Add-Type -AssemblyName System.Net.Http @@ -153,8 +159,8 @@ Stop-App $hA2 $pass = @($script:results | Where-Object { $_ -like 'PASS|*' }).Count $fail = @($script:results | Where-Object { $_ -like 'FAIL|*' }).Count Write-Host "`n=== 汇总:通过 $pass 项,失败 $fail 项 ===" -ForegroundColor ($(if ($fail -eq 0) { 'Green' } else { 'Red' })) -New-Item -ItemType Directory -Force -Path 'F:\hupeE的代肝平台\test-evidence' | Out-Null -$script:results | Set-Content 'F:\hupeE的代肝平台\test-evidence\portability-result.txt' -Encoding UTF8 +New-Item -ItemType Directory -Force -Path $Evidence | Out-Null +$script:results | Set-Content (Join-Path $Evidence 'portability-result.txt') -Encoding UTF8 Remove-Item -Recurse -Force $Work -ErrorAction SilentlyContinue if ($fail -gt 0) { exit 1 } exit 0