diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 31ff92d0..3ded42b0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -19,6 +19,10 @@ jobs: run: | make format make check + - name: Run test + working-directory: backend + run: | + make test - name: Populate environment variables run: | cp backend/.env.example backend/.env diff --git a/backend/.coveragerc b/backend/.coveragerc new file mode 100644 index 00000000..7e382fdf --- /dev/null +++ b/backend/.coveragerc @@ -0,0 +1,4 @@ +[run] +omit = + tests/* + */__init__.py \ No newline at end of file diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 00000000..0177ff3a --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1,3 @@ +# Coverage files +.coverage +coverage.xml \ No newline at end of file diff --git a/backend/Makefile b/backend/Makefile index 72878508..a104ec3b 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -15,3 +15,7 @@ format: check: @. .venv/bin/activate && \ ruff check --fix + +test: + @. .venv/bin/activate && \ + pytest diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 50ad191d..9328e330 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -90,3 +90,12 @@ skip-magic-trailing-comma = false line-ending = "auto" docstring-code-format = false docstring-code-line-length = "dynamic" + +[tool.pytest.ini_options] +python_files = "test_*.py" +python_functions = "test_*" +log_format = "%(asctime)s %(levelname)s %(message)s" +log_date_format = "%Y-%m-%d %H:%M:%S" +log_level = "NOTSET" +log_cli = true +testpaths = "tests" diff --git a/backend/requirements-test.txt b/backend/requirements-test.txt index 44d6faee..6cfd4cf0 100644 --- a/backend/requirements-test.txt +++ b/backend/requirements-test.txt @@ -26,6 +26,7 @@ sphinx-book-theme==1.1.3 sphinx-copybutton==0.5.2 sphinx-external-toc==1.0.1 pypdf==4.2.0 -unstructured==0.15.1 nltk==3.9.1 huggingface_hub[cli]==0.24.6 +pytest==8.3.0 +pytest-logger==1.1.1 diff --git a/backend/tests/test_build_docs.py b/backend/tests/test_build_docs.py new file mode 100644 index 00000000..2b2842f3 --- /dev/null +++ b/backend/tests/test_build_docs.py @@ -0,0 +1,113 @@ +import os +import sys +import logging +from dotenv import load_dotenv + +backend_dir = os.path.join(os.path.dirname(__file__), '..') +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) +load_dotenv() + +from build_docs import ( + purge_folders, + get_yosys_docs_html, + get_opensta_docs, + clone_repo, + build_or_docs, + build_orfs_docs, +) + + +docs_paths = [ + 'data/markdown/manpages', + 'data/markdown/OR_docs', + 'data/markdown/OR_docs/installation', + 'data/markdown/OR_docs/tools', + 'data/markdown/ORFS_docs', + 'data/markdown/ORFS_docs/installation', + 'data/pdf/OpenSTA', + 'data/rtdocs', +] + + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +def test_setup(): + logger.info('Setting up...') + for path in docs_paths: + os.makedirs(path, exist_ok=True) + + +def test_purge_folders(): + os.chdir(backend_dir) + lst = ['test1', 'test2'] + purge_folders(lst) + + # Check if the folders are deleted + for folder in lst: + assert not os.path.exists(folder) + + +def test_get_yosys_rtdocs(): + os.chdir(backend_dir) + yosys_version='0.36' + yosys_docs_count=283 + get_yosys_docs_html() + assert os.path.exists('data/html') + + count = sum(len(files) for _, _, files in os.walk(f"data/html/yosys_docs/yosyshq.readthedocs.io/projects/yosys/en/{yosys_version}")) + assert count == yosys_docs_count, f"Expected {yosys_docs_count} files, got {count}" + + +# def test_get_opensta_docs(): +# os.chdir(backend_dir) +# get_opensta_docs() +# assert os.path.exists('data/pdf/OpenSTA/OpenSTA_docs.pdf') + + +# def test_clone_repo(): +# os.chdir(backend_dir) +# clone_repo( +# url = 'https://github.com/octocat/Hello-World.git', +# commit_hash='7fd1a60b01f91b314f59955a4e4d4e80d8edf11d', +# folder_name = 'Hello-World', +# ) +# assert os.path.exists('README') + + +# def test_build_or_docs(): +# os.chdir(backend_dir) +# or_docs_count = 55 + +# clone_repo( +# url='https://github.com/The-OpenROAD-Project/OpenROAD.git', +# commit_hash=os.getenv('OR_REPO_COMMIT', 'ffc5760f2df639cd184c40ceba253c7e02a006d5'), +# folder_name='OpenROAD', +# ) +# build_or_docs() +# count = sum(len(files) for _, _, files in os.walk('data/markdown/OR_docs')) +# assert count == or_docs_count, f"Expected {or_docs_count} files, got {count}" + + +# def test_build_orfs_docs(): +# os.chdir(backend_dir) +# orfs_docs_count = 27 + +# clone_repo( +# url='https://github.com/The-OpenROAD-Project/OpenROAD-flow-scripts.git', +# commit_hash=os.getenv('ORFS_REPO_COMMIT', 'b94834df01cb58915bc0e8dabf85a314fbd8fb9e'), +# folder_name='OpenROAD-flow-scripts', +# ) +# build_orfs_docs() +# count = sum(len(files) for _, _, files in os.walk('data/markdown/ORFS_docs')) +# logging.info(count) +# assert count == orfs_docs_count, f"Expected {orfs_docs_count} files, got {count}" + + +def test_teardown(): + os.chdir(backend_dir) + logger.info('Cleaning up...') + purge_folders(docs_paths) + purge_folders(['Hello-World']) + purge_folders(['OpenROAD']) + purge_folders(['OpenROAD-flow-scripts']) \ No newline at end of file