diff --git a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 index c61b4ebb34..2258b321c3 100644 --- a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 @@ -3,22 +3,32 @@ Import-Module HelpersCommon +function Test-CanWriteToSystemConfigDir { + $dir = Split-Path (Get-PowerShellConfiguration -Scope AllUsers).Path + if (!(Test-Path $dir)) { + try { + $null = New-Item -ItemType Directory -Path $dir -Force -ErrorAction Stop + return $true + } catch { + return $false + } + } + try { + $testFile = Join-Path $dir ".pester-write-test" + Set-Content -Path $testFile -Value '' -ErrorAction Stop + Remove-Item $testFile -Force -ErrorAction SilentlyContinue + return $true + } catch { + return $false + } +} + Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tags "Feature","RequireAdminOnWindows" { BeforeAll { $pwsh = "$PSHOME/pwsh" - if ($IsWindows) { - $systemConfigPath = Join-Path $env:ProgramData "Microsoft\PowerShell\powershell.config.json" - } - else { - $systemConfigPath = "/etc/powershell/powershell.config.json" - } - if ($IsWindows) { - $userConfigPath = "~/Documents/powershell/powershell.config.json" - } - else { - $userConfigPath = "~/.config/powershell/powershell.config.json" - } + $systemConfigPath = (Get-PowerShellConfiguration -Scope AllUsers).Path + $userConfigPath = (Get-PowerShellConfiguration -Scope CurrentUser).Path $systemConfigDir = Split-Path $systemConfigPath if (!(Test-Path $systemConfigDir)) { @@ -65,7 +75,7 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag ) { param ($scope) - if (!(Test-CanWriteToPsHome) -and $scope -eq "AllUsers") { + if (!(Test-CanWriteToSystemConfigDir) -and $scope -eq "AllUsers") { return } @@ -83,7 +93,7 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag ) { param ($scope, $configPath) - if (!(Test-CanWriteToPsHome) -and $scope -eq "AllUsers") { + if (!(Test-CanWriteToSystemConfigDir) -and $scope -eq "AllUsers") { return } diff --git a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 index 6029532950..f3a39f312d 100644 --- a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 @@ -3,22 +3,32 @@ Import-Module HelpersCommon +function Test-CanWriteToSystemConfigDir { + $dir = Split-Path (Get-PowerShellConfiguration -Scope AllUsers).Path + if (!(Test-Path $dir)) { + try { + $null = New-Item -ItemType Directory -Path $dir -Force -ErrorAction Stop + return $true + } catch { + return $false + } + } + try { + $testFile = Join-Path $dir ".pester-write-test" + Set-Content -Path $testFile -Value '' -ErrorAction Stop + Remove-Item $testFile -Force -ErrorAction SilentlyContinue + return $true + } catch { + return $false + } +} + Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" { BeforeAll { $pwsh = "$PSHOME/pwsh" - if ($IsWindows) { - $systemConfigPath = Join-Path $env:ProgramData "Microsoft\PowerShell\powershell.config.json" - } - else { - $systemConfigPath = "/etc/powershell/powershell.config.json" - } - if ($IsWindows) { - $userConfigPath = "~/Documents/powershell/powershell.config.json" - } - else { - $userConfigPath = "~/.config/powershell/powershell.config.json" - } + $systemConfigPath = (Get-PowerShellConfiguration -Scope AllUsers).Path + $userConfigPath = (Get-PowerShellConfiguration -Scope CurrentUser).Path $systemConfigDir = Split-Path $systemConfigPath if (!(Test-Path $systemConfigDir)) { @@ -44,7 +54,7 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } AfterAll { - if ($systemConfigExists -and (Test-CanWriteToPsHome)) { + if ($systemConfigExists -and (Test-CanWriteToSystemConfigDir)) { Move-Item "$systemConfigPath.backup" $systemConfigPath -Force -ErrorAction SilentlyContinue } @@ -56,7 +66,7 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } AfterEach { - if (Test-CanWriteToPsHome) { + if (Test-CanWriteToSystemConfigDir) { Remove-Item $systemConfigPath -Force -ErrorAction SilentlyContinue } @@ -129,7 +139,7 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } Context "User config takes precedence over system config" { - It "Feature is enabled in user config only" -Skip:(!(Test-CanWriteToPsHome)) { + It "Feature is enabled in user config only" -Skip:(!(Test-CanWriteToSystemConfigDir)) { '{"ExperimentalFeatures":["ExpTest.FeatureOne"]}' > $userConfigPath '{"ExperimentalFeatures":["ExpTest.FeatureTwo"]}' > $systemConfigPath diff --git a/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 b/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 index 3eac4efd94..66e79dafe5 100644 --- a/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 +++ b/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 @@ -37,7 +37,11 @@ Describe "Get-PowerShellConfiguration Tests" -Tags "CI" { $config.Path | Should -BeLike "*powershell.config.json" $config.Path | Should -Not -BeLike "*ProgramData*" if (-not $IsWindows) { - $config.Path | Should -BeLike "*/.config/powershell/*" + $expectedBase = $env:XDG_CONFIG_HOME + if (-not $expectedBase) { + $expectedBase = Join-Path $HOME ".config" + } + $config.Path | Should -BeLike "$expectedBase/powershell/*" } }