diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 106c2bc80..5726a6c8b 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -37,3 +37,16 @@ jobs: - name: Run tests run: | python -m pytest tests + + validate-examples: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + - name: Install dependencies + run: | + python -m pip install -e ".[tests]" + - name: Validate examples + working-directory: ./examples + run: | + python -m pytest validate_examples.py diff --git a/examples/validate_examples.py b/examples/validate_examples.py new file mode 100644 index 000000000..cc4a2c237 --- /dev/null +++ b/examples/validate_examples.py @@ -0,0 +1,43 @@ +import os + +EXAMPLES_DIR = "." +REQUIRED_FILES = ["notebook.ipynb", "application.py", "README.md", "digraph.png"] + +FILTERLIST = [] + + +def should_validate(directory: str) -> bool: + """Return True if the given directory is an example directory.""" + return ( + os.path.isdir(os.path.join(EXAMPLES_DIR, directory)) + and not directory.startswith(".") + and not directory.startswith("_") + and directory not in FILTERLIST + ) + + +def get_directories(base_path: str) -> list[str]: + """Return a list of directories under the given base_path.""" + return [d for d in os.listdir(base_path) if should_validate(d)] + + +def check_files_exist(directory, files): + """Check if each file in 'files' exists in 'directory'.""" + missing_files = [] + for file in files: + if not os.path.exists(os.path.join(EXAMPLES_DIR, directory, file)): + missing_files.append(file) + return missing_files + + +# Use pytest_generate_tests to dynamically parameterize the fixture +def pytest_generate_tests(metafunc): + # if "directory" in metafunc.fixturenames: + directories = get_directories(EXAMPLES_DIR) + metafunc.parametrize("directory", directories, scope="module") + + +def test_directory_contents(directory): + required_files = ["notebook.ipynb", "application.py", "README.md", "digraph.png"] + missing_files = check_files_exist(directory, required_files) + assert not missing_files, f"Missing files in '{directory}': {', '.join(missing_files)}"