2020from pathlib import Path
2121
2222from pyfakefs .fake_filesystem_unittest import TestCase
23- from pysonar_scanner .configuration import pyproject_toml
23+ from pysonar_scanner .configuration . pyproject_toml import TomlConfigurationLoader
2424
2525
2626class TestTomlFile (TestCase ):
@@ -38,12 +38,14 @@ def test_load_toml_file_with_sonarqube_config(self):
3838 exclusions = "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*"
3939 """ ,
4040 )
41- properties = pyproject_toml .load (Path ("." ))
41+ properties = TomlConfigurationLoader .load (Path ("." ))
4242
43- self .assertEqual (properties .get ("sonar.projectKey" ), "my-project" )
44- self .assertEqual (properties .get ("sonar.projectName" ), "My Project" )
45- self .assertEqual (properties .get ("sonar.sources" ), "src" )
46- self .assertEqual (properties .get ("sonar.exclusions" ), "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*" )
43+ self .assertEqual (properties .sonar_properties .get ("sonar.projectKey" ), "my-project" )
44+ self .assertEqual (properties .sonar_properties .get ("sonar.projectName" ), "My Project" )
45+ self .assertEqual (properties .sonar_properties .get ("sonar.sources" ), "src" )
46+ self .assertEqual (
47+ properties .sonar_properties .get ("sonar.exclusions" ), "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*"
48+ )
4749
4850 def test_load_toml_file_kebab_case (self ):
4951 self .fs .create_file (
@@ -54,10 +56,10 @@ def test_load_toml_file_kebab_case(self):
5456 project-name = "My Project"
5557 """ ,
5658 )
57- properties = pyproject_toml .load (Path ("." ))
59+ properties = TomlConfigurationLoader .load (Path ("." ))
5860
59- self .assertEqual (properties .get ("sonar.projectKey" ), "my-project" )
60- self .assertEqual (properties .get ("sonar.projectName" ), "My Project" )
61+ self .assertEqual (properties .sonar_properties . get ("sonar.projectKey" ), "my-project" )
62+ self .assertEqual (properties .sonar_properties . get ("sonar.projectName" ), "My Project" )
6163
6264 def test_load_toml_file_without_sonar_section (self ):
6365 self .fs .create_file (
@@ -71,19 +73,19 @@ def test_load_toml_file_without_sonar_section(self):
7173 profile = "black"
7274 """ ,
7375 )
74- properties = pyproject_toml .load (Path ("." ))
76+ properties = TomlConfigurationLoader .load (Path ("." ))
7577
76- self .assertEqual (len (properties ), 0 )
78+ self .assertEqual (len (properties . sonar_properties ), 0 )
7779
7880 def test_load_missing_file (self ):
79- properties = pyproject_toml .load (Path ("." ))
80- self .assertEqual (len (properties ), 0 )
81+ properties = TomlConfigurationLoader .load (Path ("." ))
82+ self .assertEqual (len (properties . sonar_properties ), 0 )
8183
8284 def test_load_empty_file (self ):
8385 self .fs .create_file ("pyproject.toml" , contents = "" )
84- properties = pyproject_toml .load (Path ("." ))
86+ properties = TomlConfigurationLoader .load (Path ("." ))
8587
86- self .assertEqual (len (properties ), 0 )
88+ self .assertEqual (len (properties . sonar_properties ), 0 )
8789
8890 def test_load_malformed_toml_file (self ):
8991 self .fs .create_file (
@@ -93,9 +95,9 @@ def test_load_malformed_toml_file(self):
9395 sonar.projectKey = "my-project"
9496 """ ,
9597 )
96- properties = pyproject_toml .load (Path ("." ))
98+ properties = TomlConfigurationLoader .load (Path ("." ))
9799
98- self .assertEqual (len (properties ), 0 )
100+ self .assertEqual (len (properties . sonar_properties ), 0 )
99101
100102 def test_load_toml_with_nested_values (self ):
101103 self .fs .create_file (
@@ -109,11 +111,11 @@ def test_load_toml_with_nested_values(self):
109111 coverage.reportPaths = "coverage.xml"
110112 """ ,
111113 )
112- properties = pyproject_toml .load (Path ("." ))
114+ properties = TomlConfigurationLoader .load (Path ("." ))
113115
114- self .assertEqual (properties .get ("sonar.projectKey" ), "my-project" )
115- self .assertEqual (properties .get ("sonar.python.version" ), "3.9,3.10,3.11,3.12,3.13" )
116- self .assertEqual (properties .get ("sonar.python.coverage.reportPaths" ), "coverage.xml" )
116+ self .assertEqual (properties .sonar_properties . get ("sonar.projectKey" ), "my-project" )
117+ self .assertEqual (properties .sonar_properties . get ("sonar.python.version" ), "3.9,3.10,3.11,3.12,3.13" )
118+ self .assertEqual (properties .sonar_properties . get ("sonar.python.coverage.reportPaths" ), "coverage.xml" )
117119
118120 def test_load_toml_file_from_custom_dir (self ):
119121 self .fs .create_dir ("custom/path" )
@@ -125,7 +127,30 @@ def test_load_toml_file_from_custom_dir(self):
125127 projectName = "Custom Path Project"
126128 """ ,
127129 )
128- properties = pyproject_toml .load (Path ("custom/path" ))
130+ properties = TomlConfigurationLoader .load (Path ("custom/path" ))
131+
132+ self .assertEqual (properties .sonar_properties .get ("sonar.projectKey" ), "custom-path-project" )
133+ self .assertEqual (properties .sonar_properties .get ("sonar.projectName" ), "Custom Path Project" )
134+
135+ def test_load_toml_file_project_content (self ):
136+ self .fs .create_file (
137+ "pyproject.toml" ,
138+ contents = (
139+ """
140+ [project]
141+ name = "My Overridden Project Name"
142+ description = "My Project Description"
143+ requires-python = ["3.6", "3.7", "3.8"]
144+ [tool.sonar]
145+ project-key = "my-project"
146+ project-name = "My Project"
147+ """
148+ ),
149+ )
150+ properties = TomlConfigurationLoader .load (Path ("." ))
129151
130- self .assertEqual (properties .get ("sonar.projectKey" ), "custom-path-project" )
131- self .assertEqual (properties .get ("sonar.projectName" ), "Custom Path Project" )
152+ self .assertEqual (properties .sonar_properties .get ("sonar.projectKey" ), "my-project" )
153+ self .assertEqual (properties .sonar_properties .get ("sonar.projectName" ), "My Project" )
154+ self .assertEqual (properties .project_properties .get ("sonar.projectName" ), "My Overridden Project Name" )
155+ self .assertEqual (properties .project_properties .get ("sonar.projectDescription" ), "My Project Description" )
156+ self .assertEqual (properties .project_properties .get ("sonar.python.version" ), "3.6,3.7,3.8" )
0 commit comments