Skip to content

Commit

Permalink
Skip examples/links if files not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwthompson committed Feb 15, 2024
1 parent 2ec1d29 commit 4876cdd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
34 changes: 24 additions & 10 deletions openff/toolkit/_tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Test that the examples in the repo run without errors.
"""

import os
import pathlib
import re
import subprocess
Expand Down Expand Up @@ -38,31 +37,36 @@ def run_script_str(script_str):
"""
with tempfile.TemporaryDirectory() as tmp_dir:
temp_file_path = os.path.join(tmp_dir, "temp.py")
temp_file_path = (pathlib.Path(tmp_dir) / "temp.py").as_posix()
# Create temporary python script.
with open(temp_file_path, "w") as f:
f.write(script_str)
# Run the Python script.
try:
run_script_file(temp_file_path)
except: # noqa
except Exception as error:
script_str = textwrap.indent(script_str, " ")
raise Exception(f"The following script failed:\n{script_str}")
raise Exception(f"The following script failed:\n{script_str}") from error


def find_example_scripts():
def find_example_scripts() -> list[str]:
"""Find all Python scripts, excluding Jupyter notebooks, in the examples folder.
Returns
-------
example_file_paths : list[str]
List of full paths to python scripts to execute.
"""
examples_dir_path = ROOT_DIR_PATH.joinpath("examples")
if "site-packages" in __file__:
# This test file is being collected from the installed package, which
# does not provide the examples folder in the same location
return list()

examples_dir_path = pathlib.Path(__file__).parents[3] / "examples"

# Examples that require RDKit
rdkit_examples = {
examples_dir_path.joinpath("conformer_energies/conformer_energies.py"),
examples_dir_path / "conformer_energies/conformer_energies.py",
}

example_file_paths = []
Expand All @@ -75,17 +79,27 @@ def find_example_scripts():
return example_file_paths


def find_readme_examples():
def find_readme_examples() -> list[str]:
"""Yield the Python scripts in the main README.md file.
Returns
-------
readme_examples : list[str]
The list of Python scripts included in the README.md files.
"""
readme_path = ROOT_DIR_PATH.joinpath("README.md")
with open(readme_path, "r") as f:
if "site-packages" in __file__:
# This test file is being collected from the installed package, which
# does not provide the README file.
# Note that there will likely be a mis-bundled file
# $CONDA_PREFIX/lib/python3.x/site-packages/README.md, but this is not
# the toolkit's README file!
return list()

readme_file_path = pathlib.Path(__file__).parents[3] / "README.md"

with open(readme_file_path, "r") as f:
readme_content = f.read()

return re.findall("```python(.*?)```", readme_content, flags=re.DOTALL)


Expand Down
22 changes: 14 additions & 8 deletions openff/toolkit/_tests/test_links.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import os
import pathlib
import re
from urllib.request import Request, urlopen

import pytest

ROOT_DIR_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "..", "..", ".."
)


def find_readme_links():
def find_readme_links() -> list[str]:
"""Yield all the links in the main README.md file.
Returns
-------
readme_examples : list[str]
The list of links included in the README.md file.
"""
readme_file_path = os.path.join(ROOT_DIR_PATH, "README.md")
with open(readme_file_path, "r") as f:
if "site-packages" in __file__:
# This test file is being collected from the installed package, which
# does not provide the README file.
# Note that there will likely be a mis-bundled file
# $CONDA_PREFIX/lib/python3.x/site-packages/README.md, but this is not
# the toolkit's README file!
return list()

readme_file_path = pathlib.Path(__file__).parents[3] / "README.md"

with open(readme_file_path.as_posix(), "r") as f:
readme_content = f.read()

return re.findall("http[s]?://(?:[0-9a-zA-Z]|[-/.%:_])+", readme_content)


Expand Down

0 comments on commit 4876cdd

Please sign in to comment.