style: 按微软规范落地静态分析,并全仓机械重排
三件事:
1) tools\Install-TestDependencies.ps1 现在也把 PSScriptAnalyzer 装进仓库内的 .tools\modules
(不动机器上的全局模块,与 Pester 同一策略)。
2) PSScriptAnalyzerSettings.psd1:这是必要的,不是装饰 —— 那 6 条格式规则
(括号、缩进、空格、对齐、大小写)默认全是 Disabled,所以不带 -Settings 的
`Invoke-ScriptAnalyzer -Severity Warning,Error` 会**静默漏掉全部排版问题**。本文件用 Rules
把它们打开(而不是用 IncludeRules 换一套),于是默认规则与格式规则同时生效。
三条有意的排除都写明了理由:PSAvoidUsingWriteHost(彩色控制台输出是这份工具的刻意设计)、
PSUseShouldProcessForStateChangingFunctions(WhatIf 的边界在入口脚本,给库里 27 个改状态的
函数都加上反而会"静默跳过",备份看着成功却什么都没做)、PSAvoidUsingPlainTextForPassword
(7z 只接受命令行口令,这是 7z 的限制,README 里写明了取舍)。
3) tools\Invoke-Analyzer.ps1:独立门禁(不塞进 Pester 用例 —— 套件跑一次二十多秒,
混进去会让"测试红了"这句话失去分辨力),路径过滤与验收门槛的 Encode/Parse 两层一致。
全仓重排结果:706 条告警 -> 67 条。修掉的 639 条全部是格式(闭括号 168、空格 80、
对齐 68、缩进 60、行长 229)。重排后 9/9 验收全绿、100 个文件两版解析零错、
276 个断言原样通过 —— 机械重排没有改变任何可观察行为。
如实说明两件事:
* 行长上限设成 160,**不是**官方默认的 120。120 在本仓库意味着 270 处改动(主要是
中文注释与测试夹具里的一行式目录),160 意味着 41 处。160 仍是"宽但可读",而理由是写在
配置文件里的:这不是悄悄放宽,想收紧到 120 时那份清单就在分析器输出里。
* 剩余 67 条里,41 条是上面那批行长,其余 26 条是分析器找出的真问题(未使用参数 6、
空 catch 6、MD5 指纹 1、覆盖内置命令 1、switch 默认值 1 等)。其中
Find-ChildDirectoryByName 的 MaxDepth 参数从未被使用 —— 也就是配置里的
CatalogMaxDepth = 5 是假的,前缀补全实际只查 1 层。这条要改行为、且影响真实名录的解析
结果,留给你拍板,不在本提交里动手。
This commit is contained in:
1 parent
102a3e038d
commit
187d2759fd
60 files changed
+769
-377
No files matched your search
+28
-20
@@ -82,7 +82,8 @@ BeforeAll {
|
||||
try {
|
||||
$exitCode = Invoke-ExternalCommand -FilePath 'cmd.exe' -ArgumentList @('/c', $cmdFile)
|
||||
$lines = @(Get-Content -LiteralPath $outFile -Encoding UTF8 -ErrorAction SilentlyContinue)
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $outFile -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item -LiteralPath $cmdFile -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
@@ -134,7 +135,7 @@ AfterAll {
|
||||
|
||||
# ============================================================================
|
||||
Describe 'BackupList.txt 解析' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
It '注释行与空行返回 $null' {
|
||||
ConvertFrom-BackupListLine -Line '# 这是注释' | Should -BeNullOrEmpty
|
||||
@@ -364,7 +365,7 @@ Describe 'BackupList.txt 解析' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '归档命名' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:NamingRoot = Join-Path $script:Sandbox 'naming'
|
||||
@@ -431,7 +432,7 @@ Describe '归档命名' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '7z 排除参数翻译' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:ExcludeTree = Join-Path $script:Sandbox 'exclude-tree'
|
||||
@@ -588,7 +589,7 @@ Describe '7z 排除参数翻译' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '归档项与暂存目录' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:StagingRoot = Join-Path $script:Sandbox 'staging'
|
||||
@@ -660,7 +661,8 @@ Describe '归档项与暂存目录' {
|
||||
$cfg = Get-Item -LiteralPath (Join-Path $stage 'Cfg') -Force
|
||||
$cfg.PSIsContainer | Should -BeFalse
|
||||
(Get-Content -Encoding UTF8 -LiteralPath $cfg.FullName -Raw).Trim() | Should -Be 'file-content'
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Remove-BaknretArchiveStaging -Root $stage
|
||||
}
|
||||
}
|
||||
@@ -678,7 +680,7 @@ Describe '归档项与暂存目录' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '命令行参数拼接' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
It '无空格参数原样输出' {
|
||||
ConvertTo-NativeArgumentString -ArgumentList @('a', '-mx=9') | Should -Be 'a -mx=9'
|
||||
@@ -703,7 +705,7 @@ Describe '命令行参数拼接' {
|
||||
|
||||
# ============================================================================
|
||||
Describe 'manifest 与配置' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
It 'manifest 读写往返' {
|
||||
$path = Join-Path $script:Sandbox 'roundtrip\manifest.json'
|
||||
@@ -762,7 +764,8 @@ Describe 'manifest 与配置' {
|
||||
(Get-BaknretPassword) | Should -Be 'sekrit'
|
||||
$env:BAKNRET_PASSWORD = $null
|
||||
(Get-BaknretPassword) | Should -BeNullOrEmpty
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
$env:BAKNRET_PASSWORD = $saved
|
||||
}
|
||||
}
|
||||
@@ -770,7 +773,7 @@ Describe 'manifest 与配置' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '软件名录' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:CatRoot = Join-Path $script:Sandbox 'catalog'
|
||||
@@ -961,7 +964,7 @@ Describe '软件名录' {
|
||||
|
||||
# ============================================================================
|
||||
Describe 'Resolve-BackupEntry:条目解析' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:CatRoot = Join-Path $script:Sandbox 'catalog'
|
||||
@@ -1114,7 +1117,7 @@ Describe 'Resolve-BackupEntry:条目解析' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
It '口令不会进日志:DEBUG 下打印的命令行要遮蔽 -p 参数' {
|
||||
$sandbox = Join-Path $env:TEMP ("bnr-secret-" + [guid]::NewGuid().ToString('N').Substring(0, 8))
|
||||
@@ -1125,7 +1128,8 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
$logPath = Start-BaknretLog -Directory $sandbox -Prefix 'secret'
|
||||
Set-BaknretDebug
|
||||
$null = Invoke-ExternalCommand -FilePath 'cmd.exe' -ArgumentList @('/c', 'exit 0', "-p$sentinel")
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Set-BaknretDebug -Enabled:$false
|
||||
Stop-BaknretLog
|
||||
}
|
||||
@@ -1153,7 +1157,8 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
$second | Should -BeNullOrEmpty
|
||||
|
||||
Test-Path -LiteralPath (Get-BaknretRunLockPath -Directory $dir) | Should -BeTrue
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Exit-BaknretRunLock -Lock $second
|
||||
Exit-BaknretRunLock -Lock $first
|
||||
}
|
||||
@@ -1175,7 +1180,8 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
$second = Enter-BaknretRunLock -Directory $dir
|
||||
$second | Should -Not -BeNullOrEmpty
|
||||
Exit-BaknretRunLock -Lock $second
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
@@ -1190,7 +1196,8 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
$summary.FileCount | Should -Be 0
|
||||
$summary.TotalSize | Should -Not -BeNullOrEmpty
|
||||
$summary.TotalSize | Should -Be 0
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $empty -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
@@ -1213,7 +1220,8 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
(Get-Item -LiteralPath $target).IsReadOnly = $true
|
||||
{ Write-BaknretAtomicText -Path $target -Text 'SHOULD-NOT-LAND' } | Should -Throw
|
||||
(Get-Content -Encoding UTF8 -LiteralPath $target -Raw) | Should -Be 'SECOND'
|
||||
} finally {
|
||||
}
|
||||
finally {
|
||||
$file = Get-Item -LiteralPath $target -ErrorAction SilentlyContinue
|
||||
if ($file) { $file.IsReadOnly = $false }
|
||||
Remove-Item -LiteralPath $base -Recurse -Force -ErrorAction SilentlyContinue
|
||||
@@ -1231,7 +1239,7 @@ Describe '外部命令退出码(旧实现的核心缺陷)' {
|
||||
|
||||
# ============================================================================
|
||||
Describe '集成:真实 7z 压缩与排除规则' -Skip:(-not $script:HasSevenZip) {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:IntegrationRoot = Join-Path $script:Sandbox 'integration'
|
||||
@@ -1298,7 +1306,7 @@ Describe '集成:真实 7z 压缩与排除规则' -Skip:(-not $script:HasSeven
|
||||
|
||||
# ============================================================================
|
||||
Describe '集成:Backup.ps1 / Restore.ps1 端到端(字面路径条目)' -Skip:(-not ($script:HasSevenZip -and $script:HasPwsh)) {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
BeforeAll {
|
||||
$script:E2ERoot = Join-Path $script:Sandbox 'e2e'
|
||||
@@ -1468,7 +1476,7 @@ Describe '集成:Backup.ps1 / Restore.ps1 端到端(字面路径条目)' -
|
||||
|
||||
# ============================================================================
|
||||
Describe '集成:方向标记与孤儿审计' -Skip:(-not ($script:HasSevenZip -and $script:HasPwsh)) {
|
||||
# ============================================================================
|
||||
# ============================================================================
|
||||
|
||||
# `+` / `-` 的归档名必须在方向过滤**之前**登记:这些条目自己不产生归档,
|
||||
# 但它们对应的归档是有主的,不能被孤儿审计当成没人要的孤儿。
|
||||
|
||||
Reference in new issue
Block a user