Skip to content
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

Tests framework for backend #49

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/build-backend-docker.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: Build Backend Docker Image

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build-backend-docker:
Expand Down
18 changes: 14 additions & 4 deletions .github/workflows/mypy.yaml → .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: Mypy & pre-commit
name: CI checks

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
backend:
Expand All @@ -17,10 +23,14 @@ jobs:
pip install -r backend/requirements-test.txt
- name: Run MyPy
run: |
python -m mypy --strict backend
python -m mypy backend
- name: Run pre-commit
run: |
pre-commit run --files backend/*
- name: Run tests
working-directory: backend
run: |
pytest .
frontend:
runs-on: ubuntu-latest
steps:
Expand All @@ -35,7 +45,7 @@ jobs:
pip install -r frontend/requirements-test.txt
- name: Run MyPy
run: |
python -m mypy --strict frontend
python -m mypy frontend
- name: Run pre-commit
run: |
pre-commit run --files frontend/*
8 changes: 7 additions & 1 deletion .github/workflows/ruff_linter.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: Ruff

on: [push, pull_request]
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
lint:
Expand Down
4 changes: 4 additions & 0 deletions backend/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
omit =
tests/*
*/__init__.py
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Coverage files
.coverage
coverage.xml
25 changes: 17 additions & 8 deletions backend/build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ def clone_repo(url: str, folder_name: str, commit_hash: Optional[str] = None) ->
def build_or_docs() -> None:
logging.debug('Starting OR docs build...')

# if sphinx-build not in PATH
if not shutil.which('sphinx-build'):
logging.debug('sphinx-build not found in PATH. Exiting.')
sys.exit(1)

os.chdir(os.path.join(cur_dir, 'OpenROAD/docs'))
subprocess.run('make html', shell=True, capture_output=True)

Expand Down Expand Up @@ -365,14 +370,18 @@ def get_yosys_rtdocs() -> None:
]
purge_folders(folder_paths=docs_paths)

os.makedirs('data/markdown/manpages', exist_ok=True)
os.makedirs('data/markdown/OR_docs', exist_ok=True)
os.makedirs('data/markdown/OR_docs/installation', exist_ok=True)
os.makedirs('data/markdown/OR_docs/tools', exist_ok=True)
os.makedirs('data/markdown/ORFS_docs', exist_ok=True)
os.makedirs('data/markdown/ORFS_docs/installation', exist_ok=True)
os.makedirs('data/pdf/OpenSTA', exist_ok=True)
os.makedirs('data/rtdocs', exist_ok=True)
new_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',
]
for path in new_paths:
os.makedirs(path, exist_ok=True)

get_yosys_rtdocs()
get_opensta_docs()
Expand Down
9 changes: 9 additions & 0 deletions backend/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[pytest]
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
addopts = --cov . --cov-report term --cov-report xml:coverage.xml
testpaths = tests
5 changes: 4 additions & 1 deletion backend/requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ sphinxcontrib-mermaid==0.9.2
sphinx-book-theme==1.1.3
sphinx-copybutton==0.5.2
sphinx-external-toc==1.0.1
pypdf==4.2.0
pypdf==4.2.0
pytest==8.3.0
pytest-cov==5.0.0
pytest-logger==1.1.1
115 changes: 115 additions & 0 deletions backend/tests/test_build_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import os
import sys
import logging
from dotenv import load_dotenv

# TODO: Fix this using setup.py/pyproject.toml file in backend dir.
# TODO: Remove E402 from ruff.toml.
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_rtdocs,
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_rtdocs()
assert os.path.exists('data/rtdocs')

count = sum(len(files) for _, _, files in os.walk(f"data/rtdocs/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'])
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ warn_return_any = True
warn_unused_configs = True
ignore_missing_imports = True
disable_error_code = call-arg
exclude = ^tests/

[mypy-tests.*]
ignore_errors = True

[mypy-transformers.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ target-version = "py312"
[lint]
select = ["E4", "E7", "E9","E301","E304","E305","E401","E223","E224","E242", "F","N"]
extend-select = ["D203", "D204"]
ignore = []
ignore = ["E402"]
preview = true

# Allow fix for all enabled rules (when `--fix` is provided).
Expand Down