@@ -455,6 +455,24 @@ def test_explicit_sources_overrides_default(self, mock_get_os, mock_get_arch):
455455 configuration = ConfigurationLoader .load ()
456456 self .assertEqual (configuration [SONAR_SOURCES ], "src" )
457457
458+ @patch ("sys.argv" , ["myscript.py" ])
459+ def test_default_sources_adds_test_dirs_to_exclusions (self , mock_get_os , mock_get_arch ):
460+ """When sonar.sources defaults to '.' and sonar.tests is set, test dirs must be excluded from sources."""
461+ self .fs .create_dir ("tests" )
462+ configuration = ConfigurationLoader .load ()
463+ self .assertEqual (configuration [SONAR_SOURCES ], "." )
464+ self .assertEqual (configuration [SONAR_TESTS ], "tests" )
465+ self .assertEqual (configuration [SONAR_EXCLUSIONS ], "tests/**" )
466+
467+ @patch ("sys.argv" , ["myscript.py" ])
468+ def test_default_sources_appends_test_dirs_to_existing_exclusions (self , mock_get_os , mock_get_arch ):
469+ """When sonar.exclusions is already set, test dirs are appended rather than replacing it."""
470+ self .fs .create_dir ("tests" )
471+ self .fs .create_file ("sonar-project.properties" , contents = "sonar.exclusions=generated/**\n " )
472+ configuration = ConfigurationLoader .load ()
473+ self .assertEqual (configuration [SONAR_SOURCES ], "." )
474+ self .assertEqual (configuration [SONAR_EXCLUSIONS ], "generated/**,tests/**" )
475+
458476 @patch ("sys.argv" , ["myscript.py" ])
459477 def test_sonar_project_properties_sonar_tests_overrides_auto_detection (self , mock_get_os , mock_get_arch ):
460478 """sonar.tests in sonar-project.properties must override auto-detected value from filesystem/pytest config."""
0 commit comments