From 1f946b87c65acf8dcd0627f006b46eb3114c7257 Mon Sep 17 00:00:00 2001
From: u214892 <u214892@sbb.ch>
Date: Thu, 4 Jul 2019 10:38:28 +0200
Subject: [PATCH] run examples from python instead of shell

---
 .gitlab-ci.yml                 |  3 ++-
 benchmarks/run_all_examples.py | 22 ++++++++++++++++++++++
 tox.ini                        |  2 +-
 3 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 benchmarks/run_all_examples.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index dc0bdd7..6175ad7 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -26,7 +26,8 @@ tests:
         - apt update
         - apt install -y libgl1-mesa-glx xvfb graphviz xdg-utils libcairo2-dev libjpeg-dev libgif-dev
         - pip install tox
-        - xvfb-run tox -e benchmarks,profiling -v --recreate
+        - xvfb-run tox -v -e examples -v --recreate
+        - xvfb-run tox -v -e benchmarks,profiling -v --recreate
         - xvfb-run tox -v --recreate
 
 build_and_deploy_docs:
diff --git a/benchmarks/run_all_examples.py b/benchmarks/run_all_examples.py
new file mode 100644
index 0000000..8cc940a
--- /dev/null
+++ b/benchmarks/run_all_examples.py
@@ -0,0 +1,22 @@
+import runpy
+import sys
+from io import StringIO
+
+import importlib_resources
+import pkg_resources
+from importlib_resources import path
+
+from benchmarks.benchmark_utils import swap_attr
+
+for entry in [entry for entry in importlib_resources.contents('examples') if
+              not pkg_resources.resource_isdir('examples', entry)
+              and entry.endswith(".py")
+              and '__init__' not in entry
+              and 'demo.py' not in entry
+              ]:
+    with path('examples', entry) as file_in:
+        print("*****************************************************************")
+        print("Running {}".format(entry))
+        print("*****************************************************************")
+        with swap_attr(sys, "stdin", StringIO("q")):
+            runpy.run_path(file_in, run_name="__main__")
diff --git a/tox.ini b/tox.ini
index 4d214b1..3ff9c89 100644
--- a/tox.ini
+++ b/tox.ini
@@ -97,7 +97,7 @@ commands =
 ; run examples from subfolder to ensure that resources are accessed via resources and not via relative paths
     sh -c 'mkdir -p {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5'
 ; pipe echo into python since some examples expect input to close the window after the example is run
-    sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 && ls {toxinidir}/examples/*.py | xargs -I{} -n 1 sh -c "echo -e \"\n====== Running {} ========\n\"; echo "q" | python {}"'
+    sh -c 'cd {envtmpdir}/c236d3c240d61a0969d4cb59e2180ce5 &&  python {toxinidir}/benchmarks/run_all_examples.py'
 
 [testenv:notebooks]
 basepython = python
-- 
GitLab