|
| 1 | +import subprocess |
| 2 | + |
| 3 | +import pytest |
| 4 | +from toolbox.util.dependencies.poetry_dependencies import ( |
| 5 | + PoetryDependencies, |
| 6 | + PoetryDependency, |
| 7 | + PoetryGroup, |
| 8 | + PoetryToml, |
| 9 | +) |
| 10 | + |
| 11 | +MAIN_GROUP = PoetryGroup(name="main", toml_section="project.dependencies") |
| 12 | +DEV_GROUP = PoetryGroup(name="dev", toml_section="tool.poetry.group.dev.dependencies") |
| 13 | +ANALYSIS_GROUP = PoetryGroup( |
| 14 | + name="analysis", toml_section="tool.poetry.group.analysis.dependencies" |
| 15 | +) |
| 16 | + |
| 17 | +DIRECT_DEPENDENCIES = { |
| 18 | + MAIN_GROUP.name: [ |
| 19 | + PoetryDependency(name="numpy", version="2.2.6", group=MAIN_GROUP), |
| 20 | + PoetryDependency(name="pylint", version="3.3.7", group=MAIN_GROUP), |
| 21 | + ], |
| 22 | + DEV_GROUP.name: [PoetryDependency(name="isort", version="6.0.1", group=DEV_GROUP)], |
| 23 | + ANALYSIS_GROUP.name: [ |
| 24 | + PoetryDependency(name="black", version="25.1.0", group=ANALYSIS_GROUP) |
| 25 | + ], |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="module") |
| 30 | +def cwd(tmp_path_factory): |
| 31 | + return tmp_path_factory.mktemp("test") |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="module") |
| 35 | +def project_name(): |
| 36 | + return "project" |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture(scope="module") |
| 40 | +def project_path(cwd, project_name): |
| 41 | + return cwd / project_name |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope="module") |
| 45 | +def create_poetry_project(cwd, project_name, project_path): |
| 46 | + subprocess.run(["poetry", "new", project_name], cwd=cwd) |
| 47 | + subprocess.run(["poetry", "add", "numpy"], cwd=project_path) |
| 48 | + subprocess.run(["poetry", "add", "pylint"], cwd=project_path) |
| 49 | + subprocess.run(["poetry", "add", "--group", "dev", "isort"], cwd=project_path) |
| 50 | + subprocess.run(["poetry", "add", "--group", "analysis", "black"], cwd=project_path) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(scope="module") |
| 54 | +def pyproject_toml(project_path, create_poetry_project): |
| 55 | + return PoetryToml(working_directory=project_path) |
| 56 | + |
| 57 | + |
| 58 | +class TestPackage: |
| 59 | + @staticmethod |
| 60 | + @pytest.mark.parametrize( |
| 61 | + "name,expected", |
| 62 | + [ |
| 63 | + ("numpy", "numpy"), |
| 64 | + ("sphinxcontrib-applehelp", "sphinxcontrib-applehelp"), |
| 65 | + ("Imaginary_package", "imaginary-package"), |
| 66 | + ], |
| 67 | + ) |
| 68 | + def test_normalized_name(name, expected): |
| 69 | + dep = PoetryDependency(name=name, version="0.1.0", group=None) |
| 70 | + assert dep.normalized_name == expected |
| 71 | + |
| 72 | + |
| 73 | +class TestPoetryToml: |
| 74 | + @staticmethod |
| 75 | + def test_get_section_dict_exists(pyproject_toml): |
| 76 | + result = pyproject_toml.get_section_dict("project") |
| 77 | + assert result is not None |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def test_get_section_dict_does_not_exist(pyproject_toml): |
| 81 | + result = pyproject_toml.get_section_dict("test") |
| 82 | + assert result is None |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def test_groups(pyproject_toml): |
| 86 | + assert pyproject_toml.groups == (MAIN_GROUP, DEV_GROUP, ANALYSIS_GROUP) |
| 87 | + |
| 88 | + |
| 89 | +class TestPoetryDependencies: |
| 90 | + @staticmethod |
| 91 | + @pytest.mark.parametrize( |
| 92 | + "line,expected", |
| 93 | + [ |
| 94 | + ( |
| 95 | + "coverage 7.8.0 Code coverage measurement for Python", |
| 96 | + PoetryDependency(name="coverage", version="7.8.0", group=MAIN_GROUP), |
| 97 | + ), |
| 98 | + ( |
| 99 | + "furo 2024.8.6 A clean customisable Sphinx documentation theme.", |
| 100 | + PoetryDependency(name="furo", version="2024.8.6", group=MAIN_GROUP), |
| 101 | + ), |
| 102 | + ( |
| 103 | + "import-linter 2.3 Enforces rules for the imports within and between Python packages.", |
| 104 | + PoetryDependency(name="import-linter", version="2.3", group=MAIN_GROUP), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ) |
| 108 | + def test_extract_from_line(line, expected): |
| 109 | + result = PoetryDependencies._extract_from_line(line=line, group=MAIN_GROUP) |
| 110 | + assert result == expected |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def test_direct_dependencies(create_poetry_project, project_path): |
| 114 | + poetry_dep = PoetryDependencies( |
| 115 | + groups=(MAIN_GROUP, DEV_GROUP, ANALYSIS_GROUP), |
| 116 | + working_directory=project_path, |
| 117 | + ) |
| 118 | + assert poetry_dep.direct_dependencies == DIRECT_DEPENDENCIES |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def test_all_dependencies(create_poetry_project, project_path): |
| 122 | + # set_direct_deps = set(DIRECT_DEPENDENCIES) |
| 123 | + |
| 124 | + poetry_dep = PoetryDependencies( |
| 125 | + groups=(MAIN_GROUP, DEV_GROUP, ANALYSIS_GROUP), |
| 126 | + working_directory=project_path, |
| 127 | + ) |
| 128 | + result = poetry_dep.all_dependencies |
| 129 | + |
| 130 | + transitive = result.pop("transitive") |
| 131 | + assert len(transitive) > 0 |
| 132 | + assert result == DIRECT_DEPENDENCIES |
0 commit comments