Skip to content

Commit 1b21d14

Browse files
committed
test(tailscale): 添加行尾标准化函数以解决跨平台测试问题
添加了 Convert-ToLfLineEndings 函数来统一文本行尾为 LF 格式, 避免 GitHub Actions 在不同平台上因 CRLF/LF 行尾差异导致的测试失败。 同时更新了 Dockerfile 测试用例以使用该函数处理行尾问题。
1 parent 5896e47 commit 1b21d14

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

tests/TailscaleDerpComposeTemplate.Tests.ps1

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ BeforeAll {
66
$script:TemplateDir = Join-Path $script:RepoRoot 'config/network/tailscale/derp'
77
}
88

9+
function script:Convert-ToLfLineEndings {
10+
<#
11+
.SYNOPSIS
12+
把文本统一为 LF 行尾。
13+
14+
.DESCRIPTION
15+
GitHub Actions 在不同平台 checkout 后,原始文本断言可能拿到 LF 或 CRLF。
16+
这里先把行尾归一化,避免 `(?m)^...$` 这类精确行匹配被 `\r` 干扰。
17+
18+
.PARAMETER Content
19+
待归一化的原始文本。
20+
21+
.OUTPUTS
22+
System.String
23+
返回仅使用 LF 的文本。
24+
#>
25+
param(
26+
[AllowEmptyString()]
27+
[string]$Content
28+
)
29+
30+
return $Content -replace "`r`n?", "`n"
31+
}
32+
933
function script:Get-ComposeServiceBlock {
1034
<#
1135
.SYNOPSIS
@@ -68,8 +92,17 @@ Describe 'Tailscale DERP template files' {
6892
}
6993

7094
Describe 'Tailscale DERP Dockerfile' {
95+
It 'matches the builder stage header even if the file text uses CRLF line endings' {
96+
$dockerfile = @'
97+
ARG GO_BUILD_IMAGE=golang:1.26-alpine
98+
FROM ${GO_BUILD_IMAGE} AS build
99+
'@ -replace "`n", "`r`n"
100+
101+
(Convert-ToLfLineEndings -Content $dockerfile) | Should -Match '(?m)^FROM \$\{GO_BUILD_IMAGE\} AS build$'
102+
}
103+
71104
It 'uses a Go 1.26+ builder image that can be overridden via build args' {
72-
$dockerfile = Get-Content -LiteralPath (Join-Path $script:TemplateDir 'Dockerfile.derper') -Raw
105+
$dockerfile = Convert-ToLfLineEndings -Content (Get-Content -LiteralPath (Join-Path $script:TemplateDir 'Dockerfile.derper') -Raw)
73106

74107
$dockerfile | Should -Match '(?m)^ARG GO_BUILD_IMAGE=golang:1\.26'
75108
$dockerfile | Should -Match '(?m)^FROM \$\{GO_BUILD_IMAGE\} AS build$'

0 commit comments

Comments
 (0)