Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Support Floats for required coverage (#108)
Browse files Browse the repository at this point in the history
- We need to support floats as coverage can return floats
- Make function for cleaner % removal

Testing: Update tests to ensure it works

Addresses part of #107
  • Loading branch information
cooperlees authored Mar 16, 2021
1 parent 46b7362 commit 3d45bee
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
15 changes: 11 additions & 4 deletions ptr.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ def _config_default() -> ConfigParser:


def _config_read(
cwd: str, conf_name: str = ".ptrconfig", cp: ConfigParser = _config_default()
cwd: str, conf_name: str = ".ptrconfig", cp: Optional[ConfigParser] = None
) -> ConfigParser:
""" Look from cwd to / for a "conf_name" file - If so read it in """
if cp is None:
cp = _config_default()

cwd_path = Path(cwd) # type: Path
root_path = Path(f"{cwd_path.drive}\\") if WINDOWS else Path("/")

Expand Down Expand Up @@ -116,10 +119,14 @@ def _get_site_packages_path(venv_path: Path) -> Optional[Path]:
return None


def _remove_pct_symbol(pct_str: str) -> str:
return pct_str.strip().replace("%", "")


def _analyze_coverage(
venv_path: Path,
setup_py_path: Path,
required_cov: Dict[str, int],
required_cov: Dict[str, float],
coverage_report: str,
stats: Dict[str, int],
test_run_start_time: float,
Expand Down Expand Up @@ -172,11 +179,11 @@ def _analyze_coverage(

if len(sl) == 4:
coverage_lines[module_path_str] = coverage_line(
int(sl[1]), int(sl[2]), int(sl[3][:-1]), ""
float(sl[1]), float(sl[2]), float(_remove_pct_symbol(sl[3])), ""
)
else:
coverage_lines[module_path_str] = coverage_line(
int(sl[1]), int(sl[2]), int(sl[3][:-1]), sl[4]
float(sl[1]), float(sl[2]), float(_remove_pct_symbol(sl[3])), sl[4]
)

if sl[0] != "TOTAL":
Expand Down
13 changes: 13 additions & 0 deletions ptr_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ def test_analyze_coverage(self, mock_log: Mock, mock_time: Mock) -> None:
ptr_tests_fixtures.EXPECTED_COVERAGE_FAIL_RESULT,
)

# Test with float coverage
self.assertEqual(
ptr._analyze_coverage(
fake_venv_path,
fake_setup_py,
ptr_tests_fixtures.FAKE_REQ_COVERAGE,
ptr_tests_fixtures.SAMPLE_FLOAT_REPORT_OUTPUT,
{},
0,
),
ptr_tests_fixtures.EXPECTED_COVERAGE_FAIL_RESULT,
)

# Test with venv installed modules
cov_report = (
ptr_tests_fixtures.SAMPLE_WIN_TG_REPORT_OUTPUT
Expand Down
23 changes: 18 additions & 5 deletions ptr_tests_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
from sys import version_info
from tempfile import gettempdir
from typing import Dict

from ptr import test_result

Expand Down Expand Up @@ -42,7 +43,7 @@ def run_until_complete(self, *args, **kwargs) -> int:
returncode=3,
output=(
"The following files did not meet coverage requirements:\n"
+ f" unittest{sep}ptr.py: 69 < 99 - Missing: 70-72, 76-94, 98\n"
+ f" unittest{sep}ptr.py: 69.0 < 99.0 - Missing: 70-72, 76-94, 98\n"
),
runtime=0,
timeout=False,
Expand All @@ -52,8 +53,8 @@ def run_until_complete(self, *args, **kwargs) -> int:
returncode=3,
output=(
f"The following files did not meet coverage requirements:\n tg{sep}tg.py: "
+ "22 < 99 - Missing: 39-59, 62-73, 121, 145-149, 153-225, 231-234, 238\n "
+ "TOTAL: 40 < 99 - Missing: \n"
+ "22.0 < 99 - Missing: 39-59, 62-73, 121, 145-149, 153-225, 231-234, 238\n "
+ "TOTAL: 40.0 < 99 - Missing: \n"
),
runtime=0,
timeout=False,
Expand Down Expand Up @@ -93,8 +94,8 @@ def run_until_complete(self, *args, **kwargs) -> int:
),
]

FAKE_REQ_COVERAGE = {str(Path("unittest/ptr.py")): 99, "TOTAL": 99}
FAKE_TG_REQ_COVERAGE = {str(Path("tg/tg.py")): 99, "TOTAL": 99}
FAKE_REQ_COVERAGE = {str(Path("unittest/ptr.py")): 99.0, "TOTAL": 99}
FAKE_TG_REQ_COVERAGE: Dict[str, float] = {str(Path("tg/tg.py")): 99, "TOTAL": 99}


SAMPLE_REPORT_OUTPUT = """\
Expand All @@ -109,6 +110,18 @@ def run_until_complete(self, *args, **kwargs) -> int:
sep=sep
)

SAMPLE_FLOAT_REPORT_OUTPUT = """\
Name Stmts Miss Cover Missing
------------------------------------------------------------------
unittest{sep}ptr.py 59 14 69.00% 70-72, 76-94, 98
unittest{sep}ptr_tests.py 24 0 100.00%
unittest{sep}ptr_venv_fixtures.py 1 0 100.00%
------------------------------------------------------------------
TOTAL 84 14 99.00%
""".format(
sep=sep
)

HARD_SET_VENV = Path(f"{gettempdir()}/ptr_venv_2580217")
BASE_VENV_PATH = (
Path(environ["VIRTUAL_ENV"]) if "VIRTUAL_ENV" in environ else HARD_SET_VENV
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ entry_point_module = ptr
test_suite = ptr_tests
test_suite_timeout = 120
# `required_coverage_` will be stripped for use
required_coverage_ptr.py = 85
required_coverage_ptr.py = 85.0
required_coverage_TOTAL = 90
run_black = true
run_mypy = true
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test_suite": "ptr_tests",
"test_suite_timeout": 120,
# Relative path from setup.py to module (e.g. ptr == ptr.py)
"required_coverage": {"ptr.py": 85, "TOTAL": 90},
"required_coverage": {"ptr.py": 85.0, "TOTAL": 90},
# Run black or not
"run_black": True,
# Run mypy or not
Expand Down

0 comments on commit 3d45bee

Please sign in to comment.