Skip to content

Commit

Permalink
[ASSET-18] Add dependency test (#19)
Browse files Browse the repository at this point in the history
* ASSET-18 Update the dependency management method

Signed-off-by: jormal <[email protected]>

* ASSET-18 Add tests

Signed-off-by: jormal <[email protected]>

---------

Signed-off-by: jormal <[email protected]>
  • Loading branch information
jormal authored Apr 22, 2024
1 parent 788b5ef commit 12f8c80
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
python-version: "3.12"

- name: Set up requirements
run: pip install -e ".[dev]"
run: pip install -e ".[test]"

- name: Test
run: pytest
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ $ source venv/bin/activate
#### 3. Install the dependencies

```bash
(venv) $ pip install -e ".[dev]"
(venv) $ pip install -e ".[all]"
```

### Contribution Guidelines
Expand Down
42 changes: 42 additions & 0 deletions libraries/utils/dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from re import search

REQUIREMENT_REFERENCE_REGEX = r"^\-r ([\w]+)\.txt$"


def read_requirements(key: str) -> list[str]:
"""Reads the requirements from the given type.
Args:
key: The key of the requirements.
Returns:
The list of requirements.
"""
with open(f"requirements/{key}.txt") as file:
req_lines = list(
filter(
lambda x: not x.startswith("#"),
file.read().splitlines(),
)
)
ref_keys = [
search(REQUIREMENT_REFERENCE_REGEX, req).group(1)
for req in filter(lambda x: x.startswith("-r"), req_lines)
]
ref_reqs = [
req
for reqs in [read_requirements(ref_type) for ref_type in ref_keys]
for req in reqs
]
reqs = list(filter(lambda x: not x.startswith("-r"), req_lines))
return ref_reqs + reqs


packages = ["libraries.models"]
"""List of packages to be used by the users."""

install_require_key = "essential"
"""The key of the essential requirements."""

extras_require_keys = ["all", "dev", "test"]
"""The keys of the extra requirements."""
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-r requirements/all.txt
4 changes: 4 additions & 0 deletions requirements/all.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Contains all requirements for the project.
-r essential.txt
-r dev.txt
-r test.txt
7 changes: 7 additions & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Specifies the required packages for developing the project.
-r essential.txt
CairoSVG==2.7.1
pillow==10.2.0
setuptools==69.5.1
svgpathtools==1.6.1
web3==6.14.0
2 changes: 2 additions & 0 deletions requirements/essential.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Specifies the essential requirements of the project.
pydantic==2.5.3
5 changes: 5 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Specifies the packages for testing the project.
-r essential.txt
-r dev.txt
pytest==7.4.4
pigar==2.1.4
25 changes: 11 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
from setuptools import setup

install_required = ["pydantic==2.5.3"]
from libraries.utils.dependency import (
packages,
read_requirements,
install_require_key,
extras_require_keys,
)

dev_required = [
"CairoSVG==2.7.1",
"pillow==10.2.0",
"pytest==7.4.4",
"svgpathtools==1.6.1",
"web3==6.14.0",
]
install_requires = read_requirements(install_require_key)
extras_require = {key: read_requirements(key) for key in extras_require_keys}

setup(
name="asset-info-v2",
version="1.0.0",
packages=[
"libraries.utils",
"libraries.models",
],
install_requires=install_required,
extras_require={"dev": dev_required},
packages=packages,
install_requires=install_requires,
extras_require=extras_require,
url="https://github.com/bifrost-platform/asset-info-v2",
license="",
author="Backend Team of Bifrost",
Expand Down
Empty file added tests/dependency/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions tests/dependency/test_dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from os import listdir

from libraries.utils.dependency import (
packages,
install_require_key,
read_requirements,
extras_require_keys,
)
from libraries.utils.file import PWD
from tests.utils.dependency_getter import get_dependencies


class TestDependencyOnSetupPy:
"""Tests the dependency on `setup.py`.
Attributes:
extra_paths: The paths for testing each extra requirement.
"""

extra_paths = {
"all": PWD,
"dev": PWD.joinpath("libraries"),
"test": PWD.joinpath("tests"),
}

def test_requirements_files_exist(self):
"""Test the existence of the requirements files."""
for key in [install_require_key] + extras_require_keys:
assert PWD.joinpath(f"requirements/{key}.txt").exists()

def test_unnecessary_requirements(self):
"""Test the unnecessary requirements in `requirements`."""
for file in listdir(PWD.joinpath("requirements")):
assert any(
file.startswith(key)
for key in [install_require_key] + extras_require_keys
)

def test_install_requires(self):
"""Test the `install_requires` in `setup.py`."""
install_deps = [
dep
for deps in [
get_dependencies(PWD.joinpath(package.replace(".", "/")))
for package in packages
]
for dep in deps
]
install_requires = read_requirements(install_require_key)
for dep in install_deps:
assert any(dep in require for require in install_requires)

def test_extras_require(self):
"""Test the `extras_require` in `setup.py`."""
for extra_key in extras_require_keys:
extra_requires = read_requirements(extra_key)
assert extra_key in self.extra_paths
extra_deps = get_dependencies(self.extra_paths[extra_key])
for dep in extra_deps:
assert any(dep in require for require in extra_requires)
37 changes: 37 additions & 0 deletions tests/utils/dependency_getter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import io
from pathlib import Path

from pigar.core import RequirementsAnalyzer
from pigar.parser import DEFAULT_GLOB_EXCLUDE_PATTERNS


def get_dependencies(path: Path) -> list[str]:
"""Gets the dependencies of the given directory.
Args:
path: The path of the directory.
Returns:
The list of packages that the directory depends on.
"""
analyzer = RequirementsAnalyzer(str(path))
buf = io.StringIO()
comparison_specifier = "=="
analyzer.analyze_requirements(
visit_doc_str=False,
follow_symbolic_links=False,
ignores=DEFAULT_GLOB_EXCLUDE_PATTERNS,
)
analyzer.write_requirements(
buf,
with_ref_comments=False,
with_banner=False,
with_unknown_imports=False,
comparison_specifier=comparison_specifier,
)
return [
dep.split(comparison_specifier)[0]
for dep in filter(
lambda x: comparison_specifier in x, buf.getvalue().split("\n")
)
]

0 comments on commit 12f8c80

Please sign in to comment.