Skip to content

Commit 315c9ac

Browse files
committed
Very ugly attempt to fix issues with pickling functions for test_doc_examples.py in github CI.
Part 1: run example code by writing it to a file and loading it as a module with importlib instead of using exec. Part 2: In the example notebook, add PWD to the PYTHONPATH in each remote_info object or dict, counting on the fact that the test framework will chdir to tmp_path, where it wrote the example code as a file (see Part 1) before the example is run, so PWD is the right place for the extracted code's module. Pickling works locally, but remote jobs are using the global wfl instead of the one in the directory where pytest is being run from. The remoteinfo_env fixture deals with this by modifying the PYTHONPATH in the remote_info env_vars field for other remote run tests, but adding that to the example would make it even uglier than these modifications already do. Should the RemoteInfo constructor itself be able to add such information (e.g. extra bits for the PYTHONPATH env var) from an env var (maybe something like PYTEST_REMOTE_INFO_EXTRAS, or a more PYTHONPATH specific var name), which pytest can then use without polluting the example itself?
1 parent cd52fac commit 315c9ac

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/source/examples.daisy_chain_mlip_fitting.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"from wfl.utils.configs import atomization_energy\n",
8181
"from wfl.fit import error\n",
8282
"import wfl.map\n",
83+
"from pathlib import Path\n",
8384
"\n",
8485
"from expyre.resources import Resources"
8586
]
@@ -244,7 +245,8 @@
244245
" \"check_interval\": 5,\n",
245246
" \"num_inputs_per_queued_job\" :20,\n",
246247
" \"pre_cmds\": ['echo \"-------------------------------------\"', \"which wfl\"],\n",
247-
" \"post_cmds\": ['echo \"_______________________________\"', \"which wfl\"]\n",
248+
" \"post_cmds\": ['echo \"_______________________________\"', \"which wfl\"],\n",
249+
" \"env_vars\": [f'PYTHONPATH={Path.cwd()}:$PYTHONPATH']\n",
248250
"}\n"
249251
]
250252
},
@@ -464,6 +466,7 @@
464466
" \"num_cores\" : 2,\n",
465467
" \"partitions\" : \"stndard\"}, \n",
466468
" \"check_interval\": 5, \n",
469+
" \"env_vars\": [f'PYTHONPATH={Path.cwd()}:$PYTHONPATH']\n",
467470
"}\n"
468471
]
469472
},
@@ -548,7 +551,8 @@
548551
" job_name = \"gap-eval\",\n",
549552
" resources = resources,\n",
550553
" check_interval=10, \n",
551-
" input_files=[\"gap.xml*\"])\n"
554+
" input_files=[\"gap.xml*\"],\n",
555+
" env_vars=[f'PYTHONPATH={Path.cwd()}:$PYTHONPATH'])\n",
552556
]
553557
},
554558
{

tests/test_doc_examples.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ def test_example(tmp_path, nb_file, idx_execute, monkeypatch, needs_expyre, expy
3939

4040
monkeypatch.chdir(tmp_path)
4141
try:
42-
exec(code, globals())
42+
## exec(code, globals())
43+
run_test_mod_name = f"run_test_{nb_file.replace('.', '_')}"
44+
import sys, importlib
45+
with open(run_test_mod_name + ".py", "w") as fout:
46+
fout.write(code)
47+
sys.path.append(str(tmp_path))
48+
run_test_mod = importlib.import_module(run_test_mod_name)
49+
sys.path.pop()
50+
4351
except Exception as exc:
4452
import traceback, re
4553
tb_str = traceback.format_exc()

0 commit comments

Comments
 (0)