Skip to content

Commit 1a24540

Browse files
authored
Enable more tests to be run in a container. (PowerShell#17294)
1 parent 514d6f6 commit 1a24540

File tree

7 files changed

+50
-64
lines changed

7 files changed

+50
-64
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/SetDateCommand.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ protected override void ProcessRecord()
7171
if (ShouldProcess(dateToUse.ToString()))
7272
{
7373
#if UNIX
74-
if (!Platform.NonWindowsSetDate(dateToUse))
74+
// We are not validating the native call here.
75+
// We just want to be sure that we're using the value the user provided us.
76+
if (Dbg.Internal.InternalTestHooks.SetDate)
77+
{
78+
WriteObject(dateToUse);
79+
}
80+
else if (!Platform.NonWindowsSetDate(dateToUse))
7581
{
7682
throw new Win32Exception(Marshal.GetLastWin32Error());
7783
}
@@ -86,16 +92,23 @@ protected override void ProcessRecord()
8692
systemTime.Second = (ushort)dateToUse.Second;
8793
systemTime.Milliseconds = (ushort)dateToUse.Millisecond;
8894
#pragma warning disable 56523
89-
if (!NativeMethods.SetLocalTime(ref systemTime))
95+
if (Dbg.Internal.InternalTestHooks.SetDate)
9096
{
91-
throw new Win32Exception(Marshal.GetLastWin32Error());
97+
WriteObject(systemTime);
9298
}
93-
94-
// MSDN says to call this twice to account for changes
95-
// between DST
96-
if (!NativeMethods.SetLocalTime(ref systemTime))
99+
else
97100
{
98-
throw new Win32Exception(Marshal.GetLastWin32Error());
101+
if (!NativeMethods.SetLocalTime(ref systemTime))
102+
{
103+
throw new Win32Exception(Marshal.GetLastWin32Error());
104+
}
105+
106+
// MSDN says to call this twice to account for changes
107+
// between DST
108+
if (!NativeMethods.SetLocalTime(ref systemTime))
109+
{
110+
throw new Win32Exception(Marshal.GetLastWin32Error());
111+
}
99112
}
100113
#pragma warning restore 56523
101114
#endif
@@ -106,7 +119,11 @@ protected override void ProcessRecord()
106119
PSNoteProperty note = new("DisplayHint", DisplayHint);
107120
outputObj.Properties.Add(note);
108121

109-
WriteObject(outputObj);
122+
// If we've turned on the SetDate test hook, don't emit the output object here because we emitted it earlier.
123+
if (!Dbg.Internal.InternalTestHooks.SetDate)
124+
{
125+
WriteObject(outputObj);
126+
}
110127
}
111128

112129
#endregion

src/System.Management.Automation/engine/Utils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,8 @@ public static class InternalTestHooks
15921592
internal static bool SetConsoleWidthToZero;
15931593
internal static bool SetConsoleHeightToZero;
15941594

1595+
internal static bool SetDate;
1596+
15951597
// A location to test PSEdition compatibility functionality for Windows PowerShell modules with
15961598
// since we can't manipulate the System32 directory in a test
15971599
internal static string TestWindowsPowerShellPSHomeLocation;

test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Describe 'native commands with pipeline' -tags 'Feature' {
4747
It 'native command should be killed when pipeline is disposed' -Skip:($IsWindows) {
4848
$yes = (Get-Process 'yes' -ErrorAction Ignore).Count
4949
yes | Select-Object -First 2
50+
# wait a little to be sure that the process is ended
51+
Start-Sleep -Milliseconds 500
5052
(Get-Process 'yes' -ErrorAction Ignore).Count | Should -Be $yes
5153
}
5254
}

test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,7 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" {
254254
}
255255

256256
Context "MTUSizeDetect" {
257-
# We skip the MtuSize detection tests when in containers, as the environments throw raw exceptions
258-
# instead of returning a PacketTooBig response cleanly.
259-
# Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961
260-
It "MTUSizeDetect works" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) {
257+
It "MTUSizeDetect works" {
261258
$result = Test-Connection $testAddress -MtuSize
262259

263260
$result | Should -BeOfType Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus
@@ -266,8 +263,7 @@ Describe "Test-Connection" -tags "CI", "RequireSudoOnUnix" {
266263
$result.MtuSize | Should -BeGreaterThan 0
267264
}
268265

269-
# Test disabled due to .NET runtime issue: https://github.com/dotnet/runtime/issues/55961
270-
It "Quiet works" -Pending:(($env:__INCONTAINER -eq 1) -or $IsMacOS) {
266+
It "Quiet works" {
271267
$result = Test-Connection $gatewayAddress -MtuSize -Quiet
272268

273269
$result | Should -BeOfType Int32

test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,28 @@ Import-Module HelpersCommon
55

66
Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {
77
BeforeAll {
8-
$skipTest = (Test-IsVstsLinux) -or ($env:__INCONTAINER -eq 1)
8+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("SetDate", $true)
99
}
10-
# Fails in VSTS Linux with Operation not permitted
11-
It "Set-Date should be able to set the date in an elevated context" -Skip:$skipTest {
10+
AfterAll {
11+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook("SetDate", $false)
12+
}
13+
14+
It "Set-Date should be able to set the date in an elevated context" {
1215
{ Get-Date | Set-Date } | Should -Not -Throw
1316
}
1417

15-
# Fails in VSTS Linux with Operation not permitted
16-
It "Set-Date should be able to set the date with -Date parameter" -Skip:$skipTest {
18+
# Check the individual properties as the types may be different
19+
It "Set-Date should be able to set the date with -Date parameter" {
1720
$target = Get-Date
1821
$expected = $target
19-
Set-Date -Date $target | Should -Be $expected
22+
$observed = Set-Date -Date $target
23+
# do not test dayofweek because the number of the day is different between native and managed
24+
$observed.Day | Should -Be $expected.Day
25+
$observed.Hour | Should -Be $expected.Hour
26+
$observed.Minutes | Should -Be $expected.Minutes
27+
$observed.Month | Should -Be $expected.Month
28+
$observed.Second | Should -Be $expected.Second
29+
$observed.Year | Should -Be $expected.Year
2030
}
2131
}
2232

test/powershell/engine/Help/HelpSystem.OnlineHelp.Tests.ps1

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Describe 'Online help tests for PowerShell Cmdlets' -Tags "Feature" {
4242

4343
# TopicTitle - is the cmdlet name in the csv file
4444
# HelpURI - is the expected help URI in the csv file
45+
# If this is correct, then launching the cmdlet should open the expected help URI.
4546

4647
It "Validate 'get-help $($cmdlet.TopicTitle) -Online'" -Skip:$skipTest {
4748
$actualURI = Get-Help $cmdlet.TopicTitle -Online
@@ -52,48 +53,6 @@ Describe 'Online help tests for PowerShell Cmdlets' -Tags "Feature" {
5253
}
5354
}
5455

55-
Describe 'Get-Help -Online opens the default web browser and navigates to the cmdlet help content' -Tags "Feature" {
56-
57-
$skipTest = [System.Management.Automation.Platform]::IsIoT -or
58-
[System.Management.Automation.Platform]::IsNanoServer -or
59-
$env:__INCONTAINER -eq 1
60-
61-
# this code is a workaround for issue: https://github.com/PowerShell/PowerShell/issues/3079
62-
if((-not ($skipTest)) -and $IsWindows)
63-
{
64-
$skipTest = $true
65-
$regKey = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice"
66-
67-
try
68-
{
69-
$progId = (Get-ItemProperty $regKey).ProgId
70-
if($progId)
71-
{
72-
if (-not (Test-Path 'HKCR:\'))
73-
{
74-
New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR | Should -Not -BeNullOrEmpty
75-
}
76-
$browserExe = ((Get-ItemProperty "HKCR:\$progId\shell\open\command")."(default)" -replace '"', '') -split " "
77-
if ($browserExe.count -ge 1)
78-
{
79-
if($browserExe[0] -match '.exe')
80-
{
81-
$skipTest = $false
82-
}
83-
}
84-
}
85-
}
86-
catch
87-
{
88-
# We are not able to access Registry, skipping test.
89-
}
90-
}
91-
92-
It "Get-Help get-process -online" -Skip:$skipTest {
93-
{ Get-Help get-process -Online } | Should -Not -Throw
94-
}
95-
}
96-
9756
Describe 'Get-Help -Online is not supported on Nano Server and IoT' -Tags "CI" {
9857

9958
$skipTest = -not ([System.Management.Automation.Platform]::IsIoT -or [System.Management.Automation.Platform]::IsNanoServer)

test/powershell/engine/Help/HelpSystem.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ Describe "Help failure cases" -Tags Feature {
578578

579579
# under some conditions this does not throw, so include what we actually got
580580
$helpTopic = [guid]::NewGuid().ToString("N")
581-
{ & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "$(& $command $helpTopic -ea Ignore)"
581+
{ & $command $helpTopic -ErrorAction Stop } | Should -Throw -ErrorId "HelpNotFound,Microsoft.PowerShell.Commands.GetHelpCommand" -Because "A help topic was unexpectantly found for $helpTopic"
582582
}
583583
}
584584

0 commit comments

Comments
 (0)