2222import pathlib
2323import unittest
2424from subprocess import PIPE
25- from unittest .mock import Mock
26- from unittest .mock import patch , MagicMock
25+ from unittest .mock import MagicMock , Mock , patch
2726
2827import pyfakefs .fake_filesystem_unittest as pyfakefs
2928
30- from pysonar_scanner import cache
31- from pysonar_scanner import scannerengine
32- from pysonar_scanner .configuration .properties import SONAR_SCANNER_JAVA_OPTS
29+ from pysonar_scanner import cache , scannerengine
30+ from pysonar_scanner .configuration .properties import (
31+ SONAR_SCANNER_JAVA_OPTS ,
32+ SONAR_SCANNER_OPTS ,
33+ )
3334from pysonar_scanner .exceptions import ChecksumException
3435from pysonar_scanner .scannerengine import (
3536 LogLine ,
@@ -48,7 +49,10 @@ def test_without_stacktrace(self):
4849 def test_with_stacktrace (self ):
4950 line = '{"level":"INFO","message":"a message", "stacktrace":"a stacktrace"}'
5051 log_line = scannerengine .parse_log_line (line )
51- self .assertEqual (log_line , LogLine (level = "INFO" , message = "a message" , stacktrace = "a stacktrace" ))
52+ self .assertEqual (
53+ log_line ,
54+ LogLine (level = "INFO" , message = "a message" , stacktrace = "a stacktrace" ),
55+ )
5256
5357 def test_invalid_json (self ):
5458 line = '"level":"INFO","message":"a message", "stacktrace":"a stacktrace"}'
@@ -62,7 +66,6 @@ def test_no_level(self):
6266
6367
6468class TestCmdExecutor (unittest .TestCase ):
65-
6669 @patch ("pysonar_scanner.scannerengine.Popen" )
6770 def test_execute_successful (self , mock_popen ):
6871 mock_process = MagicMock ()
@@ -119,11 +122,17 @@ def test_to_logging_level(self):
119122 self .assertEqual (LogLine (level = "UNKNOWN" , message = "" ).get_logging_level (), logging .INFO )
120123
121124 def test_default_log_line_listener (self ):
122- with self .subTest ("log line without stacktrace" ), self .assertLogs (level = "INFO" ) as logs :
125+ with (
126+ self .subTest ("log line without stacktrace" ),
127+ self .assertLogs (level = "INFO" ) as logs ,
128+ ):
123129 scannerengine .default_log_line_listener (LogLine (level = "INFO" , message = "info1" , stacktrace = None ))
124130 self .assertEqual (logs .output , ["INFO:root:info1" ])
125131
126- with self .subTest ("log line with stacktrace" ), self .assertLogs (level = "INFO" ) as logs :
132+ with (
133+ self .subTest ("log line with stacktrace" ),
134+ self .assertLogs (level = "INFO" ) as logs ,
135+ ):
127136 default_log_line_listener (LogLine (level = "WARN" , message = "info2" , stacktrace = "a stacktrace" ))
128137 self .assertEqual (logs .output , ["WARNING:root:info2" , "WARNING:root:a stacktrace" ])
129138
@@ -161,7 +170,8 @@ def test_command_building(self, execute_mock):
161170 scannerengine .ScannerEngine (jre_resolve_path_mock , scanner_engine_mock ).run (config )
162171
163172 execute_mock .assert_called_once_with (
164- [str (java_path ), "-jar" , str (pathlib .Path ("/test/scanner-engine.jar" ))], expected_std_in
173+ [str (java_path ), "-jar" , str (pathlib .Path ("/test/scanner-engine.jar" ))],
174+ expected_std_in ,
165175 )
166176
167177 @patch ("pysonar_scanner.scannerengine.CmdExecutor" )
@@ -280,6 +290,61 @@ def test_java_opts_edge_cases(self, execute_mock):
280290 self .assertEqual (actual_command [- 2 ], "-jar" )
281291 self .assertEqual (actual_command [- 1 ], str (scanner_engine_mock ))
282292
293+ @patch ("pysonar_scanner.scannerengine.CmdExecutor" )
294+ def test_command_building_with_scanner_opts (self , execute_mock ):
295+ config = {
296+ "sonar.token" : "myToken" ,
297+ "sonar.projectKey" : "myProjectKey" ,
298+ SONAR_SCANNER_OPTS : "-Xmx1024m -XX:MaxPermSize=256m" ,
299+ }
300+
301+ java_path = pathlib .Path ("jre/bin/java" )
302+ jre_resolve_path_mock = Mock ()
303+ jre_resolve_path_mock .path = java_path
304+ scanner_engine_mock = pathlib .Path ("/test/scanner-engine.jar" )
305+
306+ scannerengine .ScannerEngine (jre_resolve_path_mock , scanner_engine_mock ).run (config )
307+
308+ called_args = execute_mock .call_args [0 ]
309+ actual_command = called_args [0 ]
310+
311+ expected_command = [
312+ str (java_path ),
313+ "-Xmx1024m" ,
314+ "-XX:MaxPermSize=256m" ,
315+ "-jar" ,
316+ str (scanner_engine_mock ),
317+ ]
318+ self .assertEqual (actual_command , expected_command )
319+
320+ @patch ("pysonar_scanner.scannerengine.CmdExecutor" )
321+ def test_command_ignore_scanner_opts_for_java_opts (self , execute_mock ):
322+ config = {
323+ "sonar.token" : "myToken" ,
324+ "sonar.projectKey" : "myProjectKey" ,
325+ SONAR_SCANNER_OPTS : "-Xmx512m -XX:MaxPermSize=128m" ,
326+ SONAR_SCANNER_JAVA_OPTS : "-Xmx1024m -XX:MaxPermSize=256m" ,
327+ }
328+
329+ java_path = pathlib .Path ("jre/bin/java" )
330+ jre_resolve_path_mock = Mock ()
331+ jre_resolve_path_mock .path = java_path
332+ scanner_engine_mock = pathlib .Path ("/test/scanner-engine.jar" )
333+
334+ scannerengine .ScannerEngine (jre_resolve_path_mock , scanner_engine_mock ).run (config )
335+
336+ called_args = execute_mock .call_args [0 ]
337+ actual_command = called_args [0 ]
338+
339+ expected_command = [
340+ str (java_path ),
341+ "-Xmx1024m" ,
342+ "-XX:MaxPermSize=256m" ,
343+ "-jar" ,
344+ str (scanner_engine_mock ),
345+ ]
346+ self .assertEqual (actual_command , expected_command )
347+
283348
284349class TestScannerEngineProvisioner (pyfakefs .TestCase ):
285350 def setUp (self ):
@@ -304,7 +369,9 @@ def test_happy_path(self):
304369 def test_happy_path_with_download_url (self ):
305370 with sq_api_utils .sq_api_mocker () as mocker :
306371 mocker .mock_analysis_engine (
307- filename = "scanner-engine.jar" , sha256 = self .test_file_checksum , download_url = "http://example.com"
372+ filename = "scanner-engine.jar" ,
373+ sha256 = self .test_file_checksum ,
374+ download_url = "http://example.com" ,
308375 )
309376 mocker .mock_download_url (url = "http://example.com" , body = self .test_file_content )
310377
@@ -331,7 +398,10 @@ def test_scanner_engine_is_cached(self):
331398 self .assertEqual (engine_download_rsps .call_count , 0 )
332399
333400 def test_checksum_is_invalid (self ):
334- with self .assertRaises (ChecksumException ), sq_api_utils .sq_api_mocker () as mocker :
401+ with (
402+ self .assertRaises (ChecksumException ),
403+ sq_api_utils .sq_api_mocker () as mocker ,
404+ ):
335405 mocker .mock_analysis_engine (filename = "scanner-engine.jar" , sha256 = "invalid-checksum" )
336406 mocker .mock_analysis_engine_download (body = self .test_file_content )
337407
0 commit comments