diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/PSSessionConfiguration.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/PSSessionConfiguration.Tests.ps1 index f86eb6600..4559109e8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/PSSessionConfiguration.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/PSSessionConfiguration.Tests.ps1 @@ -1,12 +1,16 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + +Import-Module HelpersCommon + try { # Skip all tests on non-windows and non-PowerShellCore and non-elevated platforms. $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() $originalWarningPreference = $WarningPreference $WarningPreference = "SilentlyContinue" - $IsNotSkipped = ($IsWindows -and $IsCoreCLR -and (Test-IsElevated)) + # Skip all tests if can't write to $PSHOME as Register-PSSessionConfiguration writes to $PSHOME + $IsNotSkipped = ($IsWindows -and $IsCoreCLR -and (Test-IsElevated) -and (Test-CanWriteToPsHome)) $PSDefaultParameterValues["it:skip"] = !$IsNotSkipped # diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 index 86842457e..7f0b7707f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/ExecutionPolicy.Tests.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Import-Module HelpersCommon + # # These are general tests that verify non-Windows behavior # @@ -45,7 +47,7 @@ try { $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() $IsNotSkipped = ($IsWindows -eq $true); $PSDefaultParameterValues["it:skip"] = !$IsNotSkipped - $ShouldSkipTest = !$IsNotSkipped + $ShouldSkipTest = !$IsNotSkipped -or !(Test-CanWriteToPsHome) Describe "Help work with ExecutionPolicy Restricted " -Tags "Feature" { @@ -480,7 +482,9 @@ ZoneId=$FileType foreach($fileInfo in $testFilesInfo) { - createTestFile -FilePath $fileInfo.filePath -FileType $fileInfo.fileType -AddSignature:$fileInfo.AddSignature -Corrupted:$fileInfo.corrupted + if ((Test-CanWriteToPsHome) -or (!(Test-CanWriteToPsHome) -and !$fileInfo.filePath.StartsWith($PSHOME, $true, $null)) ) { + createTestFile -FilePath $fileInfo.filePath -FileType $fileInfo.fileType -AddSignature:$fileInfo.AddSignature -Corrupted:$fileInfo.corrupted + } } #Get Execution Policy @@ -651,21 +655,27 @@ ZoneId=$FileType } $error = "UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand" + $testData = @( - @{ - module = $PSHomeUntrustedModule - error = $null - } - @{ - module = $PSHomeUnsignedModule - error = $null - } @{ module = "Microsoft.PowerShell.Archive" error = $null } ) + if (Test-CanWriteToPsHome) { + $testData += @( + @{ + module = $PSHomeUntrustedModule + error = $null + } + @{ + module = $PSHomeUnsignedModule + error = $null + } + ) + } + $TestTypePrefix = "Test 'Unrestricted' execution policy." It "$TestTypePrefix Importing Module should throw ''" -TestCases $testData { param([string]$module, [string]$error) @@ -898,20 +908,25 @@ ZoneId=$FileType $error = "UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand" $testData = @( - @{ - module = $PSHomeUntrustedModule - error = $error - } - @{ - module = $PSHomeUnsignedModule - error = $error - } @{ module = "Microsoft.PowerShell.Archive" error = $null } ) + if (Test-CanWriteToPsHome) { + $testData += @( + @{ + module = $PSHomeUntrustedModule + error = $error + } + @{ + module = $PSHomeUnsignedModule + error = $error + } + ) + } + It "$TestTypePrefix Importing Module should throw ''" -TestCases $testData { param([string]$module, [string]$error) $testScript = {Import-Module -Name $module -Force} @@ -1132,7 +1147,7 @@ ZoneId=$FileType } } - It '-Scope LocalMachine is Settable, but overridden' { + It '-Scope LocalMachine is Settable, but overridden' -Skip:(!(Test-CanWriteToPsHome)) { # In this test, we first setup execution policy in the following way: # CurrentUser is specified and takes precedence over LocalMachine. # That's why we will get an error, when we are setting up LocalMachine policy. @@ -1154,7 +1169,7 @@ ZoneId=$FileType Get-ExecutionPolicy -Scope LocalMachine | Should -Be "ByPass" } - It '-Scope LocalMachine is Settable' { + It '-Scope LocalMachine is Settable' -Skip:(!(Test-CanWriteToPsHome)) { # We need to make sure that both Process and CurrentUser policies are Undefined # before we can set LocalMachine policy without ExecutionPolicyOverride error. Set-ExecutionPolicy -Scope Process -ExecutionPolicy Undefined diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/Modules/ImportUnTrustedManifestWithBinFnExport.psd1 b/test/powershell/Modules/Microsoft.PowerShell.Security/Modules/ImportUnTrustedManifestWithBinFnExport.psd1 new file mode 100644 index 000000000..3f71f5d90 --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/Modules/ImportUnTrustedManifestWithBinFnExport.psd1 @@ -0,0 +1 @@ +@{ ModuleVersion = '1.0'; NestedModules = 'D:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Security\Modules\ImportUnTrustedManifestWithBinFnExport_System32.dll'; CmdletsToExport = 'Invoke-Hello' } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index d98f71081..c16fcffe5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -74,7 +74,7 @@ Describe "Get-Command Feature tests" -Tag Feature { It "Can return multiple results for cmdlets matching abbreviation" { # use mixed casing to validate case insensitivity $results = pwsh -outputformat xml -settingsfile $configFilePath -command "Get-Command i-C -UseAbbreviationExpansion" - $results | Should -HaveCount 3 + $results.Count | Should -BeGreaterOrEqual 3 $results.Name | Should -Contain "Invoke-Command" $results.Name | Should -Contain "Import-Clixml" $results.Name | Should -Contain "Import-Csv" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 index 45ec634ff..78d26ff0a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -129,14 +129,14 @@ Categories=Application; $windows = $shell.Windows() $before = $windows.Count - Invoke-Item -Path $PSHOME + Invoke-Item -Path ~ # may take time for explorer to open window Wait-UntilTrue -sb { $windows.Count -gt $before } -TimeoutInMilliseconds (10*1000) -IntervalInMilliseconds 100 > $null $after = $windows.Count $before + 1 | Should -Be $after $item = $windows.Item($after - 1) - $item.LocationURL | Should -Match ($PSHOME -replace '\\', '/') + $item.LocationURL | Should -Match ((Resolve-Path ~) -replace '\\', '/') ## close the windows explorer $item.Quit() } diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 28f6dbc26..aef7635c4 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -149,9 +149,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } It "Transcription should be closed if the only runspace gets closed" { - $powerShellPath = [System.Diagnostics.Process]::GetCurrentProcess().Path - $powerShellCommand = $powerShellPath + ' -c "start-transcript $transcriptFilePath; Write-Host ''Before Dispose'';"' - Invoke-Expression $powerShellCommand + pwsh -c "start-transcript $transcriptFilePath; Write-Host ''Before Dispose'';" $transcriptFilePath | Should -Exist $transcriptFilePath | Should -FileContentMatch "Before Dispose" @@ -300,7 +298,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Stop-Transcript } - $transcriptMinHeaderFilePath = $transcriptFilePath + "_minimal" + $transcriptMinHeaderFilePath = $transcriptFilePath + "_minimal" $scriptMinHeader = { Start-Transcript -Path $transcriptMinHeaderFilePath -UseMinimalHeader Stop-Transcript @@ -309,7 +307,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { & $script $transcriptFilePath | Should -Exist $transcriptLength = (Get-Content -Path $transcriptFilePath -Raw).Length - + & $scriptMinHeader $transcriptMinHeaderFilePath | Should -Exist $transcriptMinHeaderLength = (Get-Content -Path $transcriptMinHeaderFilePath -Raw).Length diff --git a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 index 4d7358e30..bbe0aa2c2 100644 --- a/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 +++ b/test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1 @@ -13,7 +13,13 @@ Describe "PSReadLine" -tags "CI" { $module = Get-Module PSReadLine $module.Name | Should -BeExactly 'PSReadLine' $module.Version | Should -BeExactly '2.0.0' - $module.Path | Should -Be (Join-Path -Path $PSHOME -ChildPath "Modules/PSReadLine/PSReadLine.psm1") + } + + It "Should be installed to `$PSHOME" { + $module = Get-Module (Join-Path -Path $PSHOME -ChildPath "Modules" -AdditionalChildPath "PSReadLine") -ListAvailable + $module.Name | Should -BeExactly 'PSReadLine' + $module.Version | Should -BeExactly '2.0.0' + $module.Path | Should -Be (Join-Path -Path $PSHOME -ChildPath "Modules/PSReadLine/PSReadLine.psd1") } It "Should use Emacs Bindings on Linux and macOS" -skip:$IsWindows { diff --git a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 index aec3feddf..75f0fa49d 100644 --- a/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/EnableDisable-ExperimentalFeature.Tests.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Import-Module HelpersCommon + Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tags "Feature","RequireAdminOnWindows" { BeforeAll { @@ -52,6 +54,10 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag ) { param ($scope) + if (!(Test-CanWriteToPsHome) -and $scope -eq "AllUsers") { + return + } + $feature = pwsh -noprofile -output xml -command Get-ExperimentalFeature ExpTest.FeatureOne $feature.Enabled | Should -BeFalse -Because "All Experimental Features disabled when no config file" $feature = pwsh -noprofile -output xml -command Enable-ExperimentalFeature ExpTest.FeatureOne -Scope $scope -WarningAction SilentlyContinue @@ -66,6 +72,10 @@ Describe "Enable-ExperimentalFeature and Disable-ExperimentalFeature tests" -tag ) { param ($scope, $configPath) + if (!(Test-CanWriteToPsHome) -and $scope -eq "AllUsers") { + return + } + '{"ExperimentalFeatures":["ExpTest.FeatureOne"]}' > $configPath $feature = pwsh -noprofile -output xml -command Get-ExperimentalFeature ExpTest.FeatureOne $feature.Enabled | Should -BeTrue -Because "Test config should enable ExpTest.FeatureOne" diff --git a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 index 684107983..bf2bbc855 100644 --- a/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 +++ b/test/powershell/engine/ExperimentalFeature/Get-ExperimentalFeature.Tests.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Import-Module HelpersCommon + Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" { BeforeAll { @@ -31,7 +33,7 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } AfterAll { - if ($systemConfigExists) { + if ($systemConfigExists -and (Test-CanWriteToPsHome)) { Move-Item "$systemConfigPath.backup" $systemConfigPath -Force -ErrorAction SilentlyContinue } @@ -43,7 +45,10 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } AfterEach { - Remove-Item $systemConfigPath -Force -ErrorAction SilentlyContinue + if (Test-CanWriteToPsHome) { + Remove-Item $systemConfigPath -Force -ErrorAction SilentlyContinue + } + Remove-Item $userConfigPath -Force -ErrorAction SilentlyContinue } @@ -113,7 +118,7 @@ Describe "Get-ExperimentalFeature Tests" -tags "Feature","RequireAdminOnWindows" } Context "User config takes precedence over system config" { - It "Feature is enabled in user config only" { + It "Feature is enabled in user config only" -Skip:(!(Test-CanWriteToPsHome)) { '{"ExperimentalFeatures":["ExpTest.FeatureOne"]}' > $userConfigPath '{"ExperimentalFeatures":["ExpTest.FeatureTwo"]}' > $systemConfigPath diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index e961c2f96..8a752fbf8 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -3,6 +3,8 @@ # # Validates Get-Help for cmdlets in Microsoft.PowerShell.Core. +Import-Module HelpersCommon + $script:cmdletsToSkip = @( "Get-PSHostProcessInfo", "Out-Default", @@ -122,14 +124,16 @@ Describe "Validate that get-help works for AllUsers Scope" -Tags @('Feature', 'R Context "for module : $moduleName" { BeforeAll { - UpdateHelpFromLocalContentPath $moduleName -Scope 'AllUsers' + if (Test-CanWriteToPsHome) { + UpdateHelpFromLocalContentPath $moduleName -Scope 'AllUsers' + } $cmdlets = Get-Command -Module $moduleName } $testCases = @() $cmdlets | Where-Object { $cmdletsToSkip -notcontains $_ } | ForEach-Object { $testCases += @{ cmdletName = $_.Name }} - It "Validate -Description and -Examples sections in help content. Run 'Get-help -name " -TestCases $testCases { + It "Validate -Description and -Examples sections in help content. Run 'Get-help -name " -TestCases $testCases -Skip:(!(Test-CanWriteToPsHome)) { param($cmdletName) $help = get-help -name $cmdletName $help.Description | Out-String | Should Match $cmdletName @@ -214,10 +218,12 @@ Describe "Validate that get-help works for provider specific help" -Tags @('CI') Describe "Validate about_help.txt under culture specific folder works" -Tags @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') { BeforeAll { $modulePath = "$pshome\Modules\Test" - $null = New-Item -Path $modulePath\en-US -ItemType Directory -Force - New-ModuleManifest -Path $modulePath\test.psd1 -RootModule test.psm1 - Set-Content -Path $modulePath\test.psm1 -Value "function foo{}" - Set-Content -Path $modulePath\en-US\about_testhelp.help.txt -Value "Hello" -NoNewline + if (Test-CanWriteToPsHome) { + $null = New-Item -Path $modulePath\en-US -ItemType Directory -Force + New-ModuleManifest -Path $modulePath\test.psd1 -RootModule test.psm1 + Set-Content -Path $modulePath\test.psm1 -Value "function foo{}" + Set-Content -Path $modulePath\en-US\about_testhelp.help.txt -Value "Hello" -NoNewline + } $aboutHelpPath = Join-Path (GetCurrentUserHelpRoot) (Get-Culture).Name @@ -228,12 +234,14 @@ Describe "Validate about_help.txt under culture specific folder works" -Tags @(' } AfterAll { - Remove-Item $modulePath -Recurse -Force + if (Test-CanWriteToPsHome) { + Remove-Item $modulePath -Recurse -Force + } # Remove all the help content. Get-ChildItem -Path $aboutHelpPath -Include @('about_*.txt', "*help.xml") -Recurse | Remove-Item -Force -ErrorAction SilentlyContinue } - It "Get-Help should return help text and not multiple HelpInfo objects when help is under `$pshome path" { + It "Get-Help should return help text and not multiple HelpInfo objects when help is under `$pshome path" -Skip:(!(Test-CanWriteToPsHome)) { $help = Get-Help about_testhelp $help.count | Should -Be 1 @@ -258,10 +266,12 @@ Describe "About help files can be found in AllUsers scope" -Tags @('Feature', 'R Remove-Item $userHelpRoot -Force -Recurse -ErrorAction Stop } - UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Core' -Scope 'AllUsers' + if (Test-CanWriteToPsHome) { + UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Core' -Scope 'AllUsers' + } } - It "Get-Help for about_Variable should return only one help object" { + It "Get-Help for about_Variable should return only one help object" -Skip:(!(Test-CanWriteToPsHome)) { $help = Get-Help about_Variables $help.count | Should Be 1 } @@ -455,25 +465,27 @@ Describe 'help can be found for AllUsers Scope' -Tags @('Feature', 'RequireAdmin ## Delete help from global scope if it exists. $currentCulture = (Get-Culture).Name - $managementHelpFilePath = Join-Path $PSHOME -ChildPath $currentCulture -AdditionalChildPath 'Microsoft.PowerShell.Commands.Management.dll-Help.xml' - if (Test-Path $managementHelpFilePath) { - Remove-Item $managementHelpFilePath -Force -ErrorAction SilentlyContinue - } + if (Test-CanWriteToPsHome) { + $managementHelpFilePath = Join-Path $PSHOME -ChildPath $currentCulture -AdditionalChildPath 'Microsoft.PowerShell.Commands.Management.dll-Help.xml' + if (Test-Path $managementHelpFilePath) { + Remove-Item $managementHelpFilePath -Force -ErrorAction SilentlyContinue + } - $coreHelpFilePath = Join-Path $PSHOME -ChildPath $currentCulture -AdditionalChildPath 'System.Management.Automation.dll-Help.xml' - if (Test-Path $coreHelpFilePath) { - Remove-Item $coreHelpFilePath -Force -ErrorAction SilentlyContinue - } + $coreHelpFilePath = Join-Path $PSHOME -ChildPath $currentCulture -AdditionalChildPath 'System.Management.Automation.dll-Help.xml' + if (Test-Path $coreHelpFilePath) { + Remove-Item $coreHelpFilePath -Force -ErrorAction SilentlyContinue + } - $archiveHelpFilePath = Join-Path (Get-Module Microsoft.PowerShell.Archive -ListAvailable).ModuleBase -ChildPath $currentCulture -AdditionalChildPath 'Microsoft.PowerShell.Archive-help.xml' - if (Test-Path $archiveHelpFilePath) { - Remove-Item $archiveHelpFilePath -Force -ErrorAction SilentlyContinue - } + $archiveHelpFilePath = Join-Path (Get-Module Microsoft.PowerShell.Archive -ListAvailable).ModuleBase -ChildPath $currentCulture -AdditionalChildPath 'Microsoft.PowerShell.Archive-help.xml' + if (Test-Path $archiveHelpFilePath) { + Remove-Item $archiveHelpFilePath -Force -ErrorAction SilentlyContinue + } - UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Core' -Scope 'AllUsers' - UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Management' -Scope 'AllUsers' - UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Archive' -Scope 'AllUsers' -Force - UpdateHelpFromLocalContentPath -ModuleName 'PackageManagement' -Scope 'AllUsers' -Force + UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Core' -Scope 'AllUsers' + UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Management' -Scope 'AllUsers' + UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Archive' -Scope 'AllUsers' -Force + UpdateHelpFromLocalContentPath -ModuleName 'PackageManagement' -Scope 'AllUsers' -Force + } $TestCases = @( @{TestName = 'module under $PSHOME'; CmdletName = 'Add-Content'} @@ -483,7 +495,7 @@ Describe 'help can be found for AllUsers Scope' -Tags @('Feature', 'RequireAdmin ) } - It 'help in user scope be found for ' -TestCases $TestCases { + It 'help in user scope be found for ' -TestCases $TestCases -Skip:(!(Test-CanWriteToPsHome)) { param($CmdletName) $helpObj = Get-Help -Name $CmdletName -Full diff --git a/test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1 b/test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1 index 19e8a506c..177294d24 100644 --- a/test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/UpdatableHelpSystem.Tests.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Import-Module HelpersCommon + # Test Settings: # This is the list of PowerShell Core modules for which we test update-help $powershellCoreModules = @( @@ -197,7 +199,7 @@ function RunUpdateHelpTests if ($powershellCoreModules -contains $moduleName) { - It "Validate Update-Help for module '$moduleName' with scope as '$userscope'" { + It "Validate Update-Help for module '$moduleName' with scope as '$userscope'" -Skip:(!(Test-CanWriteToPsHome) -and $userscope -eq $false) { if($userscope) { diff --git a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 index a4ffc7277..a40123adb 100644 --- a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 +++ b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 @@ -1,5 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + +Import-Module HelpersCommon + Describe "Test-ModuleManifest tests" -tags "CI" { BeforeEach { @@ -235,15 +238,17 @@ Describe "Tests for circular references in required modules" -tags "CI" { Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { BeforeAll { $TestModulesPath = [System.IO.Path]::Combine($PSScriptRoot, 'assets', 'testmodulerunspace') - $UserModulesPath = "$pshome\Modules" + $PSHomeModulesPath = "$pshome\Modules" # Install the Test Module - Copy-Item $TestModulesPath\* $UserModulesPath -Recurse -Force + if (Test-CanWriteToPsHome) { + Copy-Item $TestModulesPath\* $PSHomeModulesPath -Recurse -Force -ErrorAction Stop + } } - It "Test-ModuleManifest should not load unnessary modules" { + It "Test-ModuleManifest should not load unnessary modules" -Skip:(!(Test-CanWriteToPsHome)) { - $job = start-job -name "job1" -ScriptBlock {test-modulemanifest "$using:UserModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -verbose} | Wait-Job + $job = start-job -name "job1" -ScriptBlock {test-modulemanifest "$using:PSHomeModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -verbose} | Wait-Job $verbose = $job.ChildJobs[0].Verbose.ReadAll() # Before the fix, all modules under $pshome will be imported and will be far more than 15 verbose messages. However, we cannot fix the number in case verbose message may vary. @@ -252,8 +257,10 @@ Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { AfterAll { #clean up the test modules - Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue - Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue + if (Test-CanWriteToPsHome) { + Remove-Item $PSHomeModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue + Remove-Item $PSHomeModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue + } } } diff --git a/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 b/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 index 631c5e1b8..535c9b74a 100644 --- a/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 +++ b/test/powershell/engine/Remoting/RemoteSession.Basic.Tests.ps1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Import-Module HelpersCommon + Describe "New-PSSession basic test" -Tag @("CI") { It "New-PSSession should not crash powershell" { { New-PSSession -ComputerName nonexistcomputer -Authentication Basic } | @@ -52,7 +54,7 @@ Describe "JEA session Transcript script test" -Tag @("Feature", 'RequireAdminOnW $global:PSDefaultParameterValues = $originalDefaultParameterValues } - It "Configuration name should be in the transcript header" { + It "Configuration name should be in the transcript header" -Skip:(!(Test-CanWriteToPsHome)) { [string] $RoleCapDirectory = (New-Item -Path "$TestDrive\RoleCapability" -ItemType Directory -Force).FullName [string] $PSSessionConfigFile = "$RoleCapDirectory\TestConfig.pssc" [string] $transScriptFile = "$RoleCapDirectory\*.txt" @@ -94,7 +96,7 @@ Describe "JEA session Get-Help test" -Tag @("CI", 'RequireAdminOnWindows') { $global:PSDefaultParameterValues = $originalDefaultParameterValues } - It "Get-Help should work in JEA sessions" { + It "Get-Help should work in JEA sessions" -Skip:(!(Test-CanWriteToPsHome)) { [string] $RoleCapDirectory = (New-Item -Path "$TestDrive\RoleCapability" -ItemType Directory -Force).FullName [string] $PSSessionConfigFile = "$RoleCapDirectory\TestConfig.pssc" try diff --git a/test/powershell/engine/Remoting/RunspacePool.Tests.ps1 b/test/powershell/engine/Remoting/RunspacePool.Tests.ps1 index 10909afe8..b92db70ee 100644 --- a/test/powershell/engine/Remoting/RunspacePool.Tests.ps1 +++ b/test/powershell/engine/Remoting/RunspacePool.Tests.ps1 @@ -2,12 +2,13 @@ # Licensed under the MIT License. Import-Module HelpersRemoting +Import-Module HelpersCommon Describe "Remote runspace pool should expose commands in endpoint configuration" -Tags 'Feature','RequireAdminOnWindows' { BeforeAll { - if ($isWindows) + if ($isWindows -and (Test-CanWriteToPsHome)) { $configName = "restrictedV" $configPath = Join-Path $TestDrive ($configName + ".pssc") @@ -22,7 +23,7 @@ Describe "Remote runspace pool should expose commands in endpoint configuration" AfterAll { - if ($IsWindows) + if ($IsWindows -and (Test-CanWriteToPsHome)) { if ($remoteRunspacePool -ne $null) { @@ -33,7 +34,7 @@ Describe "Remote runspace pool should expose commands in endpoint configuration" } } - It "Verifies that the configured endpoint cmdlet is available in all runspace pool instances" -Skip:(! $IsWindows) { + It "Verifies that the configured endpoint cmdlet is available in all runspace pool instances" -Skip:(!$IsWindows -or !(Test-CanWriteToPsHome)) { [powershell] $ps1 = [powershell]::Create() $ps1.RunspacePool = $remoteRunspacePool diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 index 86814c644..c217dc6e7 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psd1 @@ -18,6 +18,7 @@ Description = 'Temporary module contains functions for using in tests' FunctionsToExport = @( 'Add-TestDynamicType' + 'Test-CanWriteToPsHome' 'Disable-Testhook' 'Enable-Testhook' 'Get-RandomFileName' diff --git a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 index c88916e1f..7a3b5e908 100644 --- a/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 +++ b/test/tools/Modules/HelpersCommon/HelpersCommon.psm1 @@ -320,3 +320,27 @@ function New-RandomHexString return ((1..$Length).ForEach{ '{0:x}' -f $random.Next(0xf) }) -join '' } +$script:CanWriteToPsHome = $null +function Test-CanWriteToPsHome +{ + if ($null -ne $script:CanWriteToPsHome) { + return $script:CanWriteToPsHome + } + + $script:CanWriteToPsHome = $true + + try { + $testFileName = Join-Path $PSHome (New-Guid).Guid + $null = New-Item -ItemType File -Path $testFileName -ErrorAction Stop + } + catch [System.UnauthorizedAccessException] { + $script:CanWriteToPsHome = $false + } + finally { + if ($script:CanWriteToPsHome) { + Remove-Item -Path $testFileName -ErrorAction SilentlyContinue + } + } + + $script:CanWriteToPsHome +}