Skip to content

Commit ca83884

Browse files
Disable test file inference when testFileHeuristic is disabled
1 parent e2380f2 commit ca83884

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

src/pysonar_scanner/configuration/configuration_loader.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ def load() -> dict[Key, Any]:
7373
resolved_properties.update(environment_variables.load())
7474
resolved_properties.update(cli_properties)
7575

76-
# Auto-detect sonar.tests only when the user has not set it in any higher-priority source.
77-
# Running python_project_loader unconditionally would emit confusing warnings about
78-
# pytest config even when the result would be discarded.
79-
if SONAR_TESTS not in resolved_properties:
76+
# Auto-detect sonar.tests only when the user has not set it in any higher-priority source
77+
# and has not explicitly disabled the sonar-python test file heuristic. When the heuristic
78+
# is disabled the intent is to analyse all files as main code with no test classification.
79+
heuristic_disabled = resolved_properties.get("sonar.python.testFileHeuristic.disabled", "").lower() == "true"
80+
if SONAR_TESTS not in resolved_properties and not heuristic_disabled:
8081
resolved_properties.update(test_paths_loader.load(base_dir))
8182

8283
return resolved_properties

tests/unit/test_configuration_loader.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,7 @@ def test_env_var_sonar_tests_overrides_auto_detection(self, mock_get_os, mock_ge
478478

479479
@patch("sys.argv", ["myscript.py"])
480480
@patch("pysonar_scanner.configuration.configuration_loader.test_paths_loader")
481-
def test_test_paths_loader_not_called_when_sonar_tests_already_set(
482-
self, mock_loader, mock_get_os, mock_get_arch
483-
):
481+
def test_test_paths_loader_not_called_when_sonar_tests_already_set(self, mock_loader, mock_get_os, mock_get_arch):
484482
"""test_paths_loader must not run at all when sonar.tests is already set — avoids spurious warnings."""
485483
self.fs.create_file(
486484
"sonar-project.properties",
@@ -489,6 +487,18 @@ def test_test_paths_loader_not_called_when_sonar_tests_already_set(
489487
ConfigurationLoader.load()
490488
mock_loader.load.assert_not_called()
491489

490+
@patch("sys.argv", ["myscript.py"])
491+
@patch("pysonar_scanner.configuration.configuration_loader.test_paths_loader")
492+
def test_test_paths_loader_not_called_when_heuristic_disabled(self, mock_loader, mock_get_os, mock_get_arch):
493+
"""test_paths_loader must not run when sonar.python.testFileHeuristic.disabled=true."""
494+
self.fs.create_file(
495+
"sonar-project.properties",
496+
contents="sonar.python.testFileHeuristic.disabled=true\n",
497+
)
498+
self.fs.create_dir("tests") # inference would find this if it ran
499+
ConfigurationLoader.load()
500+
mock_loader.load.assert_not_called()
501+
492502
@patch("sys.argv", ["myscript.py"])
493503
@patch.dict("os.environ", {"SONAR_TOKEN": "TokenFromEnv", "SONAR_PROJECT_KEY": "KeyFromEnv"}, clear=True)
494504
def test_load_from_env_variables_only(self, mock_get_os, mock_get_arch):

tests/unit/test_test_paths_loader.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,7 @@ def test_load_from_pytest_ini(self):
132132
def test_load_from_pytest_ini_multiline(self):
133133
self.fs.create_file(
134134
"pytest.ini",
135-
contents=(
136-
"[pytest]\n"
137-
"testpaths =\n"
138-
" tests\n"
139-
" integration\n"
140-
),
135+
contents=("[pytest]\n" "testpaths =\n" " tests\n" " integration\n"),
141136
)
142137
self.fs.create_dir("tests")
143138
self.fs.create_dir("integration")
@@ -185,12 +180,7 @@ def test_load_from_tox_ini_multiple_paths(self):
185180
def test_load_from_tox_ini_multiline(self):
186181
self.fs.create_file(
187182
"tox.ini",
188-
contents=(
189-
"[pytest]\n"
190-
"testpaths =\n"
191-
" tests\n"
192-
" integration\n"
193-
),
183+
contents=("[pytest]\n" "testpaths =\n" " tests\n" " integration\n"),
194184
)
195185
self.fs.create_dir("tests")
196186
self.fs.create_dir("integration")

0 commit comments

Comments
 (0)