From 95ce6b058e1716d57688ebec0faab20b25f61fe3 Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:50:18 -0700 Subject: [PATCH 1/6] ci: discover the installed MSVC instance for examples --- .github/workflows/examples-msvc.yml | 82 ++++++-------- examples/msvc/fmt.build.ps1 | 15 ++- scripts/ensure-msvc-spectre.ps1 | 100 +++++++++++++++++ scripts/tests/msvc-spectre.Tests.ps1 | 153 +++++++++++++++++++++++++++ 4 files changed, 301 insertions(+), 49 deletions(-) create mode 100644 scripts/ensure-msvc-spectre.ps1 create mode 100644 scripts/tests/msvc-spectre.Tests.ps1 diff --git a/.github/workflows/examples-msvc.yml b/.github/workflows/examples-msvc.yml index e629bccb8..9fd26a2a1 100644 --- a/.github/workflows/examples-msvc.yml +++ b/.github/workflows/examples-msvc.yml @@ -1,7 +1,7 @@ # MSVC Examples CI # # Runs all examples/msvc/*.build.ps1 scripts on a real Windows runner with -# MSVC Build Tools pre-installed (windows-latest already ships with VS 2022). +# MSVC tools pre-installed; discover the installed Visual Studio instance. # # Triggered when: # - examples/msvc/** changes @@ -17,10 +17,16 @@ on: paths: - "examples/msvc/**" - "crates/vx-providers/msvc/**" + - ".github/workflows/examples-msvc.yml" + - "scripts/ensure-msvc-spectre.ps1" + - "scripts/tests/msvc-spectre.Tests.ps1" pull_request: paths: - "examples/msvc/**" - "crates/vx-providers/msvc/**" + - ".github/workflows/examples-msvc.yml" + - "scripts/ensure-msvc-spectre.ps1" + - "scripts/tests/msvc-spectre.Tests.ps1" workflow_dispatch: inputs: script: @@ -35,8 +41,23 @@ concurrency: cancel-in-progress: true jobs: + test-msvc-setup: + name: Test MSVC setup contracts + runs-on: windows-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v7 + - name: Test discovery, Spectre fallback, and CMake selection + shell: pwsh + run: | + Import-Module Pester -MinimumVersion 5.0 -ErrorAction Stop + $result = Invoke-Pester -Path scripts/tests/msvc-spectre.Tests.ps1 -Output Detailed -PassThru + if ($result.FailedCount -gt 0) { exit 1 } + exit 0 + build-vx: name: Build VX (Windows) + needs: test-msvc-setup runs-on: windows-latest env: RUSTC_WRAPPER: sccache @@ -187,55 +208,20 @@ jobs: shell: pwsh run: vx --version - - name: Verify MSVC is available (windows-latest ships VS 2022) + - name: Verify MSVC and Spectre libraries shell: pwsh run: | - $clPaths = @( - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC", - "C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC", - "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC", - "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC" - ) - $found = $false - foreach ($p in $clPaths) { - if (Test-Path $p) { - Write-Host "Found MSVC at: $p" - $found = $true - break - } - } - if (-not $found) { - Write-Warning "MSVC not found in standard paths — vx will attempt to install it" - } - - - name: Ensure Spectre-mitigated VC libraries are installed - shell: pwsh - run: | - $vsRoot = "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" - $hasSpectre = @( - "$vsRoot\VC\Tools\MSVC\*\lib\x64\spectre", - "$vsRoot\VC\Tools\MSVC\*\lib\arm64\spectre" - ) | ForEach-Object { Test-Path $_ } | Where-Object { $_ } | Select-Object -First 1 - - if ($hasSpectre) { - Write-Host "Spectre libraries already installed" - exit 0 - } - - $installer = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe" - if (-not (Test-Path $installer)) { - throw "Visual Studio Installer not found at $installer" - } - - & $installer modify ` - --installPath $vsRoot ` - --add Microsoft.VisualStudio.Component.VC.ATL.Spectre ` - --add Microsoft.VisualStudio.Component.VC.MFC.Spectre ` - --add Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre ` - --quiet --norestart - - if ($LASTEXITCODE -ne 0) { - throw "Failed to install Spectre-mitigated VC libraries" + New-Item -ItemType Directory -Force examples/msvc/logs | Out-Null + Start-Transcript -Path examples/msvc/logs/msvc-setup.log + try { + . ./scripts/ensure-msvc-spectre.ps1 + $instance = Ensure-MsvcSpectre + $generator = Get-MsvcCMakeGenerator -InstallationVersion $instance.installationVersion + "CMAKE_GENERATOR=$generator" >> $env:GITHUB_ENV + "CMAKE_GENERATOR_INSTANCE=$($instance.installationPath)" >> $env:GITHUB_ENV + Write-Host "CMake generator: $generator" + } finally { + Stop-Transcript } - name: Run example — fmt (cmake + MSVC) diff --git a/examples/msvc/fmt.build.ps1 b/examples/msvc/fmt.build.ps1 index fd3389a69..b90c458c3 100644 --- a/examples/msvc/fmt.build.ps1 +++ b/examples/msvc/fmt.build.ps1 @@ -3,6 +3,19 @@ if ($null -ne (Get-Variable -Name PSNativeCommandUseErrorActionPreference -Error $PSNativeCommandUseErrorActionPreference = $false } +$generator = $env:CMAKE_GENERATOR +$instancePath = $env:CMAKE_GENERATOR_INSTANCE +if (-not $generator -or -not $instancePath) { + if ($generator -or $instancePath) { + throw "Set both CMAKE_GENERATOR and CMAKE_GENERATOR_INSTANCE, or leave both unset for discovery" + } + . (Join-Path $PSScriptRoot '../../scripts/ensure-msvc-spectre.ps1') + $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe' + $instance = Get-MsvcInstallation -VsWherePath $vswhere + $generator = Get-MsvcCMakeGenerator -InstallationVersion $instance.installationVersion + $instancePath = $instance.installationPath +} + $repoUrl = "https://github.com/fmtlib/fmt.git" $workRoot = Join-Path $PSScriptRoot "work" $repoDir = Join-Path $workRoot "fmt" @@ -50,7 +63,7 @@ try { } Invoke-LoggedNative "cmake configure" { - vx cmake -S . -B $buildDir -G "Visual Studio 17 2022" -A x64 -DFMT_TEST=OFF + vx cmake -S . -B $buildDir -G $generator -A x64 "-DCMAKE_GENERATOR_INSTANCE=$instancePath" -DFMT_TEST=OFF } Invoke-LoggedNative "cmake build" { diff --git a/scripts/ensure-msvc-spectre.ps1 b/scripts/ensure-msvc-spectre.ps1 new file mode 100644 index 000000000..ee175ddaa --- /dev/null +++ b/scripts/ensure-msvc-spectre.ps1 @@ -0,0 +1,100 @@ +# Discover the installed VC instance instead of assuming a VS year or edition. +# Dot-source this file to test the setup functions without changing the machine. + +function Get-MsvcInstallation { + param([string]$VsWherePath) + + $PSNativeCommandUseErrorActionPreference = $false + if (-not (Test-Path -LiteralPath $VsWherePath -PathType Leaf)) { + throw "Visual Studio locator not found at $VsWherePath" + } + + $json = (& $VsWherePath -latest -products '*' ` + -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -format json -utf8) -join "`n" + if ($LASTEXITCODE -ne 0) { + throw "Visual Studio locator failed with exit code $LASTEXITCODE" + } + + $instances = @($json | ConvertFrom-Json) + if ($instances.Count -ne 1 -or [string]::IsNullOrWhiteSpace($instances[0].installationPath)) { + throw "Expected one installed Visual Studio instance with x64 C++ tools" + } + $instance = $instances[0] + $tools = Join-Path $instance.installationPath 'VC/Tools/MSVC' + if (-not (Test-Path -LiteralPath $tools -PathType Container)) { + throw "MSVC tools were not found in the selected instance: $tools" + } + return $instance +} + +function Test-MsvcSpectre { + param([string]$InstallationPath) + + # The examples build x64 targets. ARM64 libraries alone do not satisfy them. + return [bool](Test-Path -Path (Join-Path $InstallationPath 'VC/Tools/MSVC/*/lib/x64/spectre') -PathType Container) +} + +function Get-MsvcCMakeGenerator { + param([string]$InstallationVersion) + + # These are CMake's documented generator names, not a name inferred from a year. + # https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html + switch (([version]$InstallationVersion).Major) { + 17 { return 'Visual Studio 17 2022' } + 18 { return 'Visual Studio 18 2026' } + default { throw "Unsupported Visual Studio version for the CMake example: $InstallationVersion" } + } +} + +function Ensure-MsvcSpectre { + param( + [string]$InstallerDirectory = (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer') + ) + + $instance = Get-MsvcInstallation -VsWherePath (Join-Path $InstallerDirectory 'vswhere.exe') + $vsRoot = $instance.installationPath + Write-Host "Selected Visual Studio $($instance.installationVersion) at $vsRoot" + + if (Test-MsvcSpectre -InstallationPath $vsRoot) { + Write-Host "x64 Spectre libraries already installed in the selected instance" + return $instance + } + + $installer = Join-Path $InstallerDirectory 'setup.exe' + if (-not (Test-Path -LiteralPath $installer -PathType Leaf)) { + throw "Visual Studio Installer not found at $installer" + } + if ([string]::IsNullOrWhiteSpace($instance.channelId)) { + throw "The selected Visual Studio instance has no channelId for modification" + } + + # Start-Process joins ArgumentList into a command line, so preserve the quotes + # around an installation path containing spaces. --wait is bootstrapper-only; + # use PowerShell's -Wait and the returned process ExitCode for setup.exe. + $arguments = @( + 'modify', '--installPath', ('"{0}"' -f $vsRoot), + '--channelId', $instance.channelId, + '--add', 'Microsoft.VisualStudio.Component.VC.ATL.Spectre', + '--add', 'Microsoft.VisualStudio.Component.VC.MFC.Spectre', + '--add', 'Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre', + '--quiet', '--norestart' + ) + $process = Start-Process -FilePath $installer -ArgumentList $arguments -Wait -PassThru + $exitCode = $process.ExitCode + Write-Host "Visual Studio Installer exit code: $exitCode" + if ($exitCode -notin @(0, 3010)) { + throw "Spectre library installation failed for '$vsRoot' with exit code $exitCode" + } + if ($exitCode -eq 3010) { + Write-Warning "Installer requested a restart; validating the installed libraries before continuing" + } + if (-not (Test-MsvcSpectre -InstallationPath $vsRoot)) { + throw "Installer returned $exitCode but x64 Spectre libraries are still missing in '$vsRoot'" + } + return $instance +} + +if ($MyInvocation.InvocationName -ne '.') { + $ErrorActionPreference = 'Stop' + Ensure-MsvcSpectre | Out-Null +} diff --git a/scripts/tests/msvc-spectre.Tests.ps1 b/scripts/tests/msvc-spectre.Tests.ps1 new file mode 100644 index 000000000..d6f8917db --- /dev/null +++ b/scripts/tests/msvc-spectre.Tests.ps1 @@ -0,0 +1,153 @@ +BeforeAll { + . (Join-Path $PSScriptRoot '../ensure-msvc-spectre.ps1') + function Invoke-FakeVsWhere {} +} + +Describe 'Visual Studio discovery' { + BeforeEach { + Mock Test-Path { $true } + Mock Invoke-FakeVsWhere { + $global:LASTEXITCODE = 0 + '[{"installationPath":"C:\\Visual Studio\\18\\Enterprise","installationVersion":"18.10.12217.157","channelId":"VisualStudio.18.Release"}]' + } + } + + It 'reads the locator-selected instance without assuming a year or edition' { + $instance = Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' + $instance.installationPath | Should -Be 'C:\Visual Studio\18\Enterprise' + $instance.installationVersion | Should -Be '18.10.12217.157' + } + + It 'fails clearly when vswhere is missing' { + Mock Test-Path { $false } + { Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' } | Should -Throw '*locator not found*' + Should -Invoke Invoke-FakeVsWhere -Times 0 + } + + It 'reports the locator exit code' { + Mock Invoke-FakeVsWhere { $global:LASTEXITCODE = 42; '[]' } + { Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' } | Should -Throw '*exit code 42*' + } + + It 'rejects an empty discovery result' { + Mock Invoke-FakeVsWhere { $global:LASTEXITCODE = 0; '[]' } + { Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' } | Should -Throw '*Expected one installed*' + } + + It 'rejects a selected instance without an MSVC directory' { + Mock Test-Path { param($LiteralPath) $LiteralPath -eq 'Invoke-FakeVsWhere' } + { Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' } | Should -Throw '*MSVC tools were not found*' + } +} + +Describe 'Spectre library detection' { + It 'finds x64 libraries below the selected instance' { + $root = Join-Path $TestDrive 'VS Enterprise' + New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/x64/spectre') | Out-Null + Test-MsvcSpectre -InstallationPath $root | Should -BeTrue + } + + It 'does not treat ARM64-only libraries as x64 support' { + $root = Join-Path $TestDrive 'VS ARM64' + New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/arm64/spectre') | Out-Null + Test-MsvcSpectre -InstallationPath $root | Should -BeFalse + } +} + +Describe 'Spectre setup' { + BeforeEach { + Mock Get-MsvcInstallation { + [pscustomobject]@{ + installationPath = 'C:\Visual Studio\18\Enterprise' + installationVersion = '18.10.12217.157' + channelId = 'VisualStudio.18.Release' + } + } + Mock Test-Path { $true } + Mock Test-MsvcSpectre { $true } + Mock Start-Process { [pscustomobject]@{ ExitCode = 0 } } + } + + It 'reuses installed libraries without running the installer' { + (Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer').installationPath | + Should -Be 'C:\Visual Studio\18\Enterprise' + Should -Invoke Start-Process -Times 0 + Should -Invoke Test-MsvcSpectre -Times 1 -ParameterFilter { + $InstallationPath -eq 'C:\Visual Studio\18\Enterprise' + } + } + + It 'modifies and rechecks the same instance after a successful install' { + $script:checks = 0 + Mock Test-MsvcSpectre { $script:checks++; $script:checks -gt 1 } + Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' | Out-Null + Should -Invoke Start-Process -Times 1 -ParameterFilter { + $FilePath -eq 'C:\VS Installer\setup.exe' -and $Wait -and $PassThru -and + $ArgumentList -contains '"C:\Visual Studio\18\Enterprise"' -and + $ArgumentList -contains 'VisualStudio.18.Release' -and + $ArgumentList -notcontains '--wait' -and + $ArgumentList -contains 'Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre' + } + Should -Invoke Test-MsvcSpectre -Times 2 -ParameterFilter { + $InstallationPath -eq 'C:\Visual Studio\18\Enterprise' + } + } + + It 'reports an unsuccessful installer exit code' { + Mock Test-MsvcSpectre { $false } + Mock Start-Process { [pscustomobject]@{ ExitCode = 1603 } } + { Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' } | Should -Throw '*exit code 1603*' + } + + It 'accepts 3010 only after installed libraries are verified' { + $script:checks = 0 + Mock Test-MsvcSpectre { $script:checks++; $script:checks -gt 1 } + Mock Start-Process { [pscustomobject]@{ ExitCode = 3010 } } + Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' | Out-Null + Should -Invoke Test-MsvcSpectre -Times 2 + } + + It 'rejects successful exit codes when libraries are still absent' -TestCases @( + @{ Code = 0 }, @{ Code = 3010 } + ) { + param($Code) + Mock Test-MsvcSpectre { $false } + $script:installerExitCode = $Code + Mock Start-Process { [pscustomobject]@{ ExitCode = $script:installerExitCode } } + { Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' } | Should -Throw '*libraries are still missing*' + } + + It 'fails before modification if the installer is missing' { + Mock Test-MsvcSpectre { $false } + Mock Test-Path { $false } + { Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' } | Should -Throw '*Installer not found*' + Should -Invoke Start-Process -Times 0 + } + + It 'does not guess an instance when discovery fails' { + Mock Get-MsvcInstallation { throw 'No VC instance' } + { Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' } | Should -Throw '*No VC instance*' + Should -Invoke Start-Process -Times 0 + } +} + +Describe 'CMake generator selection' { + It 'preserves the documented VS2022 and VS2026 generators' -TestCases @( + @{ Version = '17.14.1.0'; Generator = 'Visual Studio 17 2022' }, + @{ Version = '18.10.12217.157'; Generator = 'Visual Studio 18 2026' } + ) { + param($Version, $Generator) + Get-MsvcCMakeGenerator -InstallationVersion $Version | Should -Be $Generator + } + + It 'fails explicitly for an unknown Visual Studio major version' { + { Get-MsvcCMakeGenerator -InstallationVersion '19.0.0.0' } | Should -Throw '*Unsupported Visual Studio version*' + } + + It 'passes the selected generator and instance into fmt configuration' { + $fmt = Get-Content (Join-Path $PSScriptRoot '../../examples/msvc/fmt.build.ps1') -Raw + $fmt | Should -Match '-G \$generator -A x64' + $fmt | Should -Match '-DCMAKE_GENERATOR_INSTANCE=\$instancePath' + $fmt | Should -Not -Match '-G "Visual Studio 17 2022"' + } +} From 159651812db080c52bee79c6b85ce70ed04c4aec Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:07:41 -0700 Subject: [PATCH 2/6] ci: detect the actual MSVC Spectre library layout --- scripts/ensure-msvc-spectre.ps1 | 8 +++++++- scripts/tests/msvc-spectre.Tests.ps1 | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/scripts/ensure-msvc-spectre.ps1 b/scripts/ensure-msvc-spectre.ps1 index ee175ddaa..ff084fc16 100644 --- a/scripts/ensure-msvc-spectre.ps1 +++ b/scripts/ensure-msvc-spectre.ps1 @@ -31,7 +31,13 @@ function Test-MsvcSpectre { param([string]$InstallationPath) # The examples build x64 targets. ARM64 libraries alone do not satisfy them. - return [bool](Test-Path -Path (Join-Path $InstallationPath 'VC/Tools/MSVC/*/lib/x64/spectre') -PathType Container) + # MSVC places the mitigation before the target architecture: lib/spectre/x64. + $libraryPath = Join-Path $InstallationPath 'VC/Tools/MSVC/*/lib/spectre/x64' + $present = Test-Path -Path $libraryPath -PathType Container + if ($present) { + Write-Host "Found x64 Spectre libraries matching $libraryPath" + } + return [bool]$present } function Get-MsvcCMakeGenerator { diff --git a/scripts/tests/msvc-spectre.Tests.ps1 b/scripts/tests/msvc-spectre.Tests.ps1 index d6f8917db..886b120bc 100644 --- a/scripts/tests/msvc-spectre.Tests.ps1 +++ b/scripts/tests/msvc-spectre.Tests.ps1 @@ -43,13 +43,19 @@ Describe 'Visual Studio discovery' { Describe 'Spectre library detection' { It 'finds x64 libraries below the selected instance' { $root = Join-Path $TestDrive 'VS Enterprise' - New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/x64/spectre') | Out-Null + New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/spectre/x64') | Out-Null Test-MsvcSpectre -InstallationPath $root | Should -BeTrue } It 'does not treat ARM64-only libraries as x64 support' { $root = Join-Path $TestDrive 'VS ARM64' - New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/arm64/spectre') | Out-Null + New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/spectre/arm64') | Out-Null + Test-MsvcSpectre -InstallationPath $root | Should -BeFalse + } + + It 'rejects the old inverted library directory layout' { + $root = Join-Path $TestDrive 'VS incorrect layout' + New-Item -ItemType Directory -Force (Join-Path $root 'VC/Tools/MSVC/14.50/lib/x64/spectre') | Out-Null Test-MsvcSpectre -InstallationPath $root | Should -BeFalse } } From e9feb4897aed7671de3bc3695757d03dad21803a Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:32:01 -0700 Subject: [PATCH 3/6] ci: share a compatible VS2022 instance across MSVC examples --- .github/workflows/examples-msvc.yml | 10 ++++- scripts/ensure-msvc-spectre.ps1 | 20 ++++++--- scripts/tests/msvc-spectre.Tests.ps1 | 61 ++++++++++++++++++++++------ 3 files changed, 72 insertions(+), 19 deletions(-) diff --git a/.github/workflows/examples-msvc.yml b/.github/workflows/examples-msvc.yml index 9fd26a2a1..74cb8e067 100644 --- a/.github/workflows/examples-msvc.yml +++ b/.github/workflows/examples-msvc.yml @@ -214,11 +214,19 @@ jobs: New-Item -ItemType Directory -Force examples/msvc/logs | Out-Null Start-Transcript -Path examples/msvc/logs/msvc-setup.log try { + # Initialize the existing VX MSVC Provider before selecting the one + # instance shared by every example. node-gyp 11.x cannot use VS18. + vx cl /? + if ($LASTEXITCODE -ne 0) { throw "VX MSVC preflight failed with exit code $LASTEXITCODE" } . ./scripts/ensure-msvc-spectre.ps1 - $instance = Ensure-MsvcSpectre + $instance = Ensure-MsvcSpectre -MajorVersion 17 $generator = Get-MsvcCMakeGenerator -InstallationVersion $instance.installationVersion "CMAKE_GENERATOR=$generator" >> $env:GITHUB_ENV "CMAKE_GENERATOR_INSTANCE=$($instance.installationPath)" >> $env:GITHUB_ENV + # node-gyp accepts an exact installation path, not only a year. + # Export both supported config forms for the examples' gyp versions. + "npm_config_msvs_version=$($instance.installationPath)" >> $env:GITHUB_ENV + "npm_package_config_node_gyp_msvs_version=$($instance.installationPath)" >> $env:GITHUB_ENV Write-Host "CMake generator: $generator" } finally { Stop-Transcript diff --git a/scripts/ensure-msvc-spectre.ps1 b/scripts/ensure-msvc-spectre.ps1 index ff084fc16..fec8bb9ff 100644 --- a/scripts/ensure-msvc-spectre.ps1 +++ b/scripts/ensure-msvc-spectre.ps1 @@ -2,14 +2,20 @@ # Dot-source this file to test the setup functions without changing the machine. function Get-MsvcInstallation { - param([string]$VsWherePath) + param( + [string]$VsWherePath, + # The current VX Provider and node-pty's node-gyp 11.x share VS2022 support. + # Callers may opt into a newer major when every consumer supports it. + [ValidateSet(17, 18)][int]$MajorVersion = 17 + ) $PSNativeCommandUseErrorActionPreference = $false if (-not (Test-Path -LiteralPath $VsWherePath -PathType Leaf)) { throw "Visual Studio locator not found at $VsWherePath" } - $json = (& $VsWherePath -latest -products '*' ` + $versionRange = '[{0}.0,{1}.0)' -f $MajorVersion, ($MajorVersion + 1) + $json = (& $VsWherePath -latest -products '*' -version $versionRange ` -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -format json -utf8) -join "`n" if ($LASTEXITCODE -ne 0) { throw "Visual Studio locator failed with exit code $LASTEXITCODE" @@ -17,9 +23,12 @@ function Get-MsvcInstallation { $instances = @($json | ConvertFrom-Json) if ($instances.Count -ne 1 -or [string]::IsNullOrWhiteSpace($instances[0].installationPath)) { - throw "Expected one installed Visual Studio instance with x64 C++ tools" + throw "Expected one installed Visual Studio $MajorVersion instance with x64 C++ tools" } $instance = $instances[0] + if (([version]$instance.installationVersion).Major -ne $MajorVersion) { + throw "Discovered Visual Studio $($instance.installationVersion) outside required range $versionRange" + } $tools = Join-Path $instance.installationPath 'VC/Tools/MSVC' if (-not (Test-Path -LiteralPath $tools -PathType Container)) { throw "MSVC tools were not found in the selected instance: $tools" @@ -54,10 +63,11 @@ function Get-MsvcCMakeGenerator { function Ensure-MsvcSpectre { param( - [string]$InstallerDirectory = (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer') + [string]$InstallerDirectory = (Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer'), + [ValidateSet(17, 18)][int]$MajorVersion = 17 ) - $instance = Get-MsvcInstallation -VsWherePath (Join-Path $InstallerDirectory 'vswhere.exe') + $instance = Get-MsvcInstallation -VsWherePath (Join-Path $InstallerDirectory 'vswhere.exe') -MajorVersion $MajorVersion $vsRoot = $instance.installationPath Write-Host "Selected Visual Studio $($instance.installationVersion) at $vsRoot" diff --git a/scripts/tests/msvc-spectre.Tests.ps1 b/scripts/tests/msvc-spectre.Tests.ps1 index 886b120bc..431266803 100644 --- a/scripts/tests/msvc-spectre.Tests.ps1 +++ b/scripts/tests/msvc-spectre.Tests.ps1 @@ -1,6 +1,9 @@ BeforeAll { . (Join-Path $PSScriptRoot '../ensure-msvc-spectre.ps1') - function Invoke-FakeVsWhere {} + function Invoke-FakeVsWhere { + param([switch]$latest, [string]$products, [string]$version, + [string]$requires, [string]$format, [switch]$utf8) + } } Describe 'Visual Studio discovery' { @@ -8,14 +11,36 @@ Describe 'Visual Studio discovery' { Mock Test-Path { $true } Mock Invoke-FakeVsWhere { $global:LASTEXITCODE = 0 - '[{"installationPath":"C:\\Visual Studio\\18\\Enterprise","installationVersion":"18.10.12217.157","channelId":"VisualStudio.18.Release"}]' + '[{"installationPath":"C:\\Visual Studio\\2022\\Enterprise","installationVersion":"17.14.37710.0","channelId":"VisualStudio.17.Release"}]' } } - It 'reads the locator-selected instance without assuming a year or edition' { + It 'selects the common supported VS17 range without assuming an edition or path' { $instance = Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' - $instance.installationPath | Should -Be 'C:\Visual Studio\18\Enterprise' - $instance.installationVersion | Should -Be '18.10.12217.157' + $instance.installationPath | Should -Be 'C:\Visual Studio\2022\Enterprise' + $instance.installationVersion | Should -Be '17.14.37710.0' + Should -Invoke Invoke-FakeVsWhere -Times 1 -ParameterFilter { + $latest -and $products -eq '*' -and $version -eq '[17.0,18.0)' -and + $requires -eq 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' + } + } + + It 'rejects a locator result outside the required compatibility range' { + Mock Invoke-FakeVsWhere { + $global:LASTEXITCODE = 0 + '[{"installationPath":"C:\\VS18","installationVersion":"18.10.1.0"}]' + } + { Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' } | Should -Throw '*outside required range*' + } + + It 'allows explicit VS18 selection when consumers support it' { + Mock Invoke-FakeVsWhere { + $global:LASTEXITCODE = 0 + '[{"installationPath":"C:\\VS18","installationVersion":"18.10.1.0"}]' + } + (Get-MsvcInstallation -VsWherePath 'Invoke-FakeVsWhere' -MajorVersion 18).installationVersion | + Should -Be '18.10.1.0' + Should -Invoke Invoke-FakeVsWhere -Times 1 -ParameterFilter { $version -eq '[18.0,19.0)' } } It 'fails clearly when vswhere is missing' { @@ -64,9 +89,9 @@ Describe 'Spectre setup' { BeforeEach { Mock Get-MsvcInstallation { [pscustomobject]@{ - installationPath = 'C:\Visual Studio\18\Enterprise' - installationVersion = '18.10.12217.157' - channelId = 'VisualStudio.18.Release' + installationPath = 'C:\Visual Studio\2022\Enterprise' + installationVersion = '17.14.37710.0' + channelId = 'VisualStudio.17.Release' } } Mock Test-Path { $true } @@ -76,10 +101,10 @@ Describe 'Spectre setup' { It 'reuses installed libraries without running the installer' { (Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer').installationPath | - Should -Be 'C:\Visual Studio\18\Enterprise' + Should -Be 'C:\Visual Studio\2022\Enterprise' Should -Invoke Start-Process -Times 0 Should -Invoke Test-MsvcSpectre -Times 1 -ParameterFilter { - $InstallationPath -eq 'C:\Visual Studio\18\Enterprise' + $InstallationPath -eq 'C:\Visual Studio\2022\Enterprise' } } @@ -89,13 +114,13 @@ Describe 'Spectre setup' { Ensure-MsvcSpectre -InstallerDirectory 'C:\VS Installer' | Out-Null Should -Invoke Start-Process -Times 1 -ParameterFilter { $FilePath -eq 'C:\VS Installer\setup.exe' -and $Wait -and $PassThru -and - $ArgumentList -contains '"C:\Visual Studio\18\Enterprise"' -and - $ArgumentList -contains 'VisualStudio.18.Release' -and + $ArgumentList -contains '"C:\Visual Studio\2022\Enterprise"' -and + $ArgumentList -contains 'VisualStudio.17.Release' -and $ArgumentList -notcontains '--wait' -and $ArgumentList -contains 'Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre' } Should -Invoke Test-MsvcSpectre -Times 2 -ParameterFilter { - $InstallationPath -eq 'C:\Visual Studio\18\Enterprise' + $InstallationPath -eq 'C:\Visual Studio\2022\Enterprise' } } @@ -157,3 +182,13 @@ Describe 'CMake generator selection' { $fmt | Should -Not -Match '-G "Visual Studio 17 2022"' } } + +Describe 'Workflow consumer compatibility' { + It 'initializes VX before checking the common instance and binds CMake and node-gyp to it' { + $workflow = Get-Content (Join-Path $PSScriptRoot '../../.github/workflows/examples-msvc.yml') -Raw + $workflow | Should -Match '(?s)vx cl /\?.*Ensure-MsvcSpectre -MajorVersion 17' + $workflow | Should -Match 'CMAKE_GENERATOR_INSTANCE=\$\(\$instance.installationPath\)' + $workflow | Should -Match 'npm_config_msvs_version=\$\(\$instance.installationPath\)' + $workflow | Should -Match 'npm_package_config_node_gyp_msvs_version=\$\(\$instance.installationPath\)' + } +} From 743ba35afa9cee927c844f2cb1cc7d581aed989e Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Tue, 13 Oct 2026 01:39:20 -0700 Subject: [PATCH 4/6] ci: follow AionUi's pinned Bun workspace contract Fetch the reviewed upstream revision without editing its manifests or lock. Use the existing VX Bun pin and frozen install, source-build better-sqlite3, and validate a real SQLite query with Node. Preserve native failure checks. --- examples/msvc/aionui.build.ps1 | 54 ++++++++++++++++------------- scripts/tests/aionui-exit.Tests.ps1 | 37 +++++++++++++++++--- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/examples/msvc/aionui.build.ps1 b/examples/msvc/aionui.build.ps1 index 9c838b8dd..2362fe189 100644 --- a/examples/msvc/aionui.build.ps1 +++ b/examples/msvc/aionui.build.ps1 @@ -1,7 +1,9 @@ $ErrorActionPreference = "Stop" $repoUrl = "https://github.com/iOfficeAI/AionUi.git" +# Keep the upstream workspace manifests and bun.lock from one reviewed revision. +$repoCommit = "6744099b279b991c17e31c243f0920477bd31cb6" $workRoot = Join-Path $PSScriptRoot "work" -$repoDir = Join-Path $workRoot "aionui" +$repoDir = Join-Path $workRoot "aionui-$($repoCommit.Substring(0, 12))" $logDir = Join-Path $PSScriptRoot "logs" $logFile = Join-Path $logDir "aionui.log" @@ -12,45 +14,47 @@ New-Item -ItemType Directory -Force -Path $logDir | Out-Null "Working directory: $repoDir" | Tee-Object -FilePath $logFile -Append if (-not (Test-Path $repoDir)) { - git clone --depth 1 $repoUrl $repoDir 2>&1 | Tee-Object -FilePath $logFile -Append - if ($LASTEXITCODE -ne 0) { throw "git clone failed with exit code $LASTEXITCODE" } + git init $repoDir 2>&1 | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "git init failed with exit code $LASTEXITCODE" } + git -C $repoDir remote add origin $repoUrl 2>&1 | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "git remote add failed with exit code $LASTEXITCODE" } + git -C $repoDir fetch --depth 1 origin $repoCommit 2>&1 | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "git fetch failed with exit code $LASTEXITCODE" } + git -C $repoDir checkout --detach $repoCommit 2>&1 | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "git checkout failed with exit code $LASTEXITCODE" } } +$actualCommit = git -C $repoDir rev-parse HEAD +if ($LASTEXITCODE -ne 0) { throw "git rev-parse failed with exit code $LASTEXITCODE" } +if ("$actualCommit".Trim() -ne $repoCommit) { throw "AionUi checkout must be at $repoCommit; found $actualCommit" } +git -C $repoDir diff --quiet HEAD -- +if ($LASTEXITCODE -ne 0) { throw "AionUi checkout has local changes or cannot be checked (exit code $LASTEXITCODE)" } +"Source commit: $actualCommit" | Tee-Object -FilePath $logFile -Append + Push-Location $repoDir try { - # Workaround 1: npm doesn't support pnpm/yarn "workspace:*" protocol. - # AionUi is a monorepo with internal deps like "@aionui/web-host": "workspace:*". - # Replace with "*" so npm can resolve workspace packages. - Get-ChildItem -Path . -Recurse -Filter "package.json" -Depth 3 | ForEach-Object { - $c = Get-Content $_.FullName -Raw -Encoding UTF8 - if ($c -match 'workspace:') { - Write-Host "Replacing workspace:* in $($_.FullName)" - $c = $c -replace '"workspace:\*"', '"*"' - [System.IO.File]::WriteAllText($_.FullName, $c, [System.Text.Encoding]::UTF8) - } - } - - # Workaround 2: npm EOVERRIDE — @codemirror/language is both a direct dependency - # and in overrides/resolutions. npm 10+ rejects overrides on direct deps when - # the version specs differ. Remove the redundant override/resolution entries. - vx npm pkg delete overrides.@codemirror/language resolutions.@codemirror/language 2>&1 | Tee-Object -FilePath $logFile -Append - if ($LASTEXITCODE -ne 0) { throw "npm pkg delete failed with exit code $LASTEXITCODE" } - vx --version | Tee-Object -FilePath $logFile -Append if ($LASTEXITCODE -ne 0) { throw "vx --version failed with exit code $LASTEXITCODE" } + # VX discovers this repository's existing Bun pin above the upstream checkout. + vx bun --version | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "bun --version failed with exit code $LASTEXITCODE" } vx npm --version | Tee-Object -FilePath $logFile -Append if ($LASTEXITCODE -ne 0) { throw "npm --version failed with exit code $LASTEXITCODE" } - vx npm install 2>&1 | Tee-Object -FilePath $logFile -Append - if ($LASTEXITCODE -ne 0) { throw "npm install failed with exit code $LASTEXITCODE" } + # Follow upstream's Bun workspace contract without rewriting dependencies. + vx bun install --frozen-lockfile 2>&1 | Tee-Object -FilePath $logFile -Append + if ($LASTEXITCODE -ne 0) { throw "bun install failed with exit code $LASTEXITCODE" } + # Source-build AionUi's locked native SQLite package for the VX Node ABI. vx npm rebuild better-sqlite3 --build-from-source 2>&1 | Tee-Object -FilePath $logFile -Append if ($LASTEXITCODE -ne 0) { throw "npm rebuild failed with exit code $LASTEXITCODE" } - vx node -e "require('better-sqlite3');require('node-pty');console.log('AionUi native modules OK')" 2>&1 | Tee-Object -FilePath $logFile -Append + vx node -e "const Database = require('better-sqlite3'); const db = new Database(':memory:'); if (db.prepare('SELECT 1 AS value').get().value !== 1) { throw new Error('SQLite query failed'); } db.close(); console.log('AionUi native SQLite OK');" 2>&1 | Tee-Object -FilePath $logFile -Append if ($LASTEXITCODE -ne 0) { throw "node module validation failed with exit code $LASTEXITCODE" } - "AionUi test passed" | Tee-Object -FilePath $logFile -Append + git diff --quiet HEAD -- + if ($LASTEXITCODE -ne 0) { throw "AionUi source or lockfile changed during the test (exit code $LASTEXITCODE)" } + "AionUi test passed (better-sqlite3 rebuilt for Node)" | Tee-Object -FilePath $logFile -Append } finally { Pop-Location diff --git a/scripts/tests/aionui-exit.Tests.ps1 b/scripts/tests/aionui-exit.Tests.ps1 index c60141853..7d508b69d 100644 --- a/scripts/tests/aionui-exit.Tests.ps1 +++ b/scripts/tests/aionui-exit.Tests.ps1 @@ -5,14 +5,23 @@ Describe 'AionUi native command failures' -Skip:(-not $IsWindows) { $script:originalFailureCommand = $env:VX_AION_TEST_FAILURE_COMMAND $script:originalFailureCode = $env:VX_AION_TEST_FAILURE_CODE $script:originalCallsFile = $env:VX_AION_TEST_CALLS + $script:originalGitRevision = $env:VX_AION_TEST_GIT_REVISION $script:originalLocation = (Get-Location).Path $script:fixture = Join-Path $TestDrive ([guid]::NewGuid().ToString()) $bin = Join-Path $script:fixture 'bin' - $repo = Join-Path $script:fixture 'work/aionui' - New-Item -ItemType Directory -Force -Path $bin, $repo | Out-Null - '{}' | Set-Content (Join-Path $repo 'package.json') + New-Item -ItemType Directory -Force -Path $bin | Out-Null $script:example = Join-Path $script:fixture 'aionui.build.ps1' Copy-Item (Join-Path $PSScriptRoot '../../examples/msvc/aionui.build.ps1') $script:example + $pin = [regex]::Match((Get-Content $script:example -Raw), '\$repoCommit = "([a-f0-9]{40})"') + $pin.Success | Should -BeTrue + $script:repoCommit = $pin.Groups[1].Value + $repo = Join-Path $script:fixture "work/aionui-$($script:repoCommit.Substring(0, 12))" + New-Item -ItemType Directory -Force -Path $repo | Out-Null + $script:manifest = Join-Path $repo 'package.json' + '{"dependencies":{"@aionui/web-host":"workspace:*"},"resolutions":{"@codemirror/language":"6.12.3"}}' | + Set-Content $script:manifest + $script:originalManifest = Get-Content $script:manifest -Raw + $env:VX_AION_TEST_GIT_REVISION = $script:repoCommit $script:logFile = Join-Path $script:fixture 'logs/aionui.log' $env:VX_AION_TEST_CALLS = Join-Path $script:fixture 'calls.txt' $env:VX_AION_TEST_FAILURE_COMMAND = '' @@ -27,8 +36,14 @@ if "%~1 %~2"=="%VX_AION_TEST_FAILURE_COMMAND%" ( ) exit /b 0 '@ | Set-Content (Join-Path $bin 'vx.cmd') -Encoding ascii + @' +@echo off +if "%~3"=="rev-parse" echo %VX_AION_TEST_GIT_REVISION% +exit /b 0 +'@ | Set-Content (Join-Path $bin 'git.cmd') -Encoding ascii $env:PATH = "$bin;$env:PATH" (Get-Command vx).Source | Should -Be (Join-Path $bin 'vx.cmd') + (Get-Command git).Source | Should -Be (Join-Path $bin 'git.cmd') # The script must check native status even with PowerShell's default policy. $PSNativeCommandUseErrorActionPreference = $false } @@ -38,10 +53,11 @@ exit /b 0 $env:VX_AION_TEST_FAILURE_COMMAND = $script:originalFailureCommand $env:VX_AION_TEST_FAILURE_CODE = $script:originalFailureCode $env:VX_AION_TEST_CALLS = $script:originalCallsFile + $env:VX_AION_TEST_GIT_REVISION = $script:originalGitRevision } It 'stops after exits without claiming success' -TestCases @( - @{ Command = 'npm install'; Code = 31; ExpectedCalls = 4 }, + @{ Command = 'bun install'; Code = 31; ExpectedCalls = 4 }, @{ Command = 'npm rebuild'; Code = 32; ExpectedCalls = 5 }, @{ Command = 'node -e'; Code = 33; ExpectedCalls = 6 } ) { @@ -67,8 +83,21 @@ exit /b 0 $LASTEXITCODE | Should -Be 0 $calls = @(Get-Content $env:VX_AION_TEST_CALLS) $calls.Count | Should -Be 6 + $calls[3] | Should -BeExactly 'bun install --frozen-lockfile' $calls[-1] | Should -BeLike 'node -e*' + $calls[-1] | Should -Match 'new Database' + $calls[-1] | Should -Not -Match 'node-pty' + (Get-Content $script:manifest -Raw) | Should -BeExactly $script:originalManifest (Get-Content $script:logFile -Raw) | Should -Match 'AionUi test passed' (Get-Location).Path | Should -Be $script:originalLocation } + + It 'rejects an unexpected upstream revision before running package commands' { + $env:VX_AION_TEST_GIT_REVISION = '0000000000000000000000000000000000000000' + + { & $script:example | Out-Null } | Should -Throw '*checkout must be at*' + + Test-Path $env:VX_AION_TEST_CALLS | Should -BeFalse + (Get-Content $script:logFile -Raw) | Should -Not -Match 'AionUi test passed' + } } From 40a52f308f40385a0f2da602e3b48f07f7172a99 Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Tue, 13 Oct 2026 02:18:24 -0700 Subject: [PATCH 5/6] ci: serialize linked AionUi native rebuild scripts --- examples/msvc/aionui.build.ps1 | 4 +++- scripts/tests/aionui-exit.Tests.ps1 | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/msvc/aionui.build.ps1 b/examples/msvc/aionui.build.ps1 index 2362fe189..96a976f1c 100644 --- a/examples/msvc/aionui.build.ps1 +++ b/examples/msvc/aionui.build.ps1 @@ -46,7 +46,9 @@ try { if ($LASTEXITCODE -ne 0) { throw "bun install failed with exit code $LASTEXITCODE" } # Source-build AionUi's locked native SQLite package for the VX Node ABI. - vx npm rebuild better-sqlite3 --build-from-source 2>&1 | Tee-Object -FilePath $logFile -Append + # Bun workspace links can reference one build directory more than once; + # serialize npm lifecycle scripts so those builds cannot race on Windows. + vx npm rebuild better-sqlite3 --build-from-source --foreground-scripts 2>&1 | Tee-Object -FilePath $logFile -Append if ($LASTEXITCODE -ne 0) { throw "npm rebuild failed with exit code $LASTEXITCODE" } vx node -e "const Database = require('better-sqlite3'); const db = new Database(':memory:'); if (db.prepare('SELECT 1 AS value').get().value !== 1) { throw new Error('SQLite query failed'); } db.close(); console.log('AionUi native SQLite OK');" 2>&1 | Tee-Object -FilePath $logFile -Append diff --git a/scripts/tests/aionui-exit.Tests.ps1 b/scripts/tests/aionui-exit.Tests.ps1 index 7d508b69d..0cd51d3db 100644 --- a/scripts/tests/aionui-exit.Tests.ps1 +++ b/scripts/tests/aionui-exit.Tests.ps1 @@ -84,6 +84,7 @@ exit /b 0 $calls = @(Get-Content $env:VX_AION_TEST_CALLS) $calls.Count | Should -Be 6 $calls[3] | Should -BeExactly 'bun install --frozen-lockfile' + $calls[4] | Should -BeExactly 'npm rebuild better-sqlite3 --build-from-source --foreground-scripts' $calls[-1] | Should -BeLike 'node -e*' $calls[-1] | Should -Match 'new Database' $calls[-1] | Should -Not -Match 'node-pty' From 55f5f16fe95aff8ea6dc352634de42f23eb26893 Mon Sep 17 00:00:00 2001 From: Hal <13111745+loonghao@users.noreply.github.com> Date: Tue, 13 Oct 2026 02:59:20 -0700 Subject: [PATCH 6/6] ci: reject Pester discovery failures and empty runs --- .github/workflows/examples-msvc.yml | 2 +- scripts/tests/msvc-spectre.Tests.ps1 | 34 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/.github/workflows/examples-msvc.yml b/.github/workflows/examples-msvc.yml index eef7a63e3..03db1bd80 100644 --- a/.github/workflows/examples-msvc.yml +++ b/.github/workflows/examples-msvc.yml @@ -54,7 +54,7 @@ jobs: run: | Import-Module Pester -MinimumVersion 5.0 -ErrorAction Stop $result = Invoke-Pester -Path scripts/tests/msvc-spectre.Tests.ps1,scripts/tests/aionui-exit.Tests.ps1 -Output Detailed -PassThru - if ($result.FailedCount -gt 0) { exit 1 } + if ($result.Result -ne 'Passed' -or $result.PassedCount -eq 0) { exit 1 } exit 0 build-vx: diff --git a/scripts/tests/msvc-spectre.Tests.ps1 b/scripts/tests/msvc-spectre.Tests.ps1 index 431266803..dfe428f66 100644 --- a/scripts/tests/msvc-spectre.Tests.ps1 +++ b/scripts/tests/msvc-spectre.Tests.ps1 @@ -192,3 +192,37 @@ Describe 'Workflow consumer compatibility' { $workflow | Should -Match 'npm_package_config_node_gyp_msvs_version=\$\(\$instance.installationPath\)' } } + +Describe 'Workflow Pester result gate' -Skip:(-not $IsWindows) { + It 'returns for ' -TestCases @( + @{ Name = 'passing tests'; ExpectedCode = 0; Content = "Describe 'fixture' { It 'passes' { 1 | Should -Be 1 } }" }, + @{ Name = 'discovery failure'; ExpectedCode = 1; Content = "throw 'intentional discovery failure'" }, + @{ Name = 'BeforeAll failure'; ExpectedCode = 1; Content = "Describe 'fixture' { BeforeAll { throw 'intentional setup failure' }; It 'cannot run' { 1 | Should -Be 1 } }" }, + @{ Name = 'assertion failure'; ExpectedCode = 1; Content = "Describe 'fixture' { It 'fails' { 1 | Should -Be 2 } }" }, + @{ Name = 'zero tests'; ExpectedCode = 1; Content = "Describe 'empty fixture' {}" }, + @{ Name = 'only skipped tests'; ExpectedCode = 1; Content = "Describe 'fixture' { It 'is skipped' -Skip { 1 | Should -Be 1 } }" } + ) { + param($Name, $ExpectedCode, $Content) + $workflow = Get-Content (Join-Path $PSScriptRoot '../../.github/workflows/examples-msvc.yml') -Raw + $gate = [regex]::Match($workflow, '(?ms)^ Import-Module Pester\b.*?^ exit 0\r?$') + $gate.Success | Should -BeTrue + # Exercise the actual workflow gate in a child process, including discovery + # and container failures which are not represented by FailedCount alone. + $runnerContent = $gate.Value -replace '(?m)^ ', '' + $originalPaths = '-Path scripts/tests/msvc-spectre.Tests.ps1,scripts/tests/aionui-exit.Tests.ps1' + $runnerContent | Should -Match ([regex]::Escape($originalPaths)) + $runnerContent = $runnerContent.Replace($originalPaths, '-Path $args[0]') + $fixture = Join-Path $TestDrive ([guid]::NewGuid().ToString()) + New-Item -ItemType Directory -Path $fixture | Out-Null + $runner = Join-Path $fixture 'run-gate.ps1' + $testFile = Join-Path $fixture 'fixture.Tests.ps1' + $runnerContent | Set-Content $runner + $Content | Set-Content $testFile + $PSNativeCommandUseErrorActionPreference = $false + + & (Join-Path $PSHOME 'pwsh.exe') -NoProfile -File $runner $testFile 2>&1 | + Set-Content (Join-Path $fixture 'gate.log') + + $LASTEXITCODE | Should -Be $ExpectedCode + } +}