feat(tui): 名录编辑器循环 + 挂进 Edit-Config(第三轮第二块·完成)
装配:Get-BakNRetCatalogField → 菜单选字段 → Read-BakNRetLine → Set-BakNRetCatalogField → Save-BakNRetConfigFile(校验通过才原子替换 + 时间戳备份)。 菜单里**只列可编辑的字段**(单行的 Encrypt / Description):把只读的 Path / Exclude 也列出来再拒绝,会让每个只读项浪费用户一次回车。 校验用模块自己的 Import-BaknretDataFile:这份名录有 $( ) 动态表达式与跨行拼接,普通数据文件读取器读不了 —— 而正是它保证"改完整份文件还能被程序读回来"(值能读回来才算改成功)。定位用 (软件名, Slot, 字段名) 三元组,因为同一软件可以有多个 Slot。 Edit-Config 的三个界面(清单 / 设置 / 名录)至此全部完成。 判据 9 条:改一个值后只动一行、注释行数不变、改完能读回新值、留下时间戳备份且备份里是原文;取消时不写盘也不产生备份。 验收:test.ps1 9/9 全绿(5.1 与 7);真实清单只读冒烟 4/4。
This commit is contained in:
1 parent
8c018533c9
commit
effc38fdd3
5 files changed
+151
-2
No files matched your search
@@ -1865,6 +1865,39 @@ Test-Case '名录字段:只有单行的 Encrypt / Description 可编辑;改
|
||||
}
|
||||
}
|
||||
|
||||
Test-Case '名录编辑器:脚本驱动改一个 Encrypt,只动那一行且留下备份;取消不写盘' {
|
||||
$dir = Join-Path $env:TEMP ("bnr-cated-" + [guid]::NewGuid().ToString('N').Substring(0, 8))
|
||||
New-Item -ItemType Directory -Path $dir -Force | Out-Null
|
||||
$file = Join-Path $dir 'SoftwareCatalog.psd1'
|
||||
Copy-Item -LiteralPath (Join-Path (Split-Path -Parent $PSScriptRoot) 'SoftwareCatalog.psd1') -Destination $file
|
||||
$original = [System.IO.File]::ReadAllText($file)
|
||||
$backupDir = Join-Path $dir 'logs\config-backups'
|
||||
try {
|
||||
# 第一个可编辑字段是 AutoDarkMode.DefaultData.Encrypt(当前 $true)→ 输入 $false
|
||||
$keys = @('Enter', 'Char:$', 'Char:f', 'Char:a', 'Char:l', 'Char:s', 'Char:e', 'Enter')
|
||||
$result = Invoke-BakNRetCatalogEditor -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 '注释行数不变'
|
||||
$nowValue = ((Get-BakNRetCatalogField -Text ([System.IO.File]::ReadAllText($file)) | Where-Object { $_.App -eq 'AutoDarkMode' -and $_.Slot -eq 'DefaultData' -and $_.Key -eq 'Encrypt' }).Value)
|
||||
Assert-Equal '$false' $nowValue '改完能读回新值'
|
||||
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-BakNRetCatalogEditor -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