Skip to content

Commit 5913c88

Browse files
authored
Mark tests in macOS CI which use applescript as pending/inconclusive (#9352)
Mark tests in macOS CI which use applescript as pending/inconclusive
1 parent 1006db6 commit 5913c88

3 files changed

Lines changed: 107 additions & 12 deletions

File tree

build.psm1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,8 @@ function Start-PSPester {
909909
[string]$Title = 'PowerShell Core Tests',
910910
[Parameter(ParameterSetName='Wait', Mandatory=$true,
911911
HelpMessage='Wait for the debugger to attach to PowerShell before Pester starts. Debug builds only!')]
912-
[switch]$Wait
912+
[switch]$Wait,
913+
[switch]$SkipTestToolBuild
913914
)
914915

915916
if (-not (Get-Module -ListAvailable -Name $Pester -ErrorAction SilentlyContinue | Where-Object { $_.Version -ge "4.2" } ))
@@ -968,7 +969,10 @@ function Start-PSPester {
968969
}
969970

970971
Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
971-
Publish-PSTestTools | ForEach-Object {Write-Host $_}
972+
if(!$SkipTestToolBuild.IsPresent)
973+
{
974+
Publish-PSTestTools | ForEach-Object {Write-Host $_}
975+
}
972976

973977
# All concatenated commands/arguments are suffixed with the delimiter (space)
974978

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Categories=Application;
186186

187187
It "Should open text file without error" -Skip:(!$supportedEnvironment) {
188188
if ($IsMacOS) {
189+
Set-TestInconclusive -Message "AppleScript is not currently reliable on Az Pipelines"
189190
$expectedTitle = Split-Path $TestFile -Leaf
190191
open -F -a TextEdit
191192
$beforeCount = [int]('tell application "TextEdit" to count of windows' | osascript)

test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,67 @@
22
# Licensed under the MIT License.
33
using namespace System.Diagnostics
44

5+
function Invoke-AppleScript
6+
{
7+
param(
8+
[string]$Script,
9+
[switch]$PassThru
10+
)
11+
12+
Write-Verbose "running applescript: $Script"
13+
14+
$result = $Script | osascript
15+
if($PassThru.IsPresent)
16+
{
17+
return $result
18+
}
19+
}
20+
21+
function Get-WindowCountMacOS {
22+
param(
23+
[string]$Name
24+
)
25+
26+
$processCount = @(Get-Process $Name -ErrorAction Ignore).Count
27+
28+
if($processCount -eq 0)
29+
{
30+
return 0
31+
}
32+
33+
$title = Get-WindowsTitleMacOS -name $Name
34+
35+
if(!$title)
36+
{
37+
return 0
38+
}
39+
40+
$windowCount = [int](Invoke-AppleScript -Script ('tell application "{0}" to count of windows' -f $Name) -PassThru)
41+
return $windowCount
42+
}
43+
44+
function Get-WindowsTitleMacOS {
45+
param(
46+
[string]$Name
47+
)
48+
49+
return Invoke-AppleScript -Script ('tell application "{0}" to get name of front window' -f $Name) -PassThru
50+
}
51+
52+
function Stop-ProcessMacOS {
53+
param(
54+
[string]$Name,
55+
[switch]$QuitFirst
56+
)
57+
58+
if($QuitFirst.IsPresent)
59+
{
60+
Invoke-AppleScript -Script ('tell application "{0}" to quit' -f $Name)
61+
}
62+
63+
Get-Process -Name $Name -ErrorAction Ignore | Stop-Process -Force
64+
}
65+
566
Describe "Invoke-Item basic tests" -Tags "Feature" {
667
BeforeAll {
768
$powershell = Join-Path $PSHOME -ChildPath pwsh
@@ -15,40 +76,52 @@ Describe "Invoke-Item basic tests" -Tags "Feature" {
1576
New-Item -Path $testFile2 -ItemType File -Force > $null
1677

1778
$textFileTestCases = @(
18-
@{ TestFile = $testFile1 },
19-
@{ TestFile = $testFile2 })
79+
@{ TestFile = $testFile1; Name='file in root' },
80+
@{ TestFile = $testFile2; Name='file in subDirectory' })
2081
}
2182

2283
Context "Invoke a text file on Unix" {
2384
BeforeEach {
2485
$redirectErr = Join-Path -Path $TestDrive -ChildPath "error.txt"
86+
87+
if($IsMacOS)
88+
{
89+
Stop-ProcessMacOs -Name TextEdit -QuitFirst
90+
}
2591
}
2692

2793
AfterEach {
2894
Remove-Item -Path $redirectErr -Force -ErrorAction SilentlyContinue
95+
96+
}
97+
98+
AfterAll{
99+
if($IsMacOS)
100+
{
101+
Stop-ProcessMacOs -Name TextEdit
102+
}
29103
}
30104

31105
## Run this test only on macOS because redirecting stderr of 'xdg-open' results in weird behavior in our Linux CI,
32106
## causing this test to fail or the build to not respond.
33-
It "Should invoke text file '<TestFile>' without error on Mac" -Skip:(!$IsMacOS) -TestCases $textFileTestCases {
107+
It "Should invoke text file '<Name>' without error on Mac" -Pending -TestCases $textFileTestCases {
34108
param($TestFile)
35109

36110
$expectedTitle = Split-Path $TestFile -Leaf
37111
open -F -a TextEdit
38-
$beforeCount = [int]('tell application "TextEdit" to count of windows' | osascript)
112+
$beforeCount = Get-WindowCountMacOS -Name TextEdit
39113
Invoke-Item -Path $TestFile
40114
$startTime = Get-Date
41115
$title = [String]::Empty
42116
while (((Get-Date) - $startTime).TotalSeconds -lt 30 -and ($title -ne $expectedTitle))
43117
{
44118
Start-Sleep -Milliseconds 100
45-
$title = 'tell application "TextEdit" to get name of front window' | osascript
119+
$title = Get-WindowsTitleMacOS -name TextEdit
46120
}
47-
$afterCount = [int]('tell application "TextEdit" to count of windows' | osascript)
48-
$afterCount | Should -Be ($beforeCount + 1)
121+
$afterCount = Get-WindowCountMacOS -Name TextEdit
122+
$afterCount | Should -Be ($beforeCount + 1) -Because "There should be one more 'textEdit' windows open than when the tests started and there was $beforeCount"
49123
$title | Should -Be $expectedTitle
50-
"tell application ""TextEdit"" to close window ""$expectedTitle""" | osascript
51-
'tell application "TextEdit" to quit' | osascript
124+
Invoke-AppleScript -Script ('tell application "{0}" to close window "{1}"' -f 'TextEdit', $expectedTitle)
52125
}
53126
}
54127

@@ -122,6 +195,22 @@ Categories=Application;
122195
}
123196
}
124197

198+
BeforeEach {
199+
200+
if($IsMacOS)
201+
{
202+
Get-Process -Name Finder | Stop-Process -Force
203+
}
204+
}
205+
206+
AfterAll{
207+
if($IsMacOS)
208+
{
209+
Stop-ProcessMacOs -Name Finder
210+
}
211+
}
212+
213+
125214
It "Should invoke a folder without error" -Skip:(!$supportedEnvironment) {
126215
if ($IsWindows)
127216
{
@@ -150,8 +239,9 @@ Categories=Application;
150239
}
151240
else
152241
{
242+
Set-TestInconclusive -Message "AppleScript is not currently reliable on Az Pipelines"
153243
# validate on MacOS by using AppleScript
154-
$beforeCount = [int]('tell application "Finder" to count of windows' | osascript)
244+
$beforeCount = Get-WindowCountMacOS -Name Finder
155245
Invoke-Item -Path $PSHOME
156246
$startTime = Get-Date
157247
$expectedTitle = Split-Path $PSHOME -Leaf

0 commit comments

Comments
 (0)