-
Notifications
You must be signed in to change notification settings - Fork 5
CI: test that runs pdf pack examples in CI #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1e2632f
856596e
c379a11
522c561
ac32634
3840f07
f02c948
bce5b86
6d2764e
0ef05db
4d70e15
f1c2ac2
5b27183
d0e17cf
89e89bb
f6ad035
9408042
b2fd64a
666707d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,9 @@ | |
| print("The Ni example refines instrument parameters\n") | ||
| print("The instrument parameters are necessary to run this fit\n") | ||
| print("Please run the Ni example first\n") | ||
| print("Setting Q_damp and Q_broad to refined values\n") | ||
| QDAMP_I = 0.045298 | ||
| QBROAD_I = 0.016809 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pytest was failing on CI (but not locally) without this. The reason why is because
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we put this in a conditional, so it runs the old way but does it the new way if it can't find the other results?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that is what it is doing here. |
||
|
|
||
| # If we want to run using multiprocessors, we can switch this to 'True'. | ||
| # This requires that the 'psutil' python package installed. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Add CI for testing examples of the PDF pack. | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from conftest import TESTS_DIR | ||
|
|
||
|
|
||
| def test_example_dirs_have_tests(examples_tmpdir): | ||
| """Verify that each example directory has a corresponding test | ||
| file.""" | ||
| example_dirs = [d for d in examples_tmpdir.iterdir() if d.is_dir()] | ||
| missing_tests = [] | ||
| for example_dir in example_dirs: | ||
| # Test file expected in TESTS_DIR, named e.g. test_<example_dir>.py | ||
| test_file = TESTS_DIR / f"test_{example_dir.name}.py" | ||
| if not test_file.exists(): | ||
| missing_tests.append(example_dir.name) | ||
| assert not missing_tests, ( | ||
| f"The following example dirs have no test file: {missing_tests}.", | ||
| "Test file must be named test_<example_dir>.py.", | ||
| ) | ||
|
|
||
|
|
||
| def test_examples_tmpdir_exists(examples_tmpdir): | ||
| """Ensure that the examples temporary directory has been created.""" | ||
| # Check the directory itself exists | ||
| assert ( | ||
| examples_tmpdir.exists() and examples_tmpdir.is_dir() | ||
| ), f"Temporary examples directory does not exist: {examples_tmpdir}" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import importlib.util | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def load_module_from_path(path: Path): | ||
| """Load a module given an absolute Path.""" | ||
| spec = importlib.util.spec_from_file_location(path.stem, path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def run_cmi_script(script_path: Path): | ||
| """Run a script with a main() function.""" | ||
| old_cwd = os.getcwd() | ||
| os.chdir(script_path.parent) | ||
| try: | ||
| module = load_module_from_path(script_path) | ||
| module.main() | ||
| finally: | ||
| os.chdir(old_cwd) | ||
|
|
||
|
|
||
| def run_all_scripts_for_given_example(examples_tmpdir, test_file_path): | ||
| """Run all Python scripts in the chapter corresponding to this test | ||
| file. | ||
|
|
||
| Only scripts that define a main() function will be executed. | ||
| """ | ||
| chapter_dir_name = Path(test_file_path).stem.replace("test_", "") | ||
| chapter_dir = examples_tmpdir / chapter_dir_name | ||
| assert chapter_dir.exists(), f"Chapter dir does not exist: {chapter_dir}" | ||
|
|
||
| # Recursively find all .py scripts | ||
| scripts = list(chapter_dir.rglob("*.py")) | ||
| for script_path in scripts: | ||
| module = load_module_from_path(script_path) | ||
| if hasattr(module, "main"): | ||
| run_cmi_script(script_path) | ||
| else: | ||
| # automatically skip helper or non-example scripts | ||
| print(f"Skipping file without main(): {script_path.name}") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from helpers import run_all_scripts_for_given_example | ||
|
|
||
|
|
||
| def test_scripts(examples_tmpdir): | ||
| run_all_scripts_for_given_example(examples_tmpdir, __file__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires psutil to be installed or it throws an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to do this either. Is there a reason not to install psutil? or make it conditional on whether psutil is installed? We don't want to change the behavior of the examples just so the CI will run.