4444 TOML_PATH ,
4545 SONAR_PROJECT_DESCRIPTION ,
4646 SONAR_PYTHON_VERSION ,
47+ SONAR_HOST_URL ,
48+ SONAR_SCANNER_JAVA_OPTS ,
4749)
4850from pysonar_scanner .configuration .configuration_loader import ConfigurationLoader , SONAR_PROJECT_BASE_DIR
4951from pysonar_scanner .exceptions import MissingKeyException
@@ -53,6 +55,8 @@ class TestConfigurationLoader(pyfakefs.TestCase):
5355 def setUp (self ):
5456 self .maxDiff = None
5557 self .setUpPyfakefs ()
58+ self .env_patcher = patch .dict ("os.environ" , {}, clear = True )
59+ self .env_patcher .start ()
5660
5761 @patch ("sys.argv" , ["myscript.py" , "--token" , "myToken" , "--sonar-project-key" , "myProjectKey" ])
5862 def test_defaults (self ):
@@ -265,6 +269,19 @@ def test_load_pyproject_toml_from_toml_path(self):
265269 }
266270 self .assertDictEqual (configuration , expected_configuration )
267271
272+ @patch ("sys.argv" , ["myscript.py" ])
273+ @patch .dict ("os.environ" , {"SONAR_TOKEN" : "TokenFromEnv" , "SONAR_PROJECT_KEY" : "KeyFromEnv" }, clear = True )
274+ def test_load_from_env_variables_only (self ):
275+ """Test that configuration can be loaded exclusively from environment variables"""
276+ configuration = ConfigurationLoader .load ()
277+
278+ # Check that environment variables are loaded correctly
279+ self .assertEqual (configuration [SONAR_TOKEN ], "TokenFromEnv" )
280+ self .assertEqual (configuration [SONAR_PROJECT_KEY ], "KeyFromEnv" )
281+
282+ # Default values should still be populated
283+ self .assertEqual (configuration [SONAR_SCANNER_APP ], "python" )
284+
268285 @patch (
269286 "sys.argv" ,
270287 [
@@ -275,18 +292,38 @@ def test_load_pyproject_toml_from_toml_path(self):
275292 "ProjectKeyFromCLI" ,
276293 ],
277294 )
278- def test_properties_and_toml_priority (self ):
279- """Test that sonar-project.properties has priority over pyproject.toml when both exist"""
295+ @patch .dict (
296+ "os.environ" ,
297+ {
298+ "SONAR_TOKEN" : "TokenFromEnv" , # Should be overridden by CLI
299+ "SONAR_HOST_URL" : "https://sonar.env.example.com" , # Not set elsewhere, should be used
300+ "SONAR_USER_HOME" : "/env/sonar/home" , # Should be used (overriding sonar-project.properties)
301+ "SONAR_SCANNER_JAVA_OPTS" : "-Xmx2048m" , # Unique to env vars
302+ },
303+ clear = True ,
304+ )
305+ def test_properties_priority (self ):
306+ """Test the priority order of different configuration sources:
307+ 1. CLI args (highest)
308+ 2. Environment variables
309+ 3. Generic environment variable
310+ 4. pyproject.toml [tool.sonar] section
311+ 5. sonar-project.properties
312+ 6. Generic properties from pyproject.toml [project] section
313+ 7. Default values (lowest)
314+ """
280315 # Create both configuration files
281316 self .fs .create_file (
282317 "sonar-project.properties" ,
283318 contents = (
284319 """
285320 sonar.projectKey=ProjectKeyFromProperties
286321 sonar.projectName=Properties Project
322+ sonar.projectDescription=Properties Project Description
287323 sonar.sources=src/properties
288324 sonar.tests=test/properties
289325 sonar.exclusions=properties-exclusions/**/*
326+ sonar.userHome=/properties/sonar/home
290327 """
291328 ),
292329 )
@@ -303,23 +340,35 @@ def test_properties_and_toml_priority(self):
303340 project-name = "TOML Project"
304341 sources = "src/toml"
305342 exclusions = "toml-exclusions/**/*"
343+ userHome = "/toml/sonar/home"
306344 """
307345 ),
308346 )
309347
310348 configuration = ConfigurationLoader .load ()
311349
312- # Generic pyproject.toml properties are retrieved when no other source is available
313- self .assertEqual (configuration [SONAR_PROJECT_DESCRIPTION ], "My Project Description" )
350+ # Test Default values (lowest priority)
351+ self .assertNotEqual (configuration [SONAR_SCANNER_CONNECT_TIMEOUT ], "5" ) # Default value would be 5
352+
353+ # Generic pyproject.toml properties from [project] section
314354 self .assertEqual (configuration [SONAR_PYTHON_VERSION ], "3.6,3.7,3.8" )
315355
316- # sonar-project.properties values have priority over generic properties from pyproject.toml
317- self .assertEqual (configuration [SONAR_PROJECT_NAME ], "TOML Project" )
356+ # sonar-project.properties values
357+ self .assertEqual (configuration [SONAR_TESTS ], "test/properties" )
358+ self .assertEqual (
359+ configuration [SONAR_PROJECT_DESCRIPTION ], "Properties Project Description"
360+ ) # Overrides [project] toml
318361
319- # Sonar pyproject.toml values have priority over sonar-project.properties
362+ # pyproject.toml [tool.sonar] section overrides sonar-project.properties
320363 self .assertEqual (configuration [SONAR_SOURCES ], "src/toml" )
321364 self .assertEqual (configuration [SONAR_EXCLUSIONS ], "toml-exclusions/**/*" )
365+ self .assertEqual (configuration [SONAR_PROJECT_NAME ], "TOML Project" ) # Overrides sonar-project.properties
322366
323- # CLI args still have highest priority
367+ # Environment variables override pyproject.toml [tool.sonar]
368+ self .assertEqual (configuration [SONAR_HOST_URL ], "https://sonar.env.example.com" )
369+ self .assertEqual (configuration [SONAR_SCANNER_JAVA_OPTS ], "-Xmx2048m" )
370+ self .assertEqual (configuration [SONAR_USER_HOME ], "/env/sonar/home" ) # Env var overrides [sonar] toml
371+
372+ # CLI args have highest priority
324373 self .assertEqual (configuration [SONAR_PROJECT_KEY ], "ProjectKeyFromCLI" )
325- self .assertEqual (configuration [SONAR_TESTS ], "test/properties" )
374+ self .assertEqual (configuration [SONAR_TOKEN ], "myToken" ) # CLI overrides env var
0 commit comments