fix: read JSON as UTF-8 so BOM-less packs load correctly (#844)

This commit is contained in:
wang13zhe
2026-10-21 16:54:20 +02:00
committed by GitHub
parent 55b7b48bb9
commit 97f3f805ef
6 changed files with 47 additions and 4 deletions
+35
View File
@@ -117,6 +117,15 @@ Describe 'Save-Settings' {
}
Describe 'Import-AppsFromFile' {
It 'preserves selected non-ASCII IDs from BOM-less UTF-8 JSON' {
$path = Join-Path $TestDrive 'apps-utf8.json'
$appId = 'Test.' + [char]0x00E9 + [char]0x4E2D + [char]0x0416
$json = @{ Apps = @(@{ AppId = $appId; SelectedByDefault = $true }) } | ConvertTo-Json -Depth 4
[System.IO.File]::WriteAllText($path, $json, [System.Text.UTF8Encoding]::new($false))
@(Import-AppsFromFile -appsFilePath $path) | Should -Be @($appId)
}
It 'returns selected IDs, trims whitespace, and supports scalar and array AppId values' {
$path = Join-Path $TestDrive 'apps.json'
'{"Apps":[{"AppId":" One.App ","SelectedByDefault":true},{"AppId":["Two.App"," "],"SelectedByDefault":true},{"AppId":"Ignored.App","SelectedByDefault":false}]}' |
@@ -215,6 +224,32 @@ Describe 'Import-AppDetailsFromJson' {
$presets[0].AppIds | Should -Be 'One.App'
}
It 'preserves non-ASCII app IDs and fallback names from BOM-less UTF-8 JSON' {
$appId = 'Test.' + [char]0x00E9 + [char]0x4E2D + [char]0x0416
$json = @{ Apps = @(@{ AppId = $appId }) } | ConvertTo-Json -Depth 4
[System.IO.File]::WriteAllText($script:AppsListFilePath, $json, [System.Text.UTF8Encoding]::new($false))
$apps = @(Import-AppDetailsFromJson)
$apps | Should -HaveCount 1
$apps[0].AppId | Should -Be @($appId)
$apps[0].FriendlyName | Should -Be $appId
$apps[0].DisplayName | Should -Be "$appId ($appId)"
}
It 'preserves non-ASCII preset names and IDs from BOM-less UTF-8 JSON' {
$presetName = 'Preset ' + [char]0x00E9 + [char]0x4E2D + [char]0x0416
$appId = 'Test.' + [char]0x4E2D
$json = @{ Presets = @(@{ Name = $presetName; AppIds = @($appId) }) } | ConvertTo-Json -Depth 4
[System.IO.File]::WriteAllText($script:AppsListFilePath, $json, [System.Text.UTF8Encoding]::new($false))
$presets = @(Import-AppPresetsFromJson)
$presets | Should -HaveCount 1
$presets[0].Name | Should -Be $presetName
$presets[0].AppIds | Should -Be @($appId)
}
It 'returns empty collections and reports malformed JSON' {
'not json' | Set-Content -LiteralPath $script:AppsListFilePath
Mock Write-Error {}