Skip to content
Merged
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
20 changes: 12 additions & 8 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,28 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
Context "-SettingsFile Commandline switch" {

BeforeAll {
$CustomSettingsFile = Join-Path -Path $TestDrive -ChildPath 'Powershell.test.json'
$DefaultExecutionPolicy = 'RemoteSigned'
if ($IsWindows) {
$CustomSettingsFile = Join-Path -Path $TestDrive -ChildPath 'Powershell.test.json'
$DefaultExecutionPolicy = 'RemoteSigned'
}
}
BeforeEach {
# reset the content of the settings file to a known state.
Set-Content -Path $CustomSettingsfile -Value "{`"Microsoft.PowerShell:ExecutionPolicy`":`"$DefaultExecutionPolicy`"}" -ErrorAction Stop
if ($IsWindows) {
# reset the content of the settings file to a known state.
Set-Content -Path $CustomSettingsfile -Value "{`"Microsoft.PowerShell:ExecutionPolicy`":`"$DefaultExecutionPolicy`"}" -ErrorAction Stop
}
}

# NOTE: The -settingsFile command-line option only reads settings for the local machine. As a result, the tests that use Set/Get-ExecutionPolicy
# must use an explicit scope of LocalMachine to ensure the setting is written to the expected file.
# Skip the tests on Unix platforms because *-ExecutionPolicy cmdlets don't work by design.

It "Verifies PowerShell reads from the custom -settingsFile" {
It "Verifies PowerShell reads from the custom -settingsFile" -skip:(!$IsWindows) {
$actualValue = & $powershell -NoProfile -SettingsFile $CustomSettingsFile -Command {(Get-ExecutionPolicy -Scope LocalMachine).ToString()}
$actualValue | Should Be $DefaultExecutionPolicy
}

It "Verifies PowerShell writes to the custom -settingsFile" {
It "Verifies PowerShell writes to the custom -settingsFile" -skip:(!$IsWindows) {
$expectedValue = 'AllSigned'

# Update the execution policy; this should update the settings file.
Expand All @@ -283,15 +288,14 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
$actualValue | Should Be $expectedValue
}

It "Verify PowerShell removes a setting from the custom -settingsFile" {
It "Verify PowerShell removes a setting from the custom -settingsFile" -skip:(!$IsWindows) {
# Remove the LocalMachine execution policy; this should update the settings file.
& $powershell -NoProfile -SettingsFile $CustomSettingsFile -Command {Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope LocalMachine }

# ensure the setting was removed from the settings file.
$content = (Get-Content -Path $CustomSettingsFile | ConvertFrom-Json)
$content.'Microsoft.PowerShell:ExecutionPolicy' | Should Be $null
}

}

Context "Pipe to/from powershell" {
Expand Down