|
| 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