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
+25
-22
@@ -38,10 +38,10 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory, Position = 0)]
|
||||
[ValidateSet('status','start','stop','wait','sync','seed','backup','restore','acl-test','test','shell','console','checkpoint','reset','destroy')]
|
||||
[ValidateSet('status', 'start', 'stop', 'wait', 'sync', 'seed', 'backup', 'restore', 'acl-test', 'test', 'shell', 'console', 'checkpoint', 'reset', 'destroy')]
|
||||
[string]$Verb,
|
||||
|
||||
[ValidateSet('all','pester','zero','e2e')][string]$Suite = 'all',
|
||||
[ValidateSet('all', 'pester', 'zero', 'e2e')][string]$Suite = 'all',
|
||||
|
||||
# 恢复演练要处理的条目(写法同 BackupList.txt 的一行)
|
||||
[string[]]$Entries,
|
||||
@@ -63,10 +63,10 @@ $ErrorActionPreference = 'Stop'
|
||||
. (Join-Path $PSScriptRoot 'Lab-Common.ps1')
|
||||
$cfg = Get-LabConfig
|
||||
|
||||
$guestSandbox = "$($cfg.GuestRepoPath)\tools\lab\payload\sandbox"
|
||||
$guestList = "$guestSandbox\BackupList.txt"
|
||||
$guestConfig = "$guestSandbox\BackupConfig.psd1"
|
||||
$guestFixture = "$($cfg.GuestRepoPath)\tools\lab\payload\lab-fixtures.ps1"
|
||||
$guestSandbox = "$($cfg.GuestRepoPath)\tools\lab\payload\sandbox"
|
||||
$guestList = "$guestSandbox\BackupList.txt"
|
||||
$guestConfig = "$guestSandbox\BackupConfig.psd1"
|
||||
$guestFixture = "$($cfg.GuestRepoPath)\tools\lab\payload\lab-fixtures.ps1"
|
||||
$guestBackupDir = 'C:\BakNRet-Lab\Backups'
|
||||
|
||||
Assert-LabElevated -Why "Hyper-V 操作与 PowerShell Direct 都需要管理员(动词:$Verb)"
|
||||
@@ -80,12 +80,12 @@ function Get-VmSummary {
|
||||
if (-not $vm) { return $null }
|
||||
$mem = Get-VMMemory -VMName $cfg.VmName
|
||||
return [pscustomobject]@{
|
||||
Name = $vm.Name
|
||||
State = $vm.State
|
||||
Uptime = [int]$vm.Uptime.TotalSeconds
|
||||
Cpu = $vm.ProcessorCount
|
||||
MemoryGB = [math]::Round($mem.Startup / 1GB, 1)
|
||||
Gen = $vm.Generation
|
||||
Name = $vm.Name
|
||||
State = $vm.State
|
||||
Uptime = [int]$vm.Uptime.TotalSeconds
|
||||
Cpu = $vm.ProcessorCount
|
||||
MemoryGB = [math]::Round($mem.Startup / 1GB, 1)
|
||||
Gen = $vm.Generation
|
||||
UptimeText = "$([int]$vm.Uptime.TotalMinutes) 分钟"
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ function Invoke-GuestScriptFile {
|
||||
$text = if ($badUtf8 -le $badAnsi) { $asUtf8 } else { $asAnsi }
|
||||
return @($text -split "`r?`n" | Where-Object { $_ -ne '' } | Select-Object -Last $lines)
|
||||
}
|
||||
$all = @('-NoProfile','-ExecutionPolicy','Bypass','-File',$script) + $scriptArgs
|
||||
$all = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $script) + $scriptArgs
|
||||
$out = $logPath
|
||||
$err = "$logPath.err"
|
||||
$p = Start-Process -FilePath 'pwsh.exe' -ArgumentList $all -NoNewWindow -Wait -PassThru -RedirectStandardOutput $out -RedirectStandardError $err
|
||||
@@ -147,7 +147,8 @@ function Invoke-LabSync {
|
||||
Push-Location $cfg.RepoRoot
|
||||
try {
|
||||
& $sevenZip a -tzip $zip '.\*' '-xr!Backups' '-xr!logs' '-xr!.git' '-xr!.tools' '-xr!*.tmp.7z' '-xr!*.tmp.zip' -y | Out-Null
|
||||
} finally { Pop-Location }
|
||||
}
|
||||
finally { Pop-Location }
|
||||
Write-LabLog ("快照大小 {0} MB" -f [math]::Round((Get-Item -LiteralPath $zip).Length / 1MB, 2))
|
||||
|
||||
Write-LabLog '推送到 VM(Copy-VMFile,走 VMBus)' 'STEP'
|
||||
@@ -214,7 +215,7 @@ switch ($Verb) {
|
||||
$arch = @()
|
||||
if (Test-Path 'C:\BakNRet-Lab\Backups') {
|
||||
$arch = @(Get-ChildItem 'C:\BakNRet-Lab\Backups' -Filter *.7z -ErrorAction SilentlyContinue |
|
||||
ForEach-Object { [pscustomobject]@{ Name = $_.BaseName; MB = [math]::Round($_.Length / 1MB, 2) } })
|
||||
ForEach-Object { [pscustomobject]@{ Name = $_.BaseName; MB = [math]::Round($_.Length / 1MB, 2) } })
|
||||
}
|
||||
$src = 'C:\BakNRet-Lab\sources'
|
||||
[pscustomobject]@{
|
||||
@@ -226,7 +227,7 @@ switch ($Verb) {
|
||||
SourceMB = $(if (Test-Path $src) { [math]::Round(((Get-ChildItem $src -Recurse -File -Force -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum) / 1MB, 1) } else { 0 })
|
||||
Archives = $arch
|
||||
LastLog = (Get-ChildItem 'C:\BakNRet-Lab\logs' -Filter 'backup-*.log' -ErrorAction SilentlyContinue |
|
||||
Sort-Object LastWriteTime | Select-Object -Last 1 -ExpandProperty Name)
|
||||
Sort-Object LastWriteTime | Select-Object -Last 1 -ExpandProperty Name)
|
||||
}
|
||||
}
|
||||
Write-Host ("VM 内 : 供给={0} {1} (build {2}) PS={3}" -f $g.Provisioned, $g.OsCaption, $g.OsBuild, $g.GuestPS)
|
||||
@@ -235,9 +236,11 @@ switch ($Verb) {
|
||||
if ($g.Archives.Count -gt 0) {
|
||||
Write-Host ("沙盒归档 : {0} 个({1} MB 合计)" -f $g.Archives.Count, [math]::Round((($g.Archives | Measure-Object MB -Sum).Sum), 1))
|
||||
$g.Archives | Sort-Object MB -Descending | ForEach-Object { Write-Host (" {0,-52} {1,8} MB" -f $_.Name, $_.MB) }
|
||||
} else { Write-Host '沙盒归档 : (还没有,跑 Lab.ps1 backup)' }
|
||||
}
|
||||
else { Write-Host '沙盒归档 : (还没有,跑 Lab.ps1 backup)' }
|
||||
if ($g.LastLog) { Write-Host ("最近备份日志: {0}" -f $g.LastLog) }
|
||||
} catch {
|
||||
}
|
||||
catch {
|
||||
Write-Host ("VM 内查询失败(可能还没起来):{0}" -f $_.Exception.Message) -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
@@ -337,8 +340,8 @@ switch ($Verb) {
|
||||
'test' {
|
||||
$map = [ordered]@{
|
||||
pester = @{ Path = 'tests\Run-Pester.ps1'; Args = @(); Name = 'Pester 套件' }
|
||||
zero = @{ Path = 'tests\Run-Tests.ps1'; Args = @(); Name = '零依赖套件' }
|
||||
e2e = @{ Path = 'tests\Run-E2E.ps1'; Args = @(); Name = '端到端验收' }
|
||||
zero = @{ Path = 'tests\Run-Tests.ps1'; Args = @(); Name = '零依赖套件' }
|
||||
e2e = @{ Path = 'tests\Run-E2E.ps1'; Args = @(); Name = '端到端验收' }
|
||||
}
|
||||
$pick = if ($Suite -eq 'all') { @($map.Keys) } else { @($Suite) }
|
||||
|
||||
@@ -367,7 +370,7 @@ switch ($Verb) {
|
||||
$color = if ($_.ExitCode -eq 0) { 'Green' } else { 'Red' }
|
||||
Write-Host (" {0,-14} 退出码 {1} 日志 {2}" -f $_.Suite, $_.ExitCode, $_.Log) -ForegroundColor $color
|
||||
}
|
||||
$bad = @($results | Where-Object ExitCode -ne 0)
|
||||
$bad = @($results | Where-Object ExitCode -NE 0)
|
||||
if ($bad.Count -gt 0) { throw ("有 {0} 套件失败" -f $bad.Count) }
|
||||
}
|
||||
|
||||
@@ -398,7 +401,7 @@ switch ($Verb) {
|
||||
|
||||
'reset' {
|
||||
if (-not $CheckpointName) { $CheckpointName = $cfg.CheckpointName }
|
||||
$snap = Get-VMSnapshot -VMName $cfg.VmName -ErrorAction SilentlyContinue | Where-Object Name -eq $CheckpointName
|
||||
$snap = Get-VMSnapshot -VMName $cfg.VmName -ErrorAction SilentlyContinue | Where-Object Name -EQ $CheckpointName
|
||||
if (-not $snap) { throw "找不到检查点 $CheckpointName" }
|
||||
Write-LabLog "回到检查点 $CheckpointName" 'STEP'
|
||||
Restore-VMSnapshot -VMSnapshot $snap -Confirm:$false
|
||||
|
||||
Reference in new issue
Block a user