Files
BakNRet/tools/Install-TestDependencies.ps1
T
Shuery 187d2759fd 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 层。这条要改行为、且影响真实名录的解析
    结果,留给你拍板,不在本提交里动手。
2026-09-27 09:46:08 +08:00

93 lines
3.5 KiB
PowerShell

<#
.SYNOPSIS
把测试与静态分析用的模块装进仓库内的 .tools\modules(不动机器上的全局模块)。
.DESCRIPTION
装两样:
* Pester —— tests\BakNRet.Tests.ps1 需要 5.0+。本机常见的坑是 Windows 自带的是
3.4.0,没有 `Should -Be`,套件会语法错误;
* PSScriptAnalyzer —— tools\Invoke-Analyzer.ps1 用它做"按微软规范"的门禁。
两者都用 Save-Module 下载到仓库内,而不是 Install-Module:后者会装到全局,
可能影响机器上别的、按旧语法写的脚本。.tools\ 已进 .gitignore,不污染版本库。
.EXAMPLE
pwsh -File .\tools\Install-TestDependencies.ps1
.EXAMPLE
pwsh -File .\tools\Install-TestDependencies.ps1 -Force
#>
[CmdletBinding()]
param(
[version]$PesterVersion = [version]'5.9.1',
[string]$PSScriptAnalyzerVersion = 'latest',
[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 ("PSScriptAnalyzer: {0}" -f $PSScriptAnalyzerVersion)
Write-Host ''
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
}
New-Item -ItemType Directory -Path $target -Force | Out-Null
$needPester = $Force -or -not (Test-Path -LiteralPath $installed)
if ($needPester) {
if ($Force -and (Test-Path -LiteralPath (Join-Path $target 'Pester'))) {
Remove-Item -LiteralPath (Join-Path $target 'Pester') -Recurse -Force
}
try {
Save-Module -Name Pester -RequiredVersion $PesterVersion -Path $target -Force -ErrorAction Stop
}
catch {
Write-Error "Pester 下载失败(多半是访问不到 PSGallery):$_"
exit 1
}
Write-Host ("已安装:Pester {0}" -f $PesterVersion) -ForegroundColor Green
}
else {
Write-Host "Pester {0} 已经装好了,跳过(要重装加 -Force)。" -f $PesterVersion -ForegroundColor DarkGray
}
# PSScriptAnalyzer:版本目录带具体版本号,所以"装没装过"按目录是否存在判断
$analyzerInstalled = @(Test-Path -LiteralPath (Join-Path $target 'PSScriptAnalyzer'))
if ($Force -or -not $analyzerInstalled) {
if ($Force -and $analyzerInstalled) {
Remove-Item -LiteralPath (Join-Path $target 'PSScriptAnalyzer') -Recurse -Force
}
try {
Save-Module -Name PSScriptAnalyzer -Path $target -Force -ErrorAction Stop
}
catch {
Write-Error "PSScriptAnalyzer 下载失败(多半是访问不到 PSGallery):$_"
exit 1
}
$analyzerVersion = (Get-ChildItem (Join-Path $target 'PSScriptAnalyzer') -Directory | Select-Object -First 1).Name
Write-Host ("已安装:PSScriptAnalyzer {0}" -f $analyzerVersion) -ForegroundColor Green
}
else {
Write-Host 'PSScriptAnalyzer 已经装好了,跳过(要重装加 -Force)。' -ForegroundColor DarkGray
}
Write-Host ''
Write-Host '现在可以跑:' -ForegroundColor Cyan
Write-Host ' .\tests\Run-Pester.ps1 (单元套件)' -ForegroundColor Gray
Write-Host ' .\tools\Invoke-Analyzer.ps1 (静态分析门禁)' -ForegroundColor Gray
Write-Host ''
exit 0