8000 Update tests to account for when $PSHOME is readonly by SteveL-MSFT · Pull Request #9279 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Import-Module HelpersCommon
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't autoloading work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if you have the test modules in your PSModulePath. If you run the tests directly instead of via Invoke-PSPester, I figured not finding the module was a better error message than not finding the cmdlet

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder that we add this again because previously we removed all explicit import-module from all test files. Calling Invoke-PSPester once in a session could resolves the problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok either way on this. Some of test scripts already explicitly import helper modules.


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

#
Expand Down
B93C
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down Expand Up @@ -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" {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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> Module should throw '<error>'" -TestCases $testData {
param([string]$module, [string]$error)
Expand Down Expand Up @@ -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> Module should throw '<error>'" -TestCases $testData {
param([string]$module, [string]$error)
$testScript = {Import-Module -Name $module -Force}
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@{ ModuleVersion = '1.0'; NestedModules = 'D:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Security\Modules\ImportUnTrustedManifestWithBinFnExport_System32.dll'; CmdletsToExport = 'Invoke-Hello' }
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/powershell/Modules/PSReadLine/PSReadLine.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
Loading
0