From c1fde293bbfad277a1985a1bab562b7f81a6af25 Mon Sep 17 00:00:00 2001
From: Christoph Lehmann <christoph.lehmann@ufz.de>
Date: Tue, 25 Apr 2023 12:16:14 +0200
Subject: [PATCH] [scr/ci] Work around xvfb-run errors

---
 scripts/ci/extends/template-build-linux.yml | 30 ++++++++++++++++++---
 scripts/ci/jobs/jupyter.yml                 | 16 ++++++++++-
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/scripts/ci/extends/template-build-linux.yml b/scripts/ci/extends/template-build-linux.yml
index b1e24891038..a029ee7d95b 100644
--- a/scripts/ci/extends/template-build-linux.yml
+++ b/scripts/ci/extends/template-build-linux.yml
@@ -18,6 +18,25 @@
     # Activate .venv
     - test -f $build_dir_full/.venv/bin/activate && source $build_dir_full/.venv/bin/activate
     - |
+      function maybe_run_with_xvfb()
+      {
+          if [[ "$OSTYPE" == "darwin"* ]]; then
+            "$@"
+          elif command -v xvfb-run &> /dev/null ; then
+            local ctest_status_file="$build_dir_full/ctest_status"
+            rm -f "$ctest_status_file"
+            # status file and sh -c to workaround xvfb-run problems:
+            # /usr/bin/xvfb-run: line 186: kill: (<PID>) - No such process"
+            xvfb-run -a \
+                sh -c 'statf="$1"; shift; "$@"; echo "$?" >"$statf"' \
+                -- "$ctest_status_file" "$@" \
+                || true
+            [ 0 = "`cat "$ctest_status_file"`" ]
+          else
+            "$@"
+          fi
+      }
+
       if [[ -z "$TARGETS" ]]; then
 
         if [ "$BUILD_PACKAGE" = false ]; then
@@ -41,11 +60,8 @@
             preset_postfix="-large"
           fi
 
-          xvfb_run_cmd=""
           if [[ "$OSTYPE" == "darwin"* ]]; then
             alias date=gdate
-          elif command -v xvfb-run &> /dev/null ; then
-            xvfb_run_cmd="xvfb-run -a"
           fi
 
           ctest_arguments=""
@@ -68,7 +84,13 @@
           if [[ "$CI_MERGE_REQUEST_LABELS" =~ .*unit_tests.* ]]; then
             echo "Skipping ctests because of unit_tests-label."
           else
-            eval ${xvfb_run_cmd} ctest -M Experimental --group ${ctest_group} ${regex_argument} --test-dir ${build_dir_full} -T Start -T Test -T Submit ${ctest_arguments} --output-junit Tests/ctest.xml --stop-time `date -d "today + ${ctest_timeout} minutes" +'%H:%M:%S'`
+            maybe_run_with_xvfb ctest \
+                -M Experimental --group "${ctest_group}" "${regex_argument}" \
+                --test-dir "${build_dir_full}" -T Start -T Test -T Submit \
+                ${ctest_arguments} \
+                --output-junit Tests/ctest.xml \
+                --stop-time "`date -d "today + ${ctest_timeout} minutes" +'%H:%M:%S'`" \
+                --no-tests=error
           fi
         fi
 
diff --git a/scripts/ci/jobs/jupyter.yml b/scripts/ci/jobs/jupyter.yml
index d72753b0267..d689cdb6e1c 100644
--- a/scripts/ci/jobs/jupyter.yml
+++ b/scripts/ci/jobs/jupyter.yml
@@ -11,7 +11,21 @@ test notebooks via wheel:
     # TODO:
     #  - better timeout
     #  - run in parallel
-    - find . -type f -iname '*.ipynb' | grep -vP '\.ipynb_checkpoints|\.ci-skip.ipynb$|_out|.venv|PhaseField' | xargs xvfb-run -a python Notebooks/testrunner.py --out _out
+    - |
+      status_file="`mktemp --tmpdir`"
+      echo 0 >"$status_file"  # success if no Notebooks are found
+      # status file and sh -c to workaround xvfb-run problems:
+      # /usr/bin/xvfb-run: line 186: kill: (<PID>) - No such process"
+      find . -type f -iname '*.ipynb' \
+          | grep -vP '\.ipynb_checkpoints|\.ci-skip\.ipynb$|_out|\.venv|PhaseField' \
+          | xargs xvfb-run -a \
+              sh -c 'statf="$1"; shift; "$@" || echo "$?" >"$statf"' \
+              -- "$status_file" \
+              python Notebooks/testrunner.py --out _out \
+          || true
+      status="`cat "$status_file"`"
+      rm "$status_file"
+      [ 0 = "$status" ]
   artifacts:
     when: always
     paths:
-- 
GitLab