From 6110e2acf2284b225245c0fa695ec2b13bb01647 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Fri, 25 Oct 2024 21:38:45 -0700 Subject: [PATCH] Propagate errors from the Windows run script Powershell does not automatically exit if a subcommand fails, ie. it behaves similar to a Bash script without `set -e`. This means that `swift test` could fail and `run.ps1` would still return exit code 0, indicating success. Add an `Invoke-Program` utility to `run.ps1` that propagates error codes. --- .github/workflows/swift_package_test.yml | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/.github/workflows/swift_package_test.yml b/.github/workflows/swift_package_test.yml index 71f81d3..d291892 100644 --- a/.github/workflows/swift_package_test.yml +++ b/.github/workflows/swift_package_test.yml @@ -37,7 +37,10 @@ on: default: "" windows_build_command: type: string - description: "Windows Command Prompt command to build and test the package" + description: | + Windows Command Prompt command to build and test the package. + Note that Powershell does not automatically exit if a subcommand fails. The Invoke-Program utility is available to propagate non-zero exit codes. + It is strongly encouraged to run all command using `Invoke-Program` unless you want to continue on error eg. `Invoke-Program git apply patch.diff` instead of `git apply patch.diff`. default: "swift test" linux_env_vars: description: "List of environment variables" @@ -84,7 +87,7 @@ jobs: strategy: fail-fast: false matrix: - swift_version: ['5.9', '5.10', '6.0', 'nightly', 'nightly-6.0'] + swift_version: ['5.9', '6.0', 'nightly', 'nightly-6.0'] exclude: - ${{ fromJson(inputs.windows_exclude_swift_versions) }} steps: @@ -103,15 +106,23 @@ jobs: - name: Create test script run: | mkdir $env:TEMP\test-script - echo 'Set-PSDebug -Trace 1' >> $env:TEMP\test-script\run.ps1 - echo '$ErrorActionPreference = "Stop"' >> $env:TEMP\test-script\run.ps1 - echo 'swift --version' >> $env:TEMP\test-script\run.ps1 - echo 'swift test --version' >> $env:TEMP\test-script\run.ps1 - echo 'cd C:\source\' >> $env:TEMP\test-script\run.ps1 echo @' + Set-PSDebug -Trace 1 + + # Run the command following `Invoke-Program`. + # If that command returns a non-zero exit code, return the same exit code from this script. + function Invoke-Program($Executable) { + & $Executable @args + if ($LastExitCode -ne 0) { + exit $LastExitCode + } + } + Invoke-Program swift --version + Invoke-Program swift test --version + Invoke-Program cd C:\source\ ${{ inputs.windows_pre_build_command }} + Invoke-Program ${{ inputs.windows_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }} '@ >> $env:TEMP\test-script\run.ps1 - echo '${{ inputs.windows_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }}' >> $env:TEMP\test-script\run.ps1 - name: Build / Test timeout-minutes: 60 run: |