From 911a4fae421983f9573ab9cad66b58a4206c8150 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Tue, 9 Jan 2024 08:16:09 -0300 Subject: [PATCH] add test coverage for setup.cfg --- .../dependency_management/setupcfg_writer.py | 4 +- .../file_parsers/setup_cfg_file_parser.py | 2 +- tests/conftest.py | 22 +++++++++++ .../test_dependency_manager.py | 17 ++++++++- .../test_setupcfgt_writer.py | 37 +++++++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/src/codemodder/dependency_management/setupcfg_writer.py b/src/codemodder/dependency_management/setupcfg_writer.py index 473cf96b8..0728d4c51 100644 --- a/src/codemodder/dependency_management/setupcfg_writer.py +++ b/src/codemodder/dependency_management/setupcfg_writer.py @@ -15,7 +15,7 @@ def find_leading_whitespace(s): match = re.match(r"(\s+)", s) if match: return match.group(1) - return "" + return "" # pragma: no cover def added_line_nums_strategy(lines, i): @@ -79,7 +79,7 @@ def build_new_lines( configparser does not retain formatting or comment lines, so we have to build the output newline manually. """ - clean_lines = list(map(lambda s: s.strip(), original_lines)) + clean_lines = [s.strip() for s in original_lines] newline_separated = len(defined_dependencies.split("\n")) > 1 if newline_separated: diff --git a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py index e8c53919f..332a09db1 100644 --- a/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py +++ b/src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py @@ -20,7 +20,7 @@ def _parse_file(self, file: Path) -> PackageStore | None: config.read(file) except configparser.ParsingError: logger.debug("Unable to parse setup.cfg file.") - return None + return None # pragma: no cover if "options" not in config: return None diff --git a/tests/conftest.py b/tests/conftest.py index 4ed330e99..2ef1826b5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -79,3 +79,25 @@ def pkg_with_reqs_txt_unknown_encoding(tmp_path_factory): reqs = "\xf0\x28\x8c\xbc" req_file.write_text(reqs) return base_dir + + +@pytest.fixture(scope="module") +def pkg_with_setup_cfg(tmp_path_factory): + base_dir = tmp_path_factory.mktemp("foo") + req_file = base_dir / "setup.cfg" + reqs = """\ + [metadata] + name = my_package + version = attr: my_package.VERSION + + # some other stuff + + [options] + include_package_data = True + python_requires = >=3.7 + install_requires = + requests + importlib-metadata; python_version<"3.8" + """ + req_file.write_text(reqs) + return base_dir diff --git a/tests/dependency_management/test_dependency_manager.py b/tests/dependency_management/test_dependency_manager.py index ec6f4dedb..fee20f61a 100644 --- a/tests/dependency_management/test_dependency_manager.py +++ b/tests/dependency_management/test_dependency_manager.py @@ -3,7 +3,10 @@ from codemodder.change import ChangeSet from codemodder.dependency import DefusedXML, Security from codemodder.dependency_management import DependencyManager -from codemodder.project_analysis.file_parsers import RequirementsTxtParser +from codemodder.project_analysis.file_parsers import ( + RequirementsTxtParser, + SetupCfgParser, +) from codemodder.project_analysis.file_parsers.package_store import PackageStore @@ -33,3 +36,15 @@ def test_write_for_requirements_txt(self, pkg_with_reqs_txt): changeset = dm.write(dependencies) assert isinstance(changeset, ChangeSet) + assert len(changeset.changes) + + def test_write_for_setup_cfg(self, pkg_with_setup_cfg): + parser = SetupCfgParser(pkg_with_setup_cfg) + stores = parser.parse() + assert len(stores) == 1 + dm = DependencyManager(stores[0], pkg_with_setup_cfg) + dependencies = [DefusedXML, Security] + + changeset = dm.write(dependencies) + assert isinstance(changeset, ChangeSet) + assert len(changeset.changes) diff --git a/tests/dependency_management/test_setupcfgt_writer.py b/tests/dependency_management/test_setupcfgt_writer.py index 6366aab45..c392ad5c8 100644 --- a/tests/dependency_management/test_setupcfgt_writer.py +++ b/tests/dependency_management/test_setupcfgt_writer.py @@ -1,4 +1,5 @@ from textwrap import dedent +import mock import pytest from codemodder.dependency_management.setupcfg_writer import SetupCfgWriter @@ -237,6 +238,42 @@ def test_cfg_bad_formatting(tmpdir): assert setup_cfg.read() == dedent(orig_setupcfg) +@mock.patch( + "codemodder.dependency_management.setupcfg_writer.SetupCfgWriter.build_new_lines", + return_value=None, +) +def test_cfg_cant_build_newlines(_, tmpdir): + orig_setupcfg = """\ + [metadata] + name = my_package + version = attr: my_package.VERSION + + # some other stuff + + [options] + include_package_data = True + python_requires = >=3.7 + install_requires = + requests + importlib-metadata; python_version<"3.8" + """ + + setup_cfg = tmpdir.join("setup.cfg") + setup_cfg.write(dedent(orig_setupcfg)) + + store = PackageStore( + type=FileType.SETUP_CFG, + file=str(setup_cfg), + dependencies=set(), + py_versions=[">=3.7"], + ) + + writer = SetupCfgWriter(store, tmpdir) + dependencies = [Security, Security] + writer.write(dependencies) + assert setup_cfg.read() == dedent(orig_setupcfg) + + def test_cfg_inline_dependencies(tmpdir): orig_setupcfg = """\ [metadata]