diff --git a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 index d26bc41dd5..1a11a668fd 100644 --- a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 @@ -10,11 +10,26 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag $systemConfigPath = (Get-PowerShellConfiguration -Scope AllUsers).Path $userConfigPath = (Get-PowerShellConfiguration -Scope CurrentUser).Path + # The legacy $PSHOME config may exist (e.g., preview builds). + # Back it up so tests start clean. Also track it separately because + # writes go to the new platform location, not the legacy path. + $legacyConfigPath = Join-Path $PSHOME "powershell.config.json" + $legacyConfigExists = $false + if (($legacyConfigPath -ne $systemConfigPath) -and (Test-Path $legacyConfigPath)) { + $legacyConfigExists = $true + Move-Item $legacyConfigPath "$legacyConfigPath.backup" -Force -ErrorAction SilentlyContinue + } + $systemConfigDir = Split-Path $systemConfigPath if (!(Test-Path $systemConfigDir)) { $null = New-Item -ItemType Directory -Path $systemConfigDir -Force -ErrorAction SilentlyContinue } + $userConfigDir = Split-Path $userConfigPath + if (!(Test-Path $userConfigDir)) { + $null = New-Item -ItemType Directory -Path $userConfigDir -Force -ErrorAction SilentlyContinue + } + $systemConfigExists = $false if (Test-Path $systemConfigPath) { $systemConfigExists = $true @@ -33,6 +48,10 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag } AfterAll { + if ($legacyConfigExists) { + Move-Item "$legacyConfigPath.backup" $legacyConfigPath -Force -ErrorAction SilentlyContinue + } + if ($systemConfigExists) { Move-Item "$systemConfigPath.backup" $systemConfigPath -Force -ErrorAction SilentlyContinue } @@ -47,6 +66,9 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag AfterEach { Remove-Item $systemConfigPath -Force -ErrorAction SilentlyContinue Remove-Item $userConfigPath -Force -ErrorAction SilentlyContinue + if ($legacyConfigPath -ne $systemConfigPath) { + Remove-Item $legacyConfigPath -Force -ErrorAction SilentlyContinue + } } It "Enable-ExperimentalFeature will enable Experimental Feature for scope: " -TestCases @( diff --git a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 index 47ad3c8413..dce719c896 100644 --- a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 @@ -10,11 +10,23 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" $systemConfigPath = (Get-PowerShellConfiguration -Scope AllUsers).Path $userConfigPath = (Get-PowerShellConfiguration -Scope CurrentUser).Path + $legacyConfigPath = Join-Path $PSHOME "powershell.config.json" + $legacyConfigExists = $false + if (($legacyConfigPath -ne $systemConfigPath) -and (Test-Path $legacyConfigPath)) { + $legacyConfigExists = $true + Move-Item $legacyConfigPath "$legacyConfigPath.backup" -Force -ErrorAction SilentlyContinue + } + $systemConfigDir = Split-Path $systemConfigPath if (!(Test-Path $systemConfigDir)) { $null = New-Item -ItemType Directory -Path $systemConfigDir -Force -ErrorAction SilentlyContinue } + $userConfigDir = Split-Path $userConfigPath + if (!(Test-Path $userConfigDir)) { + $null = New-Item -ItemType Directory -Path $userConfigDir -Force -ErrorAction SilentlyContinue + } + $systemConfigExists = $false if (Test-Path $systemConfigPath) { $systemConfigExists = $true @@ -34,6 +46,10 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } AfterAll { + if ($legacyConfigExists) { + Move-Item "$legacyConfigPath.backup" $legacyConfigPath -Force -ErrorAction SilentlyContinue + } + if ($systemConfigExists -and (Test-CanWriteToSystemConfigDir)) { Move-Item "$systemConfigPath.backup" $systemConfigPath -Force -ErrorAction SilentlyContinue } @@ -51,6 +67,9 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } Remove-Item $userConfigPath -Force -ErrorAction SilentlyContinue + if ($legacyConfigPath -ne $systemConfigPath) { + Remove-Item $legacyConfigPath -Force -ErrorAction SilentlyContinue + } } Context "Feature disabled tests" { diff --git a/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 b/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 index 6f7bb5e408..7dcbaa2bb0 100644 --- a/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 +++ b/test/powershell/engine/PSConfiguration/Get-PowerShellConfiguration.Tests.ps1 @@ -6,8 +6,9 @@ Describe "Get-PowerShellConfiguration Tests" -Tags "CI" { It "Returns both scopes when no -Scope is specified" { $configs = Get-PowerShellConfiguration $configs | Should -HaveCount 2 - $configs[0].Scope | Should -Be "AllUsers" - $configs[1].Scope | Should -Be "CurrentUser" + $scopes = $configs.Scope + $scopes | Should -Contain "AllUsers" + $scopes | Should -Contain "CurrentUser" } It "Returns AllUsers scope when -Scope AllUsers is specified" { diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index f90b275bba..034683be2b 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -355,24 +355,30 @@ function Test-CanWriteToSystemConfigDir { $script:CanWriteToSystemConfigDir = $false $dir = Split-Path (Get-PowerShellConfiguration -Scope AllUsers).Path + $createdDir = $false if (!(Test-Path $dir)) { try { $null = New-Item -ItemType Directory -Path $dir -Force -ErrorAction Stop + $createdDir = $true $script:CanWriteToSystemConfigDir = $true } catch { ; # do nothing } } else { try { - $testFile = Join-Path $dir ".pester-write-test" - Set-Content -Path $testFile -Value '' -ErrorAction Stop - Remove-Item $testFile -Force -ErrorAction SilentlyContinue + $testFileName = Join-Path $dir (New-Guid).Guid + $null = New-Item -ItemType File -Path $testFileName -ErrorAction Stop $script:CanWriteToSystemConfigDir = $true + Remove-Item -Path $testFileName -ErrorAction SilentlyContinue } catch { ; # do nothing } } + if ($createdDir) { + Remove-Item $dir -Force -ErrorAction SilentlyContinue + } + $script:CanWriteToSystemConfigDir }