-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ASSET-18] Add dependency test (#19)
* 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
Showing
12 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-r requirements/all.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) | ||
] |