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
17 changes: 5 additions & 12 deletions test/powershell/Host/HostUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,23 @@ Describe "InvokeOnRunspace method as nested command" -tags "Feature" {
Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAdminOnWindows" {

BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$skipTest = (Test-IsWinWow64) -or !$IsWindows

$pendingTest = (Test-IsWinWow64)

if ($pendingTest) {
$global:PSDefaultParameterValues["it:pending"] = $true
if ($skipTest) {
return
}

if ($IsWindows) {
$script:remoteRunspace = New-RemoteRunspace
}
$script:remoteRunspace = New-RemoteRunspace
}

AfterAll {
if ($script:remoteRunspace -and -not $pendingTest)
if ($script:remoteRunspace)
{
$script:remoteRunspace.Dispose();
}

$global:PSDefaultParameterValues = $originalDefaultParameterValues
}

It "Method should successfully invoke command on remote runspace" -Skip:(!$IsWindows) {
It "Method should successfully invoke command on remote runspace" -Skip:$skipTest {

$command = [System.Management.Automation.PSCommand]::new()
$command.AddScript('"Hello!"')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ Describe "Run native command from a mounted FAT-format VHD" -tags @("Feature", "
}

It "Should run 'whoami.exe' from FAT file system without error" -Skip:(!$IsWindows) {
if ((Test-IsWinServer2012R2) -or (Test-IsWindows2016)) {
Set-ItResult -Pending -Because "Marking as pending since whomai.exe is not found on T:\ on 2012R2 and 2016 after copying to VHD"
return
}

$expected = & "$env:WinDir\System32\whoami.exe"
$result = T:\whoami.exe
$result | Should -BeExactly $expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ Describe "Import-Module from CompatiblePSEditions-checked paths" -Tag "CI" {
Describe "Additional tests for Import-Module with WinCompat" -Tag "Feature" {

BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
if ( ! $IsWindows ) {
$PSDefaultParameterValues["it:skip"] = $true
Push-DefaultParameterValueStack @{ "it:skip" = $true }
return
}

$ModuleName = "DesktopModule"
Expand All @@ -468,7 +468,10 @@ Describe "Additional tests for Import-Module with WinCompat" -Tag "Feature" {
}

AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
if ( ! $IsWindows ) {
Pop-DefaultParameterValueStack
return
}
}

Context "Tests that ErrorAction/WarningAction have effect when Import-Module with WinCompat is used" {
Expand Down Expand Up @@ -666,6 +669,11 @@ Describe "Additional tests for Import-Module with WinCompat" -Tag "Feature" {
}

It "NoClobber WinCompat list in powershell.config is a Desktop-edition module" {
if (Test-IsWinWow64) {
Set-ItResult -Skipped -Because "This test is not applicable to WoW64."
return
}

if (-not $desktopModuleToUse) {
throw 'Neither the "PersistentMemory" module nor the "RemoteDesktop" module is available. Please check and use a desktop-edition module that is under the System32 module path.'
}
Expand Down Expand Up @@ -761,14 +769,16 @@ Remove-Module $desktopModuleToUse
Describe "PSModulePath changes interacting with other PowerShell processes" -Tag "Feature" {
BeforeAll {
$pwsh = "$PSHOME/pwsh"
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
if ( ! $IsWindows ) {
$PSDefaultParameterValues["it:skip"] = $true
Push-DefaultParameterValueStack @{ "it:skip" = $true }
return
}
}

AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
if ( ! $IsWindows ) {
Pop-DefaultParameterValueStack
}
}

Context "System32 module path prepended to PSModulePath" {
Expand Down Expand Up @@ -1403,8 +1413,7 @@ Describe "Import-Module nested module behaviour with Edition checking" -Tag "Fea
Describe "WinCompat importing should check availablity of built-in modules" -Tag "CI" {
BeforeAll {
if (-not $IsWindows ) {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
Push-DefaultParameterValueStack @{ "it:skip" = $true }
return
}

Expand All @@ -1426,7 +1435,7 @@ Describe "WinCompat importing should check availablity of built-in modules" -Tag

AfterAll {
if (-not $IsWindows) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
Pop-DefaultParameterValueStack
return
}

Expand Down Expand Up @@ -1481,6 +1490,11 @@ Describe "WinCompat importing should check availablity of built-in modules" -Tag
}

It "Attempt to load a 'Desktop' edition module should fail because 'Export-PSSession' cannot be found" {
if (Test-IsWinWow64) {
Set-ItResult -Skipped -Because "This test is not applicable to WoW64."
return
}

if (-not $desktopModuleToUse) {
throw 'Neither the "PersistentMemory" module nor the "RemoteDesktop" module is available. Please check and use a desktop-edition module that is under the System32 module path.'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ Import-Module HelpersCommon
try
{
# Skip all tests on non-windows and non-PowerShellCore and non-elevated platforms.
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$originalWarningPreference = $WarningPreference
$WarningPreference = "SilentlyContinue"
# Skip all tests if can't write to $PSHOME as Register-PSSessionConfiguration writes to $PSHOME
# or if the processor architecture is Arm64
$IsNotSkipped = ($IsWindows -and $IsCoreCLR -and (Test-IsElevated) -and (Test-CanWriteToPsHome) -and [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture -ne [System.Runtime.InteropServices.Architecture]::Arm64)
$PSDefaultParameterValues["it:skip"] = !$IsNotSkipped

$IsNotSkipped = (
$IsWindows -and
$IsCoreCLR -and
(Test-IsElevated) -and
(Test-CanWriteToPsHome) -and
-not (Test-IsWindowsArm64) -and
-not (Test-IsWinWow64)
)

Push-DefaultParameterValueStack @{ "it:skip" = ! $IsNotSkipped }
#
# TODO: Enable-PSRemoting should be performed at a higher set up for all tests.
# Tests whether PowerShell remoting is enabled for this instance of PowerShell.
Expand Down Expand Up @@ -69,39 +75,28 @@ try
# Register new session configuration
function RegisterNewConfiguration {
param (

[string]
$Name,

[string]
$ConfigFilePath,

[switch]
$Enabled
[string] $Name,
[string] $ConfigFilePath,
[switch] $Enabled
)

$TestConfig = Get-PSSessionConfiguration -Name $Name -ErrorAction SilentlyContinue
if($TestConfig)
{
if($TestConfig) {
$null = Unregister-PSSessionConfiguration -Name $Name
}

if($Enabled)
{
if($Enabled) {
$null = Register-PSSessionConfiguration -Name $Name -Path $ConfigFilePath
}
else
{
else {
$null = Register-PSSessionConfiguration -Name $Name -Path $ConfigFilePath -AccessMode Disabled
}
}

# Unregister session configuration
function UnregisterPSSessionConfiguration{
param (

[string]
$Name
[string] $Name
)

Unregister-PSSessionConfiguration -Name $Name -Force -NoServiceRestart -ErrorAction SilentlyContinue
Expand All @@ -111,8 +106,7 @@ try
function CreateTestConfigFile {

$TestConfigFileLoc = Join-Path $TestDrive "Remoting"
if(-not (Test-path $TestConfigFileLoc))
{
if(-not (Test-path $TestConfigFileLoc)) {
$null = New-Item -Path $TestConfigFileLoc -ItemType Directory -Force -ErrorAction Stop
}

Expand Down Expand Up @@ -165,40 +159,26 @@ try

function VerifyEnableAndDisablePSSessionConfig {
param (
[string]
$SessionConfigName,

[string]
$ConfigFilePath,

[Bool]
$InitialSessionStateEnabled,

[Bool]
$FinalSessionStateEnabled,

[string]
$TestDescription,

[bool]
$EnablePSSessionConfig
)
[string] $SessionConfigName,
[string] $ConfigFilePath,
[Bool] $InitialSessionStateEnabled,
[Bool] $FinalSessionStateEnabled,
[string] $TestDescription,
[bool] $EnablePSSessionConfig)

It "$TestDescription" {

RegisterNewConfiguration -Name $SessionConfigName -ConfigFilePath $ConfigFilePath -Enabled:$InitialSessionStateEnabled

$TestConfigStateBeforeChange = (Get-PSSessionConfiguration -Name $SessionConfigName).Enabled

if($EnablePSSessionConfig)
{
if($EnablePSSessionConfig) {
$isSkipNetworkCheck = $true
# TODO: Get-NetConnectionProfile is not available during typical PS Core deployments. Once it is, this check should be used.
#Get-NetConnectionProfile | Where-Object { $_.NetworkCategory -eq "Public" } | ForEach-Object { $isSkipNetworkCheck = $true }
Enable-PSSessionConfiguration -Name $SessionConfigName -NoServiceRestart -SkipNetworkProfileCheck:$isSkipNetworkCheck
}
else
{
else {
Disable-PSSessionConfiguration -Name $SessionConfigName -NoServiceRestart
}

Expand Down Expand Up @@ -231,21 +211,21 @@ try
}
)

foreach ($testcase in $testData)
{
foreach ($testcase in $testData) {
VerifyEnableAndDisablePSSessionConfig @testcase
}
}

Context "Validate Unregister-PSSessionConfiguration cmdlet" {

BeforeEach {
Register-PSSessionConfiguration -Name "TestUnregisterPSSessionConfig"
if ($IsNotSkipped) {
Register-PSSessionConfiguration -Name "TestUnregisterPSSessionConfig"
}
}

AfterAll {
if ($IsNotSkipped)
{
if ($IsNotSkipped) {
Unregister-PSSessionConfiguration -name "TestUnregisterPSSessionConfig" -ErrorAction SilentlyContinue | Out-Null
}
}
Expand All @@ -258,32 +238,26 @@ try

$Result = [PSObject] @{Output = $true ; Error = $null}
$error.Clear()
try
{
try {
$null = Unregister-PSSessionConfiguration -name $SessionConfigName -ErrorAction stop
}
catch
{
catch {
$Result.Error = $_.Exception
}

if(-not $Result.Error)
{
if(-not $Result.Error) {
$ValidEndpoints = [PSObject]@(Get-PSSessionConfiguration)

foreach ($endpoint in $ValidEndpoints)
{
foreach ($endpoint in $ValidEndpoints) {
# Setting it to false means the unregister was unsuccessful
# and there is still an endpoint with name matching the one we wanted to remove.
if($endpoint.name -like $SessionConfigName)
{
if($endpoint.name -like $SessionConfigName) {
$Result.Output = $false
break
}
}
}
else
{
else {
$Result.Output = $false
}

Expand Down Expand Up @@ -833,7 +807,7 @@ namespace PowershellTestConfigNamespace
BeforeAll {
if (Test-IsWindowsArm64) {
Write-Verbose "remoting is not setup on ARM64, skipping tests" -Verbose
$PSDefaultParameterValues["it:skip"] = $true
Push-DefaultParameterValueStack @{ "it:skip" = $true }
return
}

Expand All @@ -842,6 +816,12 @@ namespace PowershellTestConfigNamespace
}
}

AfterAll {
if (Test-IsWindowsArm64) {
Pop-DefaultParameterValueStack
}
}

It "Enable-PSSession Cmdlet creates a PSSession configuration with a name tied to PowerShell version." {
$endpointName = "PowerShell." + $PSVersionTable.GitCommitId.ToString()
$matchedEndpoint = Get-PSSessionConfiguration $endpointName -ErrorAction SilentlyContinue
Expand All @@ -863,7 +843,7 @@ namespace PowershellTestConfigNamespace
}
finally
{
$global:PSDefaultParameterValues = $originalDefaultParameterValues
Pop-DefaultParameterValueStack
$WarningPreference = $originalWarningPreference
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ Describe "Get-ChildItem" -Tags "CI" {
@{Parameters = @{Path = (Join-Path $searchRoot 'F*.txt'); Recurse = $true; File = $true }; ExpectedCount = 1; Title = "file with wildcard filename"}
)

$SkipAppExeCLinks = $true
if ($IsWindows -and (Get-ChildItem -Path ~\AppData\Local\Microsoft\WindowsApps\*.exe -ErrorAction Ignore) -ne $null)
{
$SkipAppExeCLinks = $false
}
}

It "Should list the contents of the current folder" {
Expand Down Expand Up @@ -187,7 +182,8 @@ Describe "Get-ChildItem" -Tags "CI" {
(Get-ChildItem -File -LiteralPath TestDrive:/ -Filter noext*.*).Name | Should -BeExactly 'noextension'
}

It "Understand APPEXECLINKs" -Skip:($SkipAppExeCLinks) {
# Because "dotnet API is not available, see PR 16044
It "Understand APPEXECLINKs" -Skip {
$app = Get-ChildItem -Path ~\appdata\local\microsoft\windowsapps\*.exe | Select-Object -First 1
$app.Target | Should -Not -Be $app.FullName
$app.LinkType | Should -BeExactly 'AppExeCLink'
Expand Down Expand Up @@ -232,6 +228,10 @@ Describe "Get-ChildItem" -Tags "CI" {
It 'Works with Windows volume paths' -Skip:(!$IsWindows) {
$volume = (Get-Volume -DriveLetter $env:SystemDrive[0]).Path
$items = Get-ChildItem -LiteralPath "${volume}Windows"
Write-Verbose -Verbose "Trying files in '${volume}Windows'"
if (-not $items) {
Write-Verbose -Verbose "`$items is null!!"
}
$items[0].Parent | Should -BeExactly "${volume}Windows"
$items | Should -HaveCount (Get-ChildItem $env:SystemRoot).Count
}
Expand Down
Loading