改造前:全仓 6/6 个源文件在 5.1 上解析失败(README 却承诺支持 5.1)。原因是文件是无 BOM 的 UTF-8,而 5.1 没有 BOM 就按 ANSI 代码页解码源码,中文变乱码、全角问号吃掉引号,整块语法塌掉。 现在 28/28 个文件在 5.1 与 7 上都解析零错误,E2E 36 项在 5.1 上全绿。 顺带修掉一个被 5.1 掩盖的缺陷:带 [CmdletBinding()] 的脚本在 5.1 上,param() 默认值里 拿不到 $PSScriptRoot(实测为空串,7 上正常)。于是 Backup.ps1 / Restore.ps1 在 5.1 上不传 路径参数就报错退出 —— 而计划任务恰恰不传。E2E 之所以看不见,是因为它总是显式传路径。 8 处默认值全部移到 param() 之后的解析段,沿用本仓库对 -BackupDir 一直在用的写法。 新增三条可重放的约定,让编码不再是一次性动作: .gitattributes 接管行尾(本机 core.autocrlf=true,会把工作区改成 CRLF 制造伪 diff) .editorconfig 用 charset = utf-8-bom 锁住 BOM tools\Set-SourceEncoding.ps1 是规范化脚本,tools\Invoke-* 之外的任何改动之后都能重放 test.ps1 是唯一验收入口:Encode + Parse + Unit + Smoke + E2E,在 7 与 5.1 上各跑一遍 test.ps1 的 Encode 层直接检查"必须有 BOM"这条规则。加它的原因很实际:实测本仓库用的 编辑工具在保存时会悄悄去掉 BOM,而丢了 BOM 的文件只在 5.1 上出错、在 7 上完全正常, 没有这条检查就会一直漏过去。 已知未修(下一步处理):5.1 上 Unit 有 6 项、Smoke 有 1 项失败,全部源于测试夹具写 临时文件时没指定编码(5.1 的 Set-Content 默认 ANSI),与产品代码无关。
72 lines
2.4 KiB
PowerShell
72 lines
2.4 KiB
PowerShell
<#
|
||
.SYNOPSIS
|
||
把测试用的 Pester 5 装进仓库内的 .tools\modules(不动机器上的全局模块)。
|
||
|
||
.DESCRIPTION
|
||
tests\BakNRet.Tests.ps1 需要 Pester 5.0+。本机常见的坑是:
|
||
* Windows 自带的是 Pester 3.4.0,没有 `Should -Be`,套件会语法错误;
|
||
* 直接 Install-Module 会把 5.x 装到全局,可能影响机器上别的、按 3.x 语法写的脚本。
|
||
|
||
所以这里用 Save-Module 把指定版本下载到仓库内的 .tools\modules,
|
||
tests\Run-Pester.ps1 会自动把该目录加进 PSModulePath。
|
||
.tools\ 已进 .gitignore,不会污染版本库。
|
||
|
||
.EXAMPLE
|
||
pwsh -File .\tools\Install-TestDependencies.ps1
|
||
|
||
.EXAMPLE
|
||
pwsh -File .\tools\Install-TestDependencies.ps1 -PesterVersion 5.9.1 -Force
|
||
#>
|
||
|
||
[CmdletBinding()]
|
||
param(
|
||
[version]$PesterVersion = [version]'5.9.1',
|
||
[switch]$Force
|
||
)
|
||
|
||
$ErrorActionPreference = 'Stop'
|
||
$ProgressPreference = 'SilentlyContinue'
|
||
|
||
$projectRoot = Split-Path -Parent $PSScriptRoot
|
||
$target = Join-Path $projectRoot '.tools\modules'
|
||
$installed = Join-Path $target ("Pester\{0}" -f $PesterVersion)
|
||
|
||
Write-Host ''
|
||
Write-Host ("目标目录 : {0}" -f $target)
|
||
Write-Host ("Pester : {0}" -f $PesterVersion)
|
||
Write-Host ''
|
||
|
||
if ((Test-Path -LiteralPath $installed) -and -not $Force) {
|
||
Write-Host "已经装好了,无需重复下载(要重装加 -Force)。" -ForegroundColor Green
|
||
exit 0
|
||
}
|
||
|
||
if ($Force -and (Test-Path -LiteralPath (Join-Path $target 'Pester'))) {
|
||
Remove-Item -LiteralPath (Join-Path $target 'Pester') -Recurse -Force
|
||
}
|
||
|
||
New-Item -ItemType Directory -Path $target -Force | Out-Null
|
||
|
||
if (-not (Get-Command Save-Module -ErrorAction SilentlyContinue)) {
|
||
Write-Error '当前环境没有 Save-Module(需要 PowerShellGet 2.x)。请改用:Install-Module Pester -Scope CurrentUser -MinimumVersion 5.0.0'
|
||
exit 1
|
||
}
|
||
|
||
try {
|
||
Save-Module -Name Pester -RequiredVersion $PesterVersion -Path $target -Force -ErrorAction Stop
|
||
} catch {
|
||
Write-Error "下载失败(多半是访问不到 PSGallery):$_"
|
||
exit 1
|
||
}
|
||
|
||
$module = Get-Module -ListAvailable Pester | Where-Object { [version]$_.Version -eq $PesterVersion }
|
||
if (-not $module) {
|
||
Write-Error "下载结束但找不到 Pester $PesterVersion,请检查 $target。"
|
||
exit 1
|
||
}
|
||
|
||
Write-Host ("已安装:Pester {0} -> {1}" -f $module.Version, $module.ModuleBase) -ForegroundColor Green
|
||
Write-Host '现在可以跑:.\tests\Run-Pester.ps1' -ForegroundColor Cyan
|
||
Write-Host ''
|
||
exit 0
|