|
| 1 | +# |
| 2 | +# Sonar Scanner Python |
| 3 | +# Copyright (C) 2011-2024 SonarSource SA. |
| 4 | +# mailto:info AT sonarsource DOT com |
| 5 | +# |
| 6 | +# This program is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU Lesser General Public |
| 8 | +# License as published by the Free Software Foundation; either |
| 9 | +# version 3 of the License, or (at your option) any later version. |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | +# Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with this program; if not, write to the Free Software Foundation, |
| 18 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +# |
| 20 | +import configparser |
| 21 | +import logging |
| 22 | +import pathlib |
| 23 | +from typing import Any |
| 24 | + |
| 25 | + |
| 26 | +class CoverageRCConfigurationLoader: |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def load(base_dir: pathlib.Path) -> dict[str, str]: |
| 30 | + config_file_path = base_dir / ".coveragerc" |
| 31 | + result_dict: dict[str, str] = {} |
| 32 | + |
| 33 | + coverage_properties = CoverageRCConfigurationLoader.__read_config(config_file_path) |
| 34 | + if len(coverage_properties) == 0: |
| 35 | + return result_dict |
| 36 | + exclusion_properties = CoverageRCConfigurationLoader.__read_coverage_exclusions_properties( |
| 37 | + config_file_path, coverage_properties |
| 38 | + ) |
| 39 | + result_dict.update(exclusion_properties) |
| 40 | + return result_dict |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def __read_config(config_file_path: pathlib.Path) -> dict[str, Any]: |
| 44 | + config_dict: dict[str, Any] = {} |
| 45 | + if not config_file_path.exists(): |
| 46 | + logging.debug(f"Configuration file not found: {config_file_path}") |
| 47 | + return config_dict |
| 48 | + |
| 49 | + try: |
| 50 | + config_parser = configparser.ConfigParser() |
| 51 | + config_parser.read(config_file_path) |
| 52 | + |
| 53 | + # Iterate over sections and options |
| 54 | + for section in config_parser.sections(): |
| 55 | + section_values = {} |
| 56 | + for key, value in config_parser.items(section): |
| 57 | + section_values[key] = value |
| 58 | + |
| 59 | + config_dict[section] = section_values |
| 60 | + except Exception as e: |
| 61 | + logging.debug(f"Error decoding coverage file {config_file_path}: {e}") |
| 62 | + return config_dict |
| 63 | + |
| 64 | + @staticmethod |
| 65 | + def __read_coverage_exclusions_properties( |
| 66 | + config_file_path: pathlib.Path, coverage_properties: dict[str, Any] |
| 67 | + ) -> dict[str, Any]: |
| 68 | + result_dict: dict[str, Any] = {} |
| 69 | + if "run" not in coverage_properties: |
| 70 | + logging.debug(f"The run key was not found in {config_file_path}") |
| 71 | + return result_dict |
| 72 | + |
| 73 | + if "omit" not in coverage_properties["run"]: |
| 74 | + logging.debug(f"The run.omit key was not found in {config_file_path}") |
| 75 | + return result_dict |
| 76 | + |
| 77 | + omit_exclusions = coverage_properties["run"]["omit"] |
| 78 | + patterns_list = [patterns.strip() for patterns in omit_exclusions.splitlines() if patterns.strip()] |
| 79 | + translated_exclusions = ", ".join(patterns_list) |
| 80 | + |
| 81 | + result_dict["sonar.coverage.exclusions"] = translated_exclusions |
| 82 | + return result_dict |
0 commit comments