| 
 | 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.mock import patch  | 
 | 22 | + | 
 | 23 | +from pyfakefs.fake_filesystem_unittest import TestCase  | 
 | 24 | + | 
 | 25 | +from pysonar_scanner.configuration.coveragerc_loader import CoverageRCConfigurationLoader  | 
 | 26 | + | 
 | 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_exclusion_properties(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_exclusion_properties(Path("."))  | 
 | 50 | +        self.assertEqual(len(properties), 0)  | 
 | 51 | +        mock_logging.debug.assert_called_with("Coverage 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_exclusion_properties(Path("."))  | 
 | 62 | +        self.assertEqual(len(properties), 0)  | 
 | 63 | +        mock_logging.debug.assert_called_with("Coverage file .coveragerc has no exclusion properties")  | 
 | 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_exclusion_properties(Path("."))  | 
 | 74 | +        self.assertEqual(len(properties), 0)  | 
 | 75 | +        mock_logging.debug.assert_called_with("Coverage file .coveragerc has no exclusion properties")  | 
 | 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_exclusion_properties(Path("."))  | 
 | 87 | +        self.assertEqual(len(properties), 0)  | 
0 commit comments