diff --git a/ptr.py b/ptr.py index f723198..94aa657 100755 --- a/ptr.py +++ b/ptr.py @@ -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("/") @@ -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, @@ -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": diff --git a/ptr_tests.py b/ptr_tests.py index 8e6bbb3..e6e24f2 100644 --- a/ptr_tests.py +++ b/ptr_tests.py @@ -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 diff --git a/ptr_tests_fixtures.py b/ptr_tests_fixtures.py index 3ea030d..cb6f344 100644 --- a/ptr_tests_fixtures.py +++ b/ptr_tests_fixtures.py @@ -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 @@ -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, @@ -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, @@ -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 = """\ @@ -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 diff --git a/setup.cfg.sample b/setup.cfg.sample index 2b080d7..9909ef0 100644 --- a/setup.cfg.sample +++ b/setup.cfg.sample @@ -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 diff --git a/setup.py b/setup.py index 9bc683c..1655dcb 100644 --- a/setup.py +++ b/setup.py @@ -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