diff --git a/PowerShell/system-administration/maintenance/system-maintenance.ps1 b/PowerShell/system-administration/maintenance/system-maintenance.ps1 index b6b6a3d..51c051e 100644 --- a/PowerShell/system-administration/maintenance/system-maintenance.ps1 +++ b/PowerShell/system-administration/maintenance/system-maintenance.ps1 @@ -46,6 +46,10 @@ $ErrorActionPreference = 'Stop' function Get-LogFilePath { $userDocs = [Environment]::GetFolderPath('MyDocuments') + # Fallback to temp directory if MyDocuments is not available + if ([string]::IsNullOrWhiteSpace($userDocs)) { + $userDocs = [System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) + } $logRoot = Join-Path $userDocs 'SystemLogs' if (-not (Test-Path $logRoot)) { New-Item -Path $logRoot -ItemType Directory -Force | Out-Null } $timestamp = (Get-Date).ToString('yyyy-MM-dd_HH-mm-ss') diff --git a/tests/unit/PowerShell/system-maintenance.Tests.ps1 b/tests/unit/PowerShell/system-maintenance.Tests.ps1 index b9c4645..89e19d6 100644 --- a/tests/unit/PowerShell/system-maintenance.Tests.ps1 +++ b/tests/unit/PowerShell/system-maintenance.Tests.ps1 @@ -3,8 +3,19 @@ BeforeAll { # Suppress verbose output from the script itself during tests $VerbosePreference = 'SilentlyContinue' - # Path to the script being tested - $scriptPath = "$PSScriptRoot/../../../PowerShell/system-administration/maintenance/system-maintenance.ps1" + # Path to the script being tested - resolve to absolute path + $testDir = $PSScriptRoot + if (-not $testDir) { + $testDir = Split-Path -Parent $MyInvocation.MyCommand.Path + } + if (-not $testDir) { + $testDir = Get-Location + } + $relativePath = Join-Path $testDir "../../../PowerShell/system-administration/maintenance/system-maintenance.ps1" + if (-not (Test-Path $relativePath)) { + throw "Script not found at: $relativePath" + } + $script:scriptPath = Resolve-Path $relativePath | Select-Object -ExpandProperty Path } Describe "system-maintenance.ps1" { @@ -14,20 +25,22 @@ Describe "system-maintenance.ps1" { } It "should have comment-based help" { - $help = Get-Help -Path $scriptPath -ErrorAction SilentlyContinue + $help = Get-Help $scriptPath -ErrorAction SilentlyContinue $help | Should -Not -BeNull - ($help.Name -eq 'system-maintenance') | Should -Be $true + $help.Name | Should -Be 'system-maintenance.ps1' } It "should support -WhatIf" { - $command = Get-Command -Path $scriptPath + $command = Get-Command -Name $scriptPath $command.Parameters.Keys | Should -Contain 'WhatIf' } } Context "Execution Smoke Test" { It "should run without throwing errors with default parameters" { - { & $scriptPath -WhatIf } | Should -Not -Throw + # Capture the path in a local variable to ensure it's available in the scriptblock + $localPath = $scriptPath + { & $localPath -WhatIf } | Should -Not -Throw } } }