From 561da3bdbebb0405ffc978e8f93d01d9a4564d3d Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Wed, 5 Apr 2023 02:46:21 +0200 Subject: [PATCH] Fix macOS CI by running tests in graphical env (#985) --- .github/workflows/macos-ci.yml | 2 +- .../macos/scripts/run-in-macos-gui-env.sh | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100755 platform/macos/scripts/run-in-macos-gui-env.sh diff --git a/.github/workflows/macos-ci.yml b/.github/workflows/macos-ci.yml index ce9e214b4..c47d9c934 100644 --- a/.github/workflows/macos-ci.yml +++ b/.github/workflows/macos-ci.yml @@ -118,7 +118,7 @@ jobs: - name: Run macOS Objective-C tests run: | - make macos-test + ../macos/scripts/run-in-macos-gui-env.sh "make macos-test" - name: npm install # C++ tests are run with root as working directory run: npm ci --ignore-scripts diff --git a/platform/macos/scripts/run-in-macos-gui-env.sh b/platform/macos/scripts/run-in-macos-gui-env.sh new file mode 100755 index 000000000..a0226a198 --- /dev/null +++ b/platform/macos/scripts/run-in-macos-gui-env.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# This script allows you to run commands from a graphical context +# on macOS. This may be needed to run tests that depend on testmanagerd. +# See: +# https://stackoverflow.com/questions/67688130/run-macos-test-cases-on-the-jenkins-pipeline/71417206#71417206 + +# This script writes out a script that runs a command, then runs it with Terminal.app. +# Next, it prints the output from the script and exits with the same +# exit code as the command. See also this blog post: +# https://aahlenst.dev/blog/accessing-the-macos-gui-in-automation-contexts/#continuous-integration-with-ssh + +set -eo pipefail + +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi +command="$1" + +run_command_file="$(mktemp)" + +output_file="$(mktemp)" +exit_code_file="$(mktemp)" + +cat << EOF > "$run_command_file" +cd "$(pwd)" + +$command 2>&1 | tee "$output_file" +exit_code=$? +echo $exit_code > "$exit_code_file" + +osascript -e 'tell application "Terminal" to quit' & +EOF + +chmod +x "$run_command_file" +open --wait-apps --new --fresh -a Terminal.app "$run_command_file" + +cat "$output_file" + +exit $(cat "$exit_code_file")