Skip to content

Commit 839f650

Browse files
Fcursoragent
andcommitted
fix(test): defer yq detection out of conversion BeforeAll
Linux CI can expose multiple yq Application matches that break Initialize-ConversionIntegration. Probe yq lazily with caching and harden Get-CachedExternalCommand resolution instead. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 9fa8e6c commit 839f650

3 files changed

Lines changed: 116 additions & 66 deletions

File tree

profile.d/bootstrap/CommandCache.ps1

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,10 @@ function global:Get-CachedExternalCommand {
539539

540540
# Prefer explicit test mocks over host binaries with the same name.
541541
if ($global:TestRegisteredMockCommands -and $global:TestRegisteredMockCommands.Contains($candidate)) {
542-
$mockFunction = Get-Command -Name $candidate -CommandType Function -ErrorAction SilentlyContinue
543-
if ($mockFunction -and $mockFunction.Name.Equals($candidate, [StringComparison]::OrdinalIgnoreCase)) {
544-
return $mockFunction
542+
foreach ($mockFunction in @(Get-Command -Name $candidate -CommandType Function -ErrorAction SilentlyContinue)) {
543+
if ($mockFunction.Name.Equals($candidate, [StringComparison]::OrdinalIgnoreCase)) {
544+
return $mockFunction
545+
}
545546
}
546547
}
547548

@@ -552,23 +553,40 @@ function global:Get-CachedExternalCommand {
552553
continue
553554
}
554555

555-
if ($isYqLookup -and -not (Test-IsMikefarahYqExecutable -Executable $executablePath)) {
556-
continue
556+
if ($isYqLookup) {
557+
try {
558+
if (-not (Test-IsMikefarahYqExecutable -Executable $executablePath)) {
559+
continue
560+
}
561+
}
562+
catch {
563+
continue
564+
}
557565
}
558566

559567
return $application
560568
}
561569

562570
# Profile aliases with the same name as a binary would otherwise recurse into wrapper functions.
563-
$functionCmd = Get-Command -Name $candidate -CommandType Function -ErrorAction SilentlyContinue
564-
if ($functionCmd -and $functionCmd.Name.Equals($candidate, [StringComparison]::OrdinalIgnoreCase)) {
565-
return $functionCmd
571+
foreach ($functionCmd in @(Get-Command -Name $candidate -CommandType Function -ErrorAction SilentlyContinue)) {
572+
if ($functionCmd.Name.Equals($candidate, [StringComparison]::OrdinalIgnoreCase)) {
573+
return $functionCmd
574+
}
566575
}
567576

568577
foreach ($externalScript in @(Get-Command -Name $candidate -CommandType ExternalScript -ErrorAction SilentlyContinue)) {
569578
if ($isYqLookup) {
570579
$exe = Resolve-CommandExecutablePath -CommandInfo $externalScript
571-
if ([string]::IsNullOrWhiteSpace($exe) -or -not (Test-IsMikefarahYqExecutable -Executable $exe)) {
580+
if ([string]::IsNullOrWhiteSpace($exe)) {
581+
continue
582+
}
583+
584+
try {
585+
if (-not (Test-IsMikefarahYqExecutable -Executable $exe)) {
586+
continue
587+
}
588+
}
589+
catch {
572590
continue
573591
}
574592
}
@@ -599,6 +617,9 @@ function global:Clear-TestCachedCommandCache {
599617
}
600618

601619
$global:TestCachedCommandCache.Clear()
620+
if (Get-Command Clear-TestMikefarahYqAvailabilityCache -ErrorAction SilentlyContinue) {
621+
Clear-TestMikefarahYqAvailabilityCache
622+
}
602623
return $true
603624
}
604625

tests/TestSupport/TestModuleLoading.ps1

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,14 +1692,8 @@ function Initialize-ConversionIntegration {
16921692
}
16931693
}
16941694

1695-
if (Get-Command Test-MikefarahYqAvailable -ErrorAction SilentlyContinue) {
1696-
try {
1697-
$script:MikefarahYqAvailable = Test-MikefarahYqAvailable
1698-
}
1699-
catch {
1700-
$script:MikefarahYqAvailable = $false
1701-
}
1702-
}
1695+
# yq availability is resolved lazily via Test-MikefarahYqAvailable when tests call
1696+
# Skip-IfMikefarahYqUnavailable; probing here broke unrelated conversion BeforeAll hooks on Linux CI.
17031697
}
17041698

17051699

tests/TestSupport/ToolDetection.ps1

Lines changed: 84 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -863,67 +863,102 @@ function Test-MikefarahYqAvailable {
863863
#>
864864
[CmdletBinding()]
865865
[OutputType([bool])]
866-
param()
867-
868-
if (Get-Command Get-CachedExternalCommand -ErrorAction SilentlyContinue) {
869-
$yqCmd = Get-CachedExternalCommand -Name 'yq'
870-
if (-not $yqCmd) {
871-
return $false
872-
}
873-
874-
if (Get-Command Test-IsMikefarahYqExecutable -ErrorAction SilentlyContinue) {
875-
$executable = if (Get-Command Resolve-CommandExecutablePath -ErrorAction SilentlyContinue) {
876-
Resolve-CommandExecutablePath -CommandInfo $yqCmd
877-
}
878-
elseif (-not [string]::IsNullOrWhiteSpace($yqCmd.Name)) {
879-
[string]$yqCmd.Name
880-
}
881-
else {
882-
$null
883-
}
884-
return Test-IsMikefarahYqExecutable -Executable $executable
885-
}
866+
param(
867+
[switch]$Refresh
868+
)
886869

887-
return $true
870+
if (-not $Refresh -and (Get-Variable -Name MikefarahYqAvailabilityCache -Scope Script -ErrorAction SilentlyContinue)) {
871+
return [bool]$script:MikefarahYqAvailabilityCache
888872
}
889873

890-
foreach ($candidate in @('go-yq', 'yq')) {
891-
foreach ($cmd in @(Get-Command $candidate -CommandType Application -ErrorAction SilentlyContinue)) {
892-
$exe = if (Get-Command Resolve-CommandExecutablePath -ErrorAction SilentlyContinue) {
893-
Resolve-CommandExecutablePath -CommandInfo $cmd
894-
}
895-
elseif (-not [string]::IsNullOrWhiteSpace($cmd.Name)) {
896-
[string]$cmd.Name
897-
}
898-
else {
899-
$null
900-
}
901-
if ([string]::IsNullOrWhiteSpace($exe)) {
902-
continue
903-
}
904-
905-
if (Get-Command Test-IsMikefarahYqExecutable -ErrorAction SilentlyContinue) {
906-
if (Test-IsMikefarahYqExecutable -Executable $exe) {
907-
return $true
874+
try {
875+
$available = $false
876+
877+
if (Get-Command Get-CachedExternalCommand -ErrorAction SilentlyContinue) {
878+
$yqCmd = Get-CachedExternalCommand -Name 'yq'
879+
if ($yqCmd) {
880+
if (Get-Command Test-IsMikefarahYqExecutable -ErrorAction SilentlyContinue) {
881+
$executable = if (Get-Command Resolve-CommandExecutablePath -ErrorAction SilentlyContinue) {
882+
Resolve-CommandExecutablePath -CommandInfo $yqCmd
883+
}
884+
elseif (-not [string]::IsNullOrWhiteSpace($yqCmd.Name)) {
885+
[string]$yqCmd.Name
886+
}
887+
else {
888+
$null
889+
}
890+
$available = Test-IsMikefarahYqExecutable -Executable $executable
908891
}
909-
}
910-
else {
911-
$versionOutput = (& $exe --version 2>&1 | Out-String).Trim()
912-
if ($versionOutput -match 'mikefarah|github\.com/mikefarah') {
913-
return $true
892+
else {
893+
$available = $true
914894
}
895+
}
896+
}
897+
else {
898+
foreach ($candidate in @('go-yq', 'yq')) {
899+
foreach ($cmd in @(Get-Command $candidate -CommandType Application -ErrorAction SilentlyContinue)) {
900+
$exe = if (Get-Command Resolve-CommandExecutablePath -ErrorAction SilentlyContinue) {
901+
Resolve-CommandExecutablePath -CommandInfo $cmd
902+
}
903+
elseif (-not [string]::IsNullOrWhiteSpace($cmd.Name)) {
904+
[string]$cmd.Name
905+
}
906+
else {
907+
$null
908+
}
909+
if ([string]::IsNullOrWhiteSpace($exe)) {
910+
continue
911+
}
915912

916-
if ($versionOutput -notmatch '^yq\s+\d') {
917-
$evalHelp = (& $exe eval --help 2>&1 | Out-String)
918-
if ($evalHelp -match 'evaluates' -and $evalHelp -notmatch 'jq_filter') {
919-
return $true
913+
if (Get-Command Test-IsMikefarahYqExecutable -ErrorAction SilentlyContinue) {
914+
if (Test-IsMikefarahYqExecutable -Executable $exe) {
915+
$available = $true
916+
break
917+
}
918+
}
919+
else {
920+
$versionOutput = (& $exe --version 2>&1 | Out-String).Trim()
921+
if ($versionOutput -match 'mikefarah|github\.com/mikefarah') {
922+
$available = $true
923+
break
924+
}
925+
926+
if ($versionOutput -notmatch '^yq\s+\d') {
927+
$evalHelp = (& $exe eval --help 2>&1 | Out-String)
928+
if ($evalHelp -match 'evaluates' -and $evalHelp -notmatch 'jq_filter') {
929+
$available = $true
930+
break
931+
}
932+
}
920933
}
921934
}
935+
936+
if ($available) {
937+
break
938+
}
922939
}
923940
}
941+
942+
$script:MikefarahYqAvailabilityCache = $available
943+
return $available
924944
}
945+
catch {
946+
$script:MikefarahYqAvailabilityCache = $false
947+
return $false
948+
}
949+
}
925950

926-
return $false
951+
function Clear-TestMikefarahYqAvailabilityCache {
952+
<#
953+
.SYNOPSIS
954+
Clears the cached result used by Test-MikefarahYqAvailable.
955+
#>
956+
[CmdletBinding()]
957+
param()
958+
959+
if (Get-Variable -Name MikefarahYqAvailabilityCache -Scope Script -ErrorAction SilentlyContinue) {
960+
$script:MikefarahYqAvailabilityCache = $null
961+
}
927962
}
928963

929964
function Get-TestMikefarahYqSkipMessage {

0 commit comments

Comments
 (0)