Skip to content

Commit 9e6ac1d

Browse files
authored
Remove Write-Host from PRTest.ps1 (#240696)
1 parent 03b8962 commit 9e6ac1d

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

Tools/PRTest.ps1

+33-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# This script does a checkout of a Pull Request using the GitHub CLI, and then runs it using SandboxTest.ps1.
2-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'This script is not intended to have any outputs piped')]
32

43
Param(
54
[Parameter(Position = 0, HelpMessage = 'The Pull Request to checkout.', Mandatory = $true)]
@@ -16,38 +15,45 @@ Param(
1615
[switch] $Clean
1716
)
1817

19-
$PullRequest = $PullRequest.TrimStart('#')
18+
# Virtual Terminal
19+
filter Initialize-VirtualTerminalSequence {
20+
# https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
21+
if ($script:vtSupported) {
22+
return "$([char]0x001B)[${_}m"
23+
}
24+
}
2025

21-
$ErrorActionPreference = 'Stop'
26+
# Flags
27+
Write-Debug 'Checking for supported features'
28+
$script:vtSupported = (Get-Host).UI.SupportsVirtualTerminal
29+
$script:GitIsPresent = Get-Command 'git' -ErrorAction SilentlyContinue
30+
$script:GhIsPresent = Get-Command 'gh' -ErrorAction SilentlyContinue
31+
$script:SandboxIsPresent = Get-Command 'WindowsSandbox' -ErrorAction SilentlyContinue
2232

23-
$repositoryRoot = 'https://github.com/microsoft/winget-pkgs/'
33+
Write-Debug 'Initializing Virtual Terminal Sequences'
34+
$script:vtDefault = 0 | Initialize-VirtualTerminalSequence
35+
$script:vtForegroundGreen = 32 | Initialize-VirtualTerminalSequence
2436

37+
Write-Debug 'Creating internal state'
38+
$PullRequest = $PullRequest.TrimStart('#')
39+
$ErrorActionPreference = 'Stop'
40+
$repositoryRoot = 'https://github.com/microsoft/winget-pkgs/'
2541
$rootDirectory = ((Resolve-Path (git rev-parse --show-toplevel)).ToString() + '\')
2642

27-
if (-Not (Get-Command 'gh' -ErrorAction 'SilentlyContinue')) {
28-
Write-Host "The GitHub CLI is not installed. Install it via 'winget install GitHub.cli' and come back here!" -ForegroundColor Red
29-
return
30-
}
31-
32-
if (-Not (Get-Command 'git' -ErrorAction 'SilentlyContinue')) {
33-
Write-Host "Git is not installed. Install it via 'winget install Git.Git' and come back here!" -ForegroundColor Red
34-
return
35-
}
43+
Write-Verbose 'Ensuring Dependencies are Present'
44+
if (!$script:GhIsPresent) { Write-Error "The GitHub CLI is not installed. Install it via 'winget install GitHub.cli' and come back here!" -ErrorAction Stop }
45+
if (!$script:GitIsPresent) { Write-Error "Git is not installed. Install it via 'winget install Git.Git' and come back here!" -ErrorAction Stop }
46+
if (!$script:SandboxIsPresent) { Write-Error 'Windows Sandbox is not enabled. Enable it and come back here!' -ErrorAction Stop }
3647

48+
Write-Verbose 'Checking out PR'
3749
gh pr checkout $PullRequest $(if (!$KeepBranch) { '--detach' }) -f -R $repositoryRoot | Out-Null
50+
if ($LASTEXITCODE -ne 0) { Write-Error "There was an error checking out the PR. Make sure you're logged into GitHub via 'gh auth login' and come back here!" -ErrorAction Stop }
3851

39-
if ($LASTEXITCODE -ne 0) {
40-
Write-Host "There was an error checking out the PR. Make sure you're logged into GitHub via 'gh auth login' and come back here!" -ForegroundColor Red
41-
return
42-
}
43-
44-
$manifest = (git diff --name-only HEAD~1..HEAD)
45-
if ($manifest.GetType().Name -eq 'Object[]') {
46-
$path = (Get-Item (Resolve-Path ($rootDirectory + $manifest[0]))).Directory
47-
} else {
48-
$path = (Get-Item (Resolve-Path ($rootDirectory + $manifest))).Directory
49-
}
52+
Write-Verbose 'Parsing changed files'
53+
$manifest = @(gh pr diff $PullRequest --name-only)
54+
$path = (Get-Item (Resolve-Path ($rootDirectory + $manifest[0]))).Directory
5055

56+
Write-Verbose 'Passing execution to SandboxTest.ps1'
5157
$sandboxTestPath = (Resolve-Path ($PSScriptRoot.ToString() + '\SandboxTest.ps1')).ToString()
5258
$params = @{
5359
Manifest = $path
@@ -63,6 +69,7 @@ $params = @{
6369
& $sandboxTestPath @params
6470

6571
if ($Review) {
66-
Write-Host "Opening $PullRequest in browser..." -ForegroundColor Green
67-
Start-Process ($repositoryRoot + 'pull/' + $PullRequest + '/files')
72+
Write-Information "${script:vtForegroundGreen}" -InformationAction 'Continue'
73+
& gh pr diff --web $PullRequest
74+
Write-Information "${script:vtDefault}" -InformationAction 'Continue'
6875
}

0 commit comments

Comments
 (0)