fix: 让 5.1 真正可用(显式编码 + 原生 stderr 处理 + .psd1 夹具带 BOM)
上一提交让 5.1 能解析源码,但 Unit 与 Smoke 在 5.1 上仍然是红的。根因是三类彼此 无关的 5.1/7 行为差,全部实测确认: 1) 不写 -Encoding 时,5.1 的 Get-Content / Set-Content 默认是 ANSI,7 是 UTF-8。 症状是 UTF-8 字节被按 GBK 解出「璇存槑」这类乱码。62 处补上显式 -Encoding UTF8。 用 AST 而不是正则定位,避免把注释里的散文也改掉。 2) .psd1 夹具用无 BOM 写,而引擎的 .psd1 读取器(Import-PowerShellDataFile)只能靠 BOM 判断编码、没有参数可传,于是 5.1 按 ANSI 解。40 处夹具改为带 BOM 写 —— 这正是 .editorconfig 里 [*.psd1] charset = utf-8-bom 本来就要求的,是夹具违反了自己的约定。 .cmd 批次文件刻意保持无 BOM:cmd.exe 会被 BOM 弄坏。 3) 5.1 在 $ErrorActionPreference = Stop 下会把原生命令写到 stderr 的内容升级成终止性 NativeCommandError,7 改了这条。takeown/icacls 的 ACL 复位调用、以及 test.ps1 自己 调子进程的地方,都需要在 Continue 下跑。 验收:test.ps1 9/9 全绿(Encode + Parse + Unit + Smoke + E2E,在 7 与 5.1 上各跑一遍)。 已知未处理(留待后续提交):tools/lab/** 里还有若干「原生命令 + 2>&1 + Stop」的同类 写法(takeown / icacls / scoop / code / Get-WimInfo)。它们要 Hyper-V 实验机才跑得到, 不在验收门槛内。
This commit is contained in:
1 parent
2d26f78d15
commit
e10503be76
13 files changed
+122
-104
No files matched your search
+16
-16
@@ -121,7 +121,7 @@ BeforeAll {
|
||||
|
||||
function Write-ListFile {
|
||||
param([Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][string]$Content)
|
||||
[System.IO.File]::WriteAllText($Path, $Content, [System.Text.UTF8Encoding]::new($false))
|
||||
[System.IO.File]::WriteAllText($Path, $Content, [System.Text.UTF8Encoding]::new($true))
|
||||
return $Path
|
||||
}
|
||||
}
|
||||
@@ -348,7 +348,7 @@ Describe '归档命名' {
|
||||
$script:NamingRoot = Join-Path $script:Sandbox 'naming'
|
||||
$script:NamingApp = Join-Path $script:NamingRoot 'Real App'
|
||||
New-Item -ItemType Directory -Path $script:NamingApp -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $script:NamingApp 'keep.txt') 'k'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:NamingApp 'keep.txt') 'k'
|
||||
|
||||
$script:NamingCatalog = Write-ListFile -Path (Join-Path $script:NamingRoot 'cat.psd1') -Content @"
|
||||
@{
|
||||
@@ -415,8 +415,8 @@ Describe '7z 排除参数翻译' {
|
||||
$script:ExcludeTree = Join-Path $script:Sandbox 'exclude-tree'
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:ExcludeTree 'sub\Cache') -Force | Out-Null
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:ExcludeTree 'Keep') -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $script:ExcludeTree 'sub\Cache\x.bin') 'x'
|
||||
Set-Content -LiteralPath (Join-Path $script:ExcludeTree 'Keep\y.bin') 'y'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:ExcludeTree 'sub\Cache\x.bin') 'x'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:ExcludeTree 'Keep\y.bin') 'y'
|
||||
|
||||
function New-TestArchiveItem {
|
||||
param([string]$ArchivePath, [string]$RealPath = 'C:\nonexistent-path')
|
||||
@@ -573,8 +573,8 @@ Describe '归档项与暂存目录' {
|
||||
$script:StagingDir = Join-Path $script:StagingRoot 'real-dir'
|
||||
$script:StagingFile = Join-Path $script:StagingRoot 'settings.json'
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:StagingDir 'inner') -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $script:StagingDir 'inner\a.txt') 'a'
|
||||
Set-Content -LiteralPath $script:StagingFile 'file-content'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:StagingDir 'inner\a.txt') 'a'
|
||||
Set-Content -Encoding UTF8 -LiteralPath $script:StagingFile 'file-content'
|
||||
}
|
||||
|
||||
It 'New-BaknretArchiveItem 规范化归档内路径并算出 TopName' {
|
||||
@@ -611,7 +611,7 @@ Describe '归档项与暂存目录' {
|
||||
# 文件项:归档内的名字就叫 Cfg(没有扩展名),内容与源文件一致
|
||||
$cfg = Get-Item -LiteralPath (Join-Path $stage 'Cfg') -Force
|
||||
$cfg.PSIsContainer | Should -BeFalse
|
||||
(Get-Content -LiteralPath $cfg.FullName -Raw).Trim() | Should -Be 'file-content'
|
||||
(Get-Content -Encoding UTF8 -LiteralPath $cfg.FullName -Raw).Trim() | Should -Be 'file-content'
|
||||
} finally {
|
||||
Remove-BaknretArchiveStaging -Root $stage
|
||||
}
|
||||
@@ -734,8 +734,8 @@ Describe '软件名录' {
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:CatRoot 'real app') -Force | Out-Null
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:CatRoot 'other') -Force | Out-Null
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:CatRoot 'inc') -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $script:CatRoot 'real app\keep.txt') 'k'
|
||||
Set-Content -LiteralPath (Join-Path $script:CatRoot 'file.txt') 'file-content'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:CatRoot 'real app\keep.txt') 'k'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:CatRoot 'file.txt') 'file-content'
|
||||
|
||||
$script:CatReal = Join-Path $script:CatRoot 'real app'
|
||||
$script:CatOther = Join-Path $script:CatRoot 'other'
|
||||
@@ -1090,12 +1090,12 @@ Describe '集成:真实 7z 压缩与排除规则' -Skip:(-not $script:HasSeven
|
||||
foreach ($directory in 'Default\Cache', 'Default\Code Cache', 'Default\Extensions', 'component_crx_cache', 'Default\IndexedDB') {
|
||||
New-Item -ItemType Directory -Path (Join-Path $script:IntegrationPath $directory) -Force | Out-Null
|
||||
}
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'keep.txt') 'keep'
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'Default\Cache\c.bin') 'c'
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'Default\Code Cache\cc.bin') 'cc'
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'Default\Extensions\e.bin') 'e'
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'component_crx_cache\x.bin') 'x'
|
||||
Set-Content -LiteralPath (Join-Path $script:IntegrationPath 'Default\IndexedDB\i.bin') 'i'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'keep.txt') 'keep'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'Default\Cache\c.bin') 'c'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'Default\Code Cache\cc.bin') 'cc'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'Default\Extensions\e.bin') 'e'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'component_crx_cache\x.bin') 'x'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:IntegrationPath 'Default\IndexedDB\i.bin') 'i'
|
||||
}
|
||||
|
||||
It '排除规则与空格处理在真实归档上生效' {
|
||||
@@ -1326,7 +1326,7 @@ Describe '集成:方向标记与孤儿审计' -Skip:(-not ($script:HasSevenZip
|
||||
$script:DirectionRoot = Join-Path $script:Sandbox 'direction'
|
||||
$script:DirectionSource = Join-Path $script:DirectionRoot 'src\Keep Me'
|
||||
New-Item -ItemType Directory -Path $script:DirectionSource -Force | Out-Null
|
||||
Set-Content -LiteralPath (Join-Path $script:DirectionSource 'data.txt') 'keep'
|
||||
Set-Content -Encoding UTF8 -LiteralPath (Join-Path $script:DirectionSource 'data.txt') 'keep'
|
||||
|
||||
$script:DirectionBackupDir = Join-Path $script:DirectionRoot 'Backups'
|
||||
$script:DirectionList = Join-Path $script:DirectionRoot 'list.txt'
|
||||
|
||||
Reference in new issue
Block a user