Skip to content

Commit

Permalink
add test coverage for setup.cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
clavedeluna committed Jan 9, 2024
1 parent 8e51c98 commit 911a4fa
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/codemodder/dependency_management/setupcfg_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Check warning on line 22 in src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py

View check run for this annotation

Codecov / codecov/patch

src/codemodder/project_analysis/file_parsers/setup_cfg_file_parser.py#L22

Added line #L22 was not covered by tests
return None
return None # pragma: no cover

if "options" not in config:
return None
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 16 additions & 1 deletion tests/dependency_management/test_dependency_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
37 changes: 37 additions & 0 deletions tests/dependency_management/test_setupcfgt_writer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from textwrap import dedent
import mock
import pytest

from codemodder.dependency_management.setupcfg_writer import SetupCfgWriter
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 911a4fa

Please sign in to comment.