Skip to content

Commit 290380d

Browse files
Ensure no overlap with auto-detected tests
1 parent 1a5a38c commit 290380d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/pysonar_scanner/configuration/configuration_loader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
SONAR_PROJECT_KEY,
2929
SONAR_TOKEN,
3030
SONAR_PROJECT_BASE_DIR,
31+
SONAR_EXCLUSIONS,
3132
SONAR_SOURCES,
3233
SONAR_TESTS,
3334
SONAR_PYTHON_TEST_FILE_HEURISTIC_DISABLED,
@@ -90,6 +91,15 @@ def load() -> dict[Key, Any]:
9091
if SONAR_SOURCES not in resolved_properties:
9192
logging.info("sonar.sources is not set; defaulting to the current directory '.'")
9293
resolved_properties[SONAR_SOURCES] = "."
94+
if SONAR_TESTS in resolved_properties:
95+
test_dirs = [d.strip() for d in resolved_properties[SONAR_TESTS].split(",") if d.strip()]
96+
test_exclusion_patterns = ",".join(f"{d}/**" for d in test_dirs)
97+
existing = resolved_properties.get(SONAR_EXCLUSIONS, "")
98+
resolved_properties[SONAR_EXCLUSIONS] = f"{existing},{test_exclusion_patterns}" if existing else test_exclusion_patterns
99+
logging.info(
100+
"sonar.sources defaults to '.'; adding test directories to sonar.exclusions to avoid overlap: '%s'",
101+
test_exclusion_patterns,
102+
)
93103

94104
return resolved_properties
95105

tests/unit/test_configuration_loader.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)