22
22
import pathlib
23
23
import unittest
24
24
from subprocess import PIPE
25
- from unittest .mock import Mock
26
- from unittest .mock import patch , MagicMock
25
+ from unittest .mock import MagicMock , Mock , patch
27
26
28
27
import pyfakefs .fake_filesystem_unittest as pyfakefs
29
28
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
+ )
33
34
from pysonar_scanner .exceptions import ChecksumException
34
35
from pysonar_scanner .scannerengine import (
35
36
LogLine ,
@@ -48,7 +49,10 @@ def test_without_stacktrace(self):
48
49
def test_with_stacktrace (self ):
49
50
line = '{"level":"INFO","message":"a message", "stacktrace":"a stacktrace"}'
50
51
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
+ )
52
56
53
57
def test_invalid_json (self ):
54
58
line = '"level":"INFO","message":"a message", "stacktrace":"a stacktrace"}'
@@ -62,7 +66,6 @@ def test_no_level(self):
62
66
63
67
64
68
class TestCmdExecutor (unittest .TestCase ):
65
-
66
69
@patch ("pysonar_scanner.scannerengine.Popen" )
67
70
def test_execute_successful (self , mock_popen ):
68
71
mock_process = MagicMock ()
@@ -119,11 +122,17 @@ def test_to_logging_level(self):
119
122
self .assertEqual (LogLine (level = "UNKNOWN" , message = "" ).get_logging_level (), logging .INFO )
120
123
121
124
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
+ ):
123
129
scannerengine .default_log_line_listener (LogLine (level = "INFO" , message = "info1" , stacktrace = None ))
124
130
self .assertEqual (logs .output , ["INFO:root:info1" ])
125
131
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
+ ):
127
136
default_log_line_listener (LogLine (level = "WARN" , message = "info2" , stacktrace = "a stacktrace" ))
128
137
self .assertEqual (logs .output , ["WARNING:root:info2" , "WARNING:root:a stacktrace" ])
129
138
@@ -161,7 +170,8 @@ def test_command_building(self, execute_mock):
161
170
scannerengine .ScannerEngine (jre_resolve_path_mock , scanner_engine_mock ).run (config )
162
171
163
172
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 ,
165
175
)
166
176
167
177
@patch ("pysonar_scanner.scannerengine.CmdExecutor" )
@@ -280,6 +290,61 @@ def test_java_opts_edge_cases(self, execute_mock):
280
290
self .assertEqual (actual_command [- 2 ], "-jar" )
281
291
self .assertEqual (actual_command [- 1 ], str (scanner_engine_mock ))
282
292
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
+
283
348
284
349
class TestScannerEngineProvisioner (pyfakefs .TestCase ):
285
350
def setUp (self ):
@@ -304,7 +369,9 @@ def test_happy_path(self):
304
369
def test_happy_path_with_download_url (self ):
305
370
with sq_api_utils .sq_api_mocker () as mocker :
306
371
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" ,
308
375
)
309
376
mocker .mock_download_url (url = "http://example.com" , body = self .test_file_content )
310
377
@@ -331,7 +398,10 @@ def test_scanner_engine_is_cached(self):
331
398
self .assertEqual (engine_download_rsps .call_count , 0 )
332
399
333
400
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
+ ):
335
405
mocker .mock_analysis_engine (filename = "scanner-engine.jar" , sha256 = "invalid-checksum" )
336
406
mocker .mock_analysis_engine_download (body = self .test_file_content )
337
407
0 commit comments