Skip to content

Commit 6110e2a

Browse files
committed
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.
1 parent 43aff0e commit 6110e2a

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

.github/workflows/swift_package_test.yml

+19-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ on:
3737
default: ""
3838
windows_build_command:
3939
type: string
40-
description: "Windows Command Prompt command to build and test the package"
40+
description: |
41+
Windows Command Prompt command to build and test the package.
42+
Note that Powershell does not automatically exit if a subcommand fails. The Invoke-Program utility is available to propagate non-zero exit codes.
43+
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`.
4144
default: "swift test"
4245
linux_env_vars:
4346
description: "List of environment variables"
@@ -84,7 +87,7 @@ jobs:
8487
strategy:
8588
fail-fast: false
8689
matrix:
87-
swift_version: ['5.9', '5.10', '6.0', 'nightly', 'nightly-6.0']
90+
swift_version: ['5.9', '6.0', 'nightly', 'nightly-6.0']
8891
exclude:
8992
- ${{ fromJson(inputs.windows_exclude_swift_versions) }}
9093
steps:
@@ -103,15 +106,23 @@ jobs:
103106
- name: Create test script
104107
run: |
105108
mkdir $env:TEMP\test-script
106-
echo 'Set-PSDebug -Trace 1' >> $env:TEMP\test-script\run.ps1
107-
echo '$ErrorActionPreference = "Stop"' >> $env:TEMP\test-script\run.ps1
108-
echo 'swift --version' >> $env:TEMP\test-script\run.ps1
109-
echo 'swift test --version' >> $env:TEMP\test-script\run.ps1
110-
echo 'cd C:\source\' >> $env:TEMP\test-script\run.ps1
111109
echo @'
110+
Set-PSDebug -Trace 1
111+
112+
# Run the command following `Invoke-Program`.
113+
# If that command returns a non-zero exit code, return the same exit code from this script.
114+
function Invoke-Program($Executable) {
115+
& $Executable @args
116+
if ($LastExitCode -ne 0) {
117+
exit $LastExitCode
118+
}
119+
}
120+
Invoke-Program swift --version
121+
Invoke-Program swift test --version
122+
Invoke-Program cd C:\source\
112123
${{ inputs.windows_pre_build_command }}
124+
Invoke-Program ${{ inputs.windows_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }}
113125
'@ >> $env:TEMP\test-script\run.ps1
114-
echo '${{ inputs.windows_build_command }} ${{ (contains(matrix.swift_version, 'nightly') && inputs.swift_nightly_flags) || inputs.swift_flags }}' >> $env:TEMP\test-script\run.ps1
115126
- name: Build / Test
116127
timeout-minutes: 60
117128
run: |

0 commit comments

Comments
 (0)