From dd929f8eec19622723e706e5d69510e974fb309a Mon Sep 17 00:00:00 2001 From: Jeffrey <9938813+Raphire@users.noreply.github.com> Date: Wed, 12 Aug 2026 17:31:50 +0200 Subject: [PATCH] Surface errors when writing to file --- Scripts/FileIO/Save-ToFile.ps1 | 3 ++- Tests/Settings-And-Apps-FileIO.Tests.ps1 | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Scripts/FileIO/Save-ToFile.ps1 b/Scripts/FileIO/Save-ToFile.ps1 index 10a38a9..f989a2b 100644 --- a/Scripts/FileIO/Save-ToFile.ps1 +++ b/Scripts/FileIO/Save-ToFile.ps1 @@ -27,10 +27,11 @@ function Save-ToFile { ) try { - $Config | ConvertTo-Json -Depth $MaxDepth | Set-Content -Path $FilePath -Encoding UTF8 + $Config | ConvertTo-Json -Depth $MaxDepth | Set-Content -Path $FilePath -Encoding UTF8 -ErrorAction Stop return $true } catch { + Write-Error "Failed to write '$FilePath': $($_.Exception.Message)" return $false } } diff --git a/Tests/Settings-And-Apps-FileIO.Tests.ps1 b/Tests/Settings-And-Apps-FileIO.Tests.ps1 index d84cb06..d89765d 100644 --- a/Tests/Settings-And-Apps-FileIO.Tests.ps1 +++ b/Tests/Settings-And-Apps-FileIO.Tests.ps1 @@ -14,6 +14,19 @@ BeforeAll { $script:JsonFixturePath = Join-Path $PSScriptRoot 'TestData\JsonFileLoading' } +Describe 'Save-ToFile' { + It 'reports the write error and returns false when persistence fails' { + Mock Set-Content { throw [System.IO.IOException]::new('Disk is full') } + Mock Write-Error {} + + Save-ToFile -Config @{ Setting = $true } -FilePath (Join-Path $TestDrive 'settings.json') | Should -BeFalse + + Should -Invoke Write-Error -Times 1 -Exactly -ParameterFilter { + $Message -eq "Failed to write '$TestDrive\settings.json': Disk is full" + } + } +} + Describe 'Import-Settings' { BeforeEach { $script:Params = @{}