-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_pyproject_toml.py
More file actions
210 lines (182 loc) · 8.64 KB
/
test_pyproject_toml.py
File metadata and controls
210 lines (182 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#
# Sonar Scanner Python
# Copyright (C) 2011-2026 SonarSource Sàrl
# mailto:info AT sonarsource DOT com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
#
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
from pathlib import Path
from unittest import mock
from unittest.mock import MagicMock, patch
from pyfakefs.fake_filesystem_unittest import TestCase
from pysonar_scanner.configuration.pyproject_toml import TomlConfigurationLoader
class TestTomlFile(TestCase):
def setUp(self):
self.setUpPyfakefs()
def test_load_toml_file_with_sonarqube_config(self):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.sonar]
projectKey = "my-project"
projectName = "My Project"
sources = "src"
exclusions = "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*"
some.unknownProperty = "unknown_property_value"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "my-project")
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "My Project")
self.assertEqual(properties.sonar_properties.get("sonar.sources"), "src")
self.assertEqual(
properties.sonar_properties.get("sonar.exclusions"), "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*"
)
self.assertEqual(properties.sonar_properties.get("sonar.some.unknownProperty"), "unknown_property_value")
def test_load_toml_file_kebab_case(self):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.sonar]
project-key = "my-project"
project-name = "My Project"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "my-project")
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "My Project")
@patch("pysonar_scanner.configuration.pyproject_toml.logging")
def test_load_toml_file_kebab_case_unknown_properties(self, mock_logging):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.sonar]
coverage-report-paths = "coverage.xml"
some-unknown-property = "some-value"
nested-property.some-nested-key = "nested-value"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(properties.sonar_properties.get("sonar.coverageReportPaths"), "coverage.xml")
self.assertEqual(properties.sonar_properties.get("sonar.someUnknownProperty"), "some-value")
self.assertEqual(properties.sonar_properties.get("sonar.nestedProperty.someNestedKey"), "nested-value")
mock_logging.debug.assert_any_call(
"Converting kebab-case property 'sonar.coverage-report-paths' to camelCase: 'sonar.coverageReportPaths'"
)
mock_logging.debug.assert_any_call(
"Converting kebab-case property 'sonar.some-unknown-property' to camelCase: 'sonar.someUnknownProperty'"
)
mock_logging.debug.assert_any_call(
"Converting kebab-case property 'sonar.nested-property.some-nested-key' to camelCase: 'sonar.nestedProperty.someNestedKey'"
)
def test_load_toml_file_without_sonar_section(self):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.black]
line-length = 88
target-version = ["py38"]
[tool.isort]
profile = "black"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(len(properties.sonar_properties), 0)
def test_load_missing_file(self):
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(len(properties.sonar_properties), 0)
def test_load_empty_file(self):
self.fs.create_file("pyproject.toml", contents="")
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(len(properties.sonar_properties), 0)
@patch("pysonar_scanner.configuration.pyproject_toml.logging")
def test_load_malformed_toml_file(self, mock_logging):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.sonar
sonar.projectKey = "my-project"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(len(properties.sonar_properties), 0)
mock_logging.warning.assert_called_once_with(
"There was an error reading the pyproject.toml file. No properties from the TOML file were extracted. Error: Expected ']' at the end of a table declaration (at line 2, column 24)",
)
def test_load_toml_with_nested_values(self):
self.fs.create_file(
"pyproject.toml",
contents="""
[tool.sonar]
projectKey = "my-project"
[tool.sonar.python]
version = "3.9,3.10,3.11,3.12,3.13"
coverage.reportPaths = "coverage.xml"
""",
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "my-project")
self.assertEqual(properties.sonar_properties.get("sonar.python.version"), "3.9,3.10,3.11,3.12,3.13")
self.assertEqual(properties.sonar_properties.get("sonar.python.coverage.reportPaths"), "coverage.xml")
def test_load_toml_file_from_custom_dir(self):
self.fs.create_dir("custom/path")
self.fs.create_file(
"custom/path/pyproject.toml",
contents="""
[tool.sonar]
projectKey = "custom-path-project"
projectName = "Custom Path Project"
""",
)
properties = TomlConfigurationLoader.load(Path("custom/path"))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "custom-path-project")
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "Custom Path Project")
def test_load_toml_file_from_direct_file_path(self):
self.fs.create_dir("custom/path")
self.fs.create_file(
"custom/path/pyproject.toml",
contents="""
[tool.sonar]
projectKey = "direct-file-project"
projectName = "Direct File Project"
""",
)
properties = TomlConfigurationLoader.load(Path("custom/path/pyproject.toml"))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "direct-file-project")
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "Direct File Project")
def test_load_toml_file_from_direct_file_path_missing(self):
properties = TomlConfigurationLoader.load(Path("nonexistent/pyproject.toml"))
self.assertEqual(len(properties.sonar_properties), 0)
def test_load_toml_file_project_content(self):
self.fs.create_file(
"pyproject.toml",
contents=(
"""
[project]
name = "My Overridden Project Name"
description = "My Project Description"
requires-python = ["3.6", "3.7", "3.8"]
[tool.sonar]
project-key = "my-project"
project-name = "My Project"
"""
),
)
properties = TomlConfigurationLoader.load(Path("."))
self.assertEqual(properties.sonar_properties.get("sonar.projectKey"), "my-project")
self.assertEqual(properties.sonar_properties.get("sonar.projectName"), "My Project")
self.assertEqual(properties.project_properties.get("sonar.projectName"), "My Overridden Project Name")
self.assertEqual(properties.project_properties.get("sonar.projectDescription"), "My Project Description")
self.assertEqual(properties.project_properties.get("sonar.python.version"), "3.6,3.7,3.8")