Skip to content

Commit 9dbdc69

Browse files
committed
SCANPY-178: The scanner should respect the SONAR_SCANNER_OPTS environment variable
1 parent 33f72bb commit 9dbdc69

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/pysonar_scanner/configuration/environment_variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ def load_properties_env_variables():
6363
env_var_name = prop.env_variable_name()
6464
if env_var_name in os.environ:
6565
properties[prop.name] = os.environ[env_var_name]
66+
if prop.deprecated:
67+
logging.warning(prop.deprecation_message)
6668
return properties

src/pysonar_scanner/configuration/properties.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@
101101
SONAR_PYTHON_RUFF_REPORT_PATHS = "sonar.python.ruff.reportPaths"
102102
TOML_PATH: Key = "toml-path"
103103

104+
# ============ DEPRECATED ==============
105+
SONAR_SCANNER_OPTS = "sonar.scanner.opts"
106+
104107

105108
@dataclass
106109
class Property:
@@ -112,6 +115,10 @@ class Property:
112115

113116
cli_getter: Optional[Callable[[argparse.Namespace], Any]] = None
114117
"""function to get the value from the CLI arguments namespace. If None, the property is not settable via CLI"""
118+
119+
deprecated: bool = False
120+
121+
deprecation_message: Optional[str] = None
115122

116123
def python_name(self) -> str:
117124
"""Convert Java-style camel case name to Python-style kebab-case name."""
@@ -524,6 +531,13 @@ def env_variable_name(self) -> str:
524531
name=SONAR_MODULES,
525532
default_value=None,
526533
cli_getter=lambda args: args.sonar_modules
534+
),
535+
Property(
536+
name=SONAR_SCANNER_OPTS,
537+
default_value=None,
538+
cli_getter=None,
539+
deprecated=True,
540+
deprecation_message="SONAR_SCANNER_OPTS is deprecated, please use SONAR_SCANNER_JAVA_OPTS instead."
527541
)
528542
]
529543
# fmt: on

tests/test_environment_variables.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
# along with this program; if not, write to the Free Software Foundation,
1818
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
#
20-
from json import JSONDecodeError
2120
import unittest
22-
from unittest.mock import MagicMock, patch
21+
from unittest.mock import patch
2322

2423
from pysonar_scanner.configuration import environment_variables
2524
from pysonar_scanner.configuration.properties import (
@@ -31,6 +30,7 @@
3130
SONAR_TOKEN,
3231
SONAR_USER_HOME,
3332
SONAR_PROJECT_KEY,
33+
SONAR_SCANNER_OPTS,
3434
)
3535

3636

@@ -134,3 +134,27 @@ def test_environment_variables_priority_over_json_params(self):
134134
}
135135
self.assertEqual(len(properties), 3)
136136
self.assertDictEqual(properties, expected_properties)
137+
138+
@patch("pysonar_scanner.configuration.environment_variables.logging")
139+
def test_SONAR_SCANNER_OPTS(self, mock_logging):
140+
env = {
141+
"SONAR_TOKEN": "my-token",
142+
"SONAR_HOST_URL": "https://sonarqube.example.com",
143+
"SONAR_USER_HOME": "/custom/sonar/home",
144+
"SONAR_SCANNER_OPTS": "-Xmx1024m -XX:MaxPermSize=256m",
145+
"SONAR_REGION": "us",
146+
}
147+
with patch.dict("os.environ", env, clear=True):
148+
properties = environment_variables.load()
149+
expected_properties = {
150+
SONAR_TOKEN: "my-token",
151+
SONAR_HOST_URL: "https://sonarqube.example.com",
152+
SONAR_USER_HOME: "/custom/sonar/home",
153+
SONAR_SCANNER_OPTS: "-Xmx1024m -XX:MaxPermSize=256m",
154+
SONAR_REGION: "us",
155+
}
156+
self.assertEqual(len(properties), 5)
157+
self.assertDictEqual(properties, expected_properties)
158+
mock_logging.warning.assert_called_once_with(
159+
"SONAR_SCANNER_OPTS is deprecated, please use SONAR_SCANNER_JAVA_OPTS instead.",
160+
)

0 commit comments

Comments
 (0)