diff --git a/Scripts/AppRemoval/Remove-SelectedApps.ps1 b/Scripts/AppRemoval/Remove-SelectedApps.ps1 index 897719c..98c08b1 100644 --- a/Scripts/AppRemoval/Remove-SelectedApps.ps1 +++ b/Scripts/AppRemoval/Remove-SelectedApps.ps1 @@ -384,7 +384,7 @@ function Get-AppRemovalMethod { $script:AppRemovalMethodCache = @{} try { 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) { $rawMethod = $appData.RemovalMethod $method = if ($rawMethod -and $rawMethod -eq 'WinGet') { 'WinGet' } else { 'Appx' } diff --git a/Scripts/FileIO/Import-AppDetailsFromJson.ps1 b/Scripts/FileIO/Import-AppDetailsFromJson.ps1 index 684adeb..5dbb639 100644 --- a/Scripts/FileIO/Import-AppDetailsFromJson.ps1 +++ b/Scripts/FileIO/Import-AppDetailsFromJson.ps1 @@ -30,7 +30,7 @@ function Import-AppDetailsFromJson { $apps = @() try { - $jsonContent = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json + $jsonContent = Get-Content -Path $script:AppsListFilePath -Raw -Encoding UTF8 | ConvertFrom-Json } catch { Write-Error "Failed to read Apps.json: $_" diff --git a/Scripts/FileIO/Import-AppPresetsFromJson.ps1 b/Scripts/FileIO/Import-AppPresetsFromJson.ps1 index 6113124..8a7e69e 100644 --- a/Scripts/FileIO/Import-AppPresetsFromJson.ps1 +++ b/Scripts/FileIO/Import-AppPresetsFromJson.ps1 @@ -4,7 +4,7 @@ #> function Import-AppPresetsFromJson { try { - $jsonContent = Get-Content -Path $script:AppsListFilePath -Raw | ConvertFrom-Json + $jsonContent = Get-Content -Path $script:AppsListFilePath -Raw -Encoding UTF8 | ConvertFrom-Json } catch { Write-Warning "Failed to read Apps.json: $_" diff --git a/Scripts/FileIO/Import-AppsFromFile.ps1 b/Scripts/FileIO/Import-AppsFromFile.ps1 index f9bb534..69db3fc 100644 --- a/Scripts/FileIO/Import-AppsFromFile.ps1 +++ b/Scripts/FileIO/Import-AppsFromFile.ps1 @@ -26,7 +26,7 @@ function Import-AppsFromFile { } try { - $jsonContent = Get-Content -Path $appsFilePath -Raw | ConvertFrom-Json + $jsonContent = Get-Content -Path $appsFilePath -Raw -Encoding UTF8 | ConvertFrom-Json Foreach ($appData in $jsonContent.Apps) { # Handle AppId as array (could be single or multiple IDs) $appIdArray = if ($appData.AppId -is [array]) { $appData.AppId } else { @($appData.AppId) } diff --git a/Tests/Remove-SelectedApps.Tests.ps1 b/Tests/Remove-SelectedApps.Tests.ps1 index 6ad7d96..785a7d2 100644 --- a/Tests/Remove-SelectedApps.Tests.ps1 +++ b/Tests/Remove-SelectedApps.Tests.ps1 @@ -187,6 +187,14 @@ Describe 'Get-AppRemovalMethod' { 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' { 'not json' | Set-Content -LiteralPath $script:AppsListFilePath Mock Write-Warning {} diff --git a/Tests/Settings-And-Apps-FileIO.Tests.ps1 b/Tests/Settings-And-Apps-FileIO.Tests.ps1 index eed011a..b101b89 100644 --- a/Tests/Settings-And-Apps-FileIO.Tests.ps1 +++ b/Tests/Settings-And-Apps-FileIO.Tests.ps1 @@ -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 {}