feat(tui): 配置编辑器循环 + 挂进 Edit-Config(第二轮第三块·完成)
装配:Get-BakNRetConfigSetting(读)→ 菜单选设置 → Read-BakNRetLine(输入新值)→ Set-BakNRetConfigSetting(只改那一行)→ Save-BakNRetConfigFile(校验通过才原子替换 + 时间戳备份)。 输入行**从空开始**、当前值只作提示:预填当前值看着贴心,实际等于逼用户先删一遍(对 'Backups' 这种带引号的原文尤其别扭)。 校验用 Import-PowerShellDataFile:这份配置里没有动态表达式,能被它读回来是个**强校验** —— 比"语法能不能过"更强,它还能抓出"值是合法语法、但结构不对"。它只接受文件路径,所以先把候选文本写到临时文件再读。 Edit-Config 的 -Target Settings 与菜单里的"设置"都挂上了同一个编辑器;-Target Catalog 仍明确报"还没做(第三轮)"。 判据 9 条:改一个值后只动一行、注释行数不变、新值落盘、留下时间戳备份且备份里是原文;取消时不写盘也不产生备份。 验收:test.ps1 9/9 全绿(5.1 与 7)。
This commit is contained in:
1 parent
92bbc995c4
commit
4aa44e8965
5 files changed
+143
-2
No files matched your search
@@ -1786,6 +1786,38 @@ Test-Case '输入行:字符追加、Backspace 删除、Esc 取消返回 $null
|
||||
Assert-Equal 'k' (Read-BakNRetLine -Driver (New-BakNRetInputDriver -Script @('Char:K', 'Enter'))) '字母按小写存(与键名归一化一致)'
|
||||
}
|
||||
|
||||
Test-Case '配置编辑器:脚本驱动改一个值,只动那一行且留下备份;取消不写盘' {
|
||||
$dir = Join-Path $env:TEMP ("bnr-cfged-" + [guid]::NewGuid().ToString('N').Substring(0, 8))
|
||||
New-Item -ItemType Directory -Path $dir -Force | Out-Null
|
||||
$file = Join-Path $dir 'BackupConfig.psd1'
|
||||
Copy-Item -LiteralPath (Join-Path (Split-Path -Parent $PSScriptRoot) 'BackupConfig.psd1') -Destination $file
|
||||
$original = [System.IO.File]::ReadAllText($file)
|
||||
$backupDir = Join-Path $dir 'logs\config-backups'
|
||||
try {
|
||||
# MinFreeSpaceGB 是文件里第 5 个标量设置:下移四次选中它,输入 9
|
||||
$keys = @('Down', 'Down', 'Down', 'Down', 'Enter', 'Char:9', 'Enter')
|
||||
$result = Invoke-BakNRetConfigEditor -Path $file -Driver (New-BakNRetInputDriver -Script $keys)
|
||||
Assert-Equal $true $result.Saved '应当保存'
|
||||
$before = @($original -split "`n")
|
||||
$after = @([System.IO.File]::ReadAllText($file) -split "`n")
|
||||
Assert-Equal 2 @(Compare-Object -ReferenceObject $before -DifferenceObject $after).Count '只应当有一行不同'
|
||||
Assert-Equal @($before | Where-Object { $_.TrimStart().StartsWith('#') }).Count @($after | Where-Object { $_.TrimStart().StartsWith('#') }).Count '注释行数不变'
|
||||
Assert-True (([System.IO.File]::ReadAllText($file)) -match 'MinFreeSpaceGB = 9') '新值写进去了'
|
||||
Assert-True (Test-Path $result.BackupPath) '应当留下时间戳备份'
|
||||
Assert-Equal $original ([System.IO.File]::ReadAllText($result.BackupPath)) '备份里是原文'
|
||||
|
||||
# 取消(选设置时 Esc):不写盘、不产生新备份
|
||||
$countBefore = @(Get-ChildItem $backupDir -File | Measure-Object).Count
|
||||
$contentBefore = [System.IO.File]::ReadAllText($file)
|
||||
$cancelled = Invoke-BakNRetConfigEditor -Path $file -Driver (New-BakNRetInputDriver -Script @('Esc'))
|
||||
Assert-Equal $false $cancelled.Saved '取消不该保存'
|
||||
Assert-Equal $contentBefore ([System.IO.File]::ReadAllText($file)) '取消后内容不变'
|
||||
Assert-Equal $countBefore @(Get-ChildItem $backupDir -File | Measure-Object).Count '取消不该新增备份'
|
||||
} finally {
|
||||
Remove-Item -LiteralPath $dir -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
Remove-Item -LiteralPath $sandbox -Recurse -Force -ErrorAction SilentlyContinue
|
||||
Remove-Item -LiteralPath $itemSandbox -Recurse -Force -ErrorAction SilentlyContinue
|
||||
|
||||
Reference in new issue
Block a user