yeg
2026-09-17 f7a18613438ce6fbac6a7e2525e5a955cb83cf29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Runs the server test suite against the MySQL test schema with the TEST_DATASOURCE_* variables
# already set, so a new session does not have to rediscover them.
#
# It refuses to start while another run against this checkout is still active: two Maven builds
# sharing one target/ directory leave inconsistent class files, which then surface as impossible
# errors such as a @ConfigurationProperties bean that "does not exist".
#
# Usage:
#   .\scripts\run-server-tests.ps1                                  # whole suite
#   .\scripts\run-server-tests.ps1 -Test PaymentSettlementTests     # one class
#   .\scripts\run-server-tests.ps1 -Test PaymentSettlementTests,AppOrderPortalServiceTests
#   .\scripts\run-server-tests.ps1 -Reset                           # reset the schema first, then run
#   .\scripts\run-server-tests.ps1 -Clean                           # clean build (use after an interrupted run)
param(
    [string[]] $Test = @(),
    [switch]   $Reset,
    [switch]   $Clean
)
 
$ErrorActionPreference = 'Stop'
$serverRoot = Split-Path -Parent $PSScriptRoot
$lockFile = Join-Path $env:TEMP ("talent-card-tests-{0}.lock" -f ($serverRoot -replace '[\\/:]', '_'))
 
function Test-RunActive {
    if (-not (Test-Path $lockFile)) { return $false }
    $owner = (Get-Content $lockFile -ErrorAction SilentlyContinue | Select-Object -First 1)
    if (-not $owner) { return $false }
    $processId = 0
    if (-not [int]::TryParse($owner.Trim(), [ref] $processId)) { return $false }
    return [bool] (Get-Process -Id $processId -ErrorAction SilentlyContinue)
}
 
if (Test-RunActive) {
    $owner = (Get-Content $lockFile | Select-Object -First 1).Trim()
    throw "Another test run (pid $owner) is still using $serverRoot. Wait for it to finish: concurrent Maven builds corrupt target/ and produce misleading failures."
}
 
Set-Content -Path $lockFile -Value $PID
 
try {
    if (-not $env:TEST_DATASOURCE_URL) {
        $env:TEST_DATASOURCE_URL = 'jdbc:mysql://127.0.0.1:8134/talent_card_test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true'
    }
    if (-not $env:TEST_DATASOURCE_USERNAME) { $env:TEST_DATASOURCE_USERNAME = 'root' }
    if (-not $env:TEST_DATASOURCE_PASSWORD) { $env:TEST_DATASOURCE_PASSWORD = '123456' }
    if (-not $env:TEST_DATASOURCE_DRIVER)   { $env:TEST_DATASOURCE_DRIVER = 'com.mysql.cj.jdbc.Driver' }
 
    if ($Reset) {
        & (Join-Path $PSScriptRoot 'reset-test-db.ps1')
        if ($LASTEXITCODE -ne 0) { throw "Database reset failed (exit $LASTEXITCODE)." }
        $Clean = $true
    }
 
    $goals = @('test')
    $arguments = @('-o') + $goals
    if ($Test.Count -gt 0) { $arguments += ('-Dtest=' + ($Test -join ',')) }
    $arguments += '-DfailIfNoTests=false'
 
    $log = Join-Path $serverRoot 'target\server-tests.log'
    Push-Location $serverRoot
    # 原生命令往 stderr 写东西(例如 JVM 那句 sun.misc.Unsafe 警告)时,PowerShell 5.1 会把它当成
    # 终止错误、在编译中途掐断构建(子进程也会被带走)。Maven 的退出码才是唯一判据,所以这一段
    # 放宽 ErrorActionPreference,只在结束时看 $LASTEXITCODE。
    $strict = $ErrorActionPreference
    $ErrorActionPreference = 'Continue'
    try {
        # clean 只能单独先跑:日志文件就在 target\ 下、且被下面那条 `*>` 重定向一直打开着,
        # 把 clean 并进同一次 Maven 调用会以 "Failed to delete target\server-tests.log" 失败。
        if ($Clean) {
            Write-Host 'Running: .\mvnw.cmd -o clean' -ForegroundColor Cyan
            & (Join-Path $serverRoot 'mvnw.cmd') -o clean *> $null
            if ($LASTEXITCODE -ne 0) { throw "mvnw clean failed (exit $LASTEXITCODE)." }
            # clean 把整个 target\ 删掉,重定向就没地方写日志了(而且会静默变成"假成功")。
            New-Item -ItemType Directory -Force -Path (Split-Path $log) | Out-Null
        }
        # 先落一个空文件:万一这条重定向失败,$LASTEXITCODE 会留着上一次的值,下面据此判定"没跑成"。
        Set-Content -Path $log -Value ''
        Write-Host ("Running: .\mvnw.cmd {0}" -f ($arguments -join ' ')) -ForegroundColor Cyan
        & (Join-Path $serverRoot 'mvnw.cmd') @arguments *> $log
        $exitCode = $LASTEXITCODE
        if ((Get-Item $log).Length -eq 0) { throw "Maven wrote nothing to $log; the run did not start." }
    } finally {
        $ErrorActionPreference = $strict
        Pop-Location
    }
 
    Select-String -Path $log -Pattern 'Tests run:.*Skipped: \d+$|^\[ERROR\]   ' |
        ForEach-Object { $_.Line } |
        Select-Object -Last 12
 
    if ($exitCode -eq 0) {
        Write-Host "Tests passed. Full log: $log" -ForegroundColor Green
    } else {
        Write-Host "Tests failed (exit $exitCode). Full log: $log" -ForegroundColor Red
        Write-Host 'Known baseline failures (not regressions, see docs/agents/testing.md):' -ForegroundColor Yellow
        Write-Host '  MerchantWorkServiceTests.filtersOrdersByCategoryStatusAndDateWithoutExposingFullMobile'
        Write-Host '  TalentAuthServiceTests.movesWechatBindingWhenSameWechatLogsInWithAnotherMobile'
        Write-Host '  MerchantSettlementStatisticsMapperTests.includesHotelFreeOfflineSubsidyWithoutIncreasingTransactionAmount'
    }
    exit $exitCode
} finally {
    Remove-Item $lockFile -Force -ErrorAction SilentlyContinue
}