mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-11-26 07:26:18 +01:00
fix: read JSON as UTF-8 so BOM-less packs load correctly (#844)
This commit is contained in:
@@ -384,7 +384,7 @@ function Get-AppRemovalMethod {
|
|||||||
$script:AppRemovalMethodCache = @{}
|
$script:AppRemovalMethodCache = @{}
|
||||||
try {
|
try {
|
||||||
if (Test-Path $script:AppsListFilePath) {
|
if (Test-Path $script:AppsListFilePath) {
|
||||||
$appsJson = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json
|
$appsJson = Get-Content -Path $script:AppsListFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||||
foreach ($appData in $appsJson.Apps) {
|
foreach ($appData in $appsJson.Apps) {
|
||||||
$rawMethod = $appData.RemovalMethod
|
$rawMethod = $appData.RemovalMethod
|
||||||
$method = if ($rawMethod -and $rawMethod -eq 'WinGet') { 'WinGet' } else { 'Appx' }
|
$method = if ($rawMethod -and $rawMethod -eq 'WinGet') { 'WinGet' } else { 'Appx' }
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ function Import-AppDetailsFromJson {
|
|||||||
|
|
||||||
$apps = @()
|
$apps = @()
|
||||||
try {
|
try {
|
||||||
$jsonContent = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json
|
$jsonContent = Get-Content -Path $script:AppsListFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Error "Failed to read Apps.json: $_"
|
Write-Error "Failed to read Apps.json: $_"
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#>
|
#>
|
||||||
function Import-AppPresetsFromJson {
|
function Import-AppPresetsFromJson {
|
||||||
try {
|
try {
|
||||||
$jsonContent = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json
|
$jsonContent = Get-Content -Path $script:AppsListFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
Write-Warning "Failed to read Apps.json: $_"
|
Write-Warning "Failed to read Apps.json: $_"
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ function Import-AppsFromFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$jsonContent = Get-Content -Path $appsFilePath -Raw | ConvertFrom-Json
|
$jsonContent = Get-Content -Path $appsFilePath -Raw -Encoding UTF8 | ConvertFrom-Json
|
||||||
Foreach ($appData in $jsonContent.Apps) {
|
Foreach ($appData in $jsonContent.Apps) {
|
||||||
# Handle AppId as array (could be single or multiple IDs)
|
# Handle AppId as array (could be single or multiple IDs)
|
||||||
$appIdArray = if ($appData.AppId -is [array]) { $appData.AppId } else { @($appData.AppId) }
|
$appIdArray = if ($appData.AppId -is [array]) { $appData.AppId } else { @($appData.AppId) }
|
||||||
|
|||||||
@@ -187,6 +187,14 @@ Describe 'Get-AppRemovalMethod' {
|
|||||||
Get-AppRemovalMethod -appId 'Unknown.App' | Should -Be 'Appx'
|
Get-AppRemovalMethod -appId 'Unknown.App' | Should -Be 'Appx'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It 'resolves WinGet removal for non-ASCII IDs from BOM-less UTF-8 JSON' {
|
||||||
|
$appId = 'Test.' + [char]0x00E9 + [char]0x4E2D + [char]0x0416
|
||||||
|
$json = @{ Apps = @(@{ AppId = $appId; RemovalMethod = 'WinGet' }) } | ConvertTo-Json -Depth 4
|
||||||
|
[System.IO.File]::WriteAllText($script:AppsListFilePath, $json, [System.Text.UTF8Encoding]::new($false))
|
||||||
|
|
||||||
|
Get-AppRemovalMethod -appId $appId | Should -Be 'WinGet'
|
||||||
|
}
|
||||||
|
|
||||||
It 'warns and defaults to Appx when the catalog is malformed' {
|
It 'warns and defaults to Appx when the catalog is malformed' {
|
||||||
'not json' | Set-Content -LiteralPath $script:AppsListFilePath
|
'not json' | Set-Content -LiteralPath $script:AppsListFilePath
|
||||||
Mock Write-Warning {}
|
Mock Write-Warning {}
|
||||||
|
|||||||
@@ -117,6 +117,15 @@ Describe 'Save-Settings' {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Describe 'Import-AppsFromFile' {
|
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' {
|
It 'returns selected IDs, trims whitespace, and supports scalar and array AppId values' {
|
||||||
$path = Join-Path $TestDrive 'apps.json'
|
$path = Join-Path $TestDrive 'apps.json'
|
||||||
'{"Apps":[{"AppId":" One.App ","SelectedByDefault":true},{"AppId":["Two.App"," "],"SelectedByDefault":true},{"AppId":"Ignored.App","SelectedByDefault":false}]}' |
|
'{"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'
|
$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' {
|
It 'returns empty collections and reports malformed JSON' {
|
||||||
'not json' | Set-Content -LiteralPath $script:AppsListFilePath
|
'not json' | Set-Content -LiteralPath $script:AppsListFilePath
|
||||||
Mock Write-Error {}
|
Mock Write-Error {}
|
||||||
|
|||||||
Reference in New Issue
Block a user