Skip to content

Commit b9f1246

Browse files
add coverage
1 parent 4e79413 commit b9f1246

File tree

2 files changed

+91
-4
lines changed

2 files changed

+91
-4
lines changed

src/pysonar_scanner/configuration/coveragerc_loader.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def load(base_dir: pathlib.Path) -> dict[str, str]:
3131
result_dict: dict[str, str] = {}
3232

3333
coverage_properties = CoverageRCConfigurationLoader.__read_config(config_file_path)
34+
if len(coverage_properties) == 0:
35+
return result_dict
3436
exclusion_properties = CoverageRCConfigurationLoader.__read_coverage_exclusions_properties(
3537
config_file_path, coverage_properties
3638
)
@@ -55,10 +57,8 @@ def __read_config(config_file_path: pathlib.Path) -> dict[str, Any]:
5557
section_values[key] = value
5658

5759
config_dict[section] = section_values
58-
except configparser.Error as e:
59-
logging.debug(f"Error decoding coverage file {config_file_path}: {e}")
6060
except Exception as e:
61-
logging.debug(f"An unexpected error occurred while processing {config_file_path}: {e}")
61+
logging.debug(f"Error decoding coverage file {config_file_path}: {e}")
6262
return config_dict
6363

6464
@staticmethod
@@ -71,7 +71,7 @@ def __read_coverage_exclusions_properties(
7171
return result_dict
7272

7373
if "omit" not in coverage_properties["run"]:
74-
logging.debug(f"The run.omit was not found in {config_file_path}")
74+
logging.debug(f"The run.omit key was not found in {config_file_path}")
7575
return result_dict
7676

7777
omit_exclusions = coverage_properties["run"]["omit"]

tests/unit/test_coveragerc_loader.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
from pathlib import Path
21+
from unittest import mock
22+
from unittest.mock import MagicMock, patch
23+
24+
from pyfakefs.fake_filesystem_unittest import TestCase
25+
from pysonar_scanner.configuration.pyproject_toml import TomlConfigurationLoader
26+
from pysonar_scanner.configuration.coveragerc_loader import CoverageRCConfigurationLoader
27+
28+
class TestCoverageRcFile(TestCase):
29+
def setUp(self):
30+
self.setUpPyfakefs()
31+
32+
def test_load_coverage_file(self):
33+
self.fs.create_file(
34+
".coveragerc",
35+
contents="""
36+
[run]
37+
omit =
38+
*/.local/*
39+
/usr/*
40+
utils/tirefire.py
41+
""",
42+
)
43+
properties = CoverageRCConfigurationLoader.load(Path("."))
44+
45+
self.assertEqual(properties["sonar.coverage.exclusions"], "*/.local/*, /usr/*, utils/tirefire.py")
46+
47+
@patch("pysonar_scanner.configuration.coveragerc_loader.logging")
48+
def test_load_missing_file(self, mock_logging):
49+
properties = CoverageRCConfigurationLoader.load(Path("."))
50+
self.assertEqual(len(properties), 0)
51+
mock_logging.debug.assert_called_with("Configuration file not found: .coveragerc")
52+
53+
@patch("pysonar_scanner.configuration.coveragerc_loader.logging")
54+
def test_load_without_run_section(self, mock_logging):
55+
self.fs.create_file(
56+
".coveragerc",
57+
contents="""
58+
[something_else]
59+
""",
60+
)
61+
properties = CoverageRCConfigurationLoader.load(Path("."))
62+
self.assertEqual(len(properties), 0)
63+
mock_logging.debug.assert_called_with("The run key was not found in .coveragerc")
64+
65+
@patch("pysonar_scanner.configuration.coveragerc_loader.logging")
66+
def test_load_without_exclusions_property(self, mock_logging):
67+
self.fs.create_file(
68+
".coveragerc",
69+
contents="""
70+
[run]
71+
""",
72+
)
73+
properties = CoverageRCConfigurationLoader.load(Path("."))
74+
self.assertEqual(len(properties), 0)
75+
mock_logging.debug.assert_called_with("The run.omit key was not found in .coveragerc")
76+
77+
@patch("pysonar_scanner.configuration.coveragerc_loader.logging")
78+
def test_load_malformed_file(self, mock_logging):
79+
self.fs.create_file(
80+
".coveragerc",
81+
contents="""
82+
[run
83+
omit =
84+
""",
85+
)
86+
properties = CoverageRCConfigurationLoader.load(Path("."))
87+
self.assertEqual(len(properties), 0)

0 commit comments

Comments
 (0)