18
18
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
#
20
20
import unittest
21
- from unittest .mock import Mock
21
+ from unittest .mock import Mock , patch , call
22
22
import threading
23
23
from py_sonar_scanner .scanner import Scanner
24
24
from py_sonar_scanner .configuration import Configuration
@@ -31,13 +31,13 @@ def test_scanner_compute_command(self):
31
31
cfg .scan_arguments = ["a" , "b" ]
32
32
scanner = Scanner (cfg )
33
33
34
- assert scanner .compute_command () == ["test" , "a" , "b" ]
34
+ self . assertEqual ( scanner .compute_command (), ["test" , "a" , "b" ])
35
35
36
36
cfg .sonar_scanner_executable_path = "test"
37
37
cfg .scan_arguments = []
38
38
scanner = Scanner (cfg )
39
39
40
- assert scanner .compute_command () == ["test" ]
40
+ self . assertEqual ( scanner .compute_command (), ["test" ])
41
41
42
42
cfg .sonar_scanner_executable_path = ""
43
43
cfg .scan_arguments = []
@@ -49,8 +49,8 @@ def test_process_output(self):
49
49
scanner = Scanner (Configuration ())
50
50
output_thread = threading .Thread ()
51
51
error_thread = threading .Thread ()
52
-
53
- process = Mock ()
52
+ success_code = 0
53
+ process = Mock (returncode = success_code )
54
54
output_thread .start = Mock ()
55
55
error_thread .start = Mock ()
56
56
output_thread .join = Mock ()
@@ -64,14 +64,48 @@ def test_process_output(self):
64
64
output_thread .join .assert_called_once ()
65
65
error_thread .join .assert_called_once ()
66
66
process .wait .assert_called_once ()
67
+ self .assertEqual (return_code , success_code )
67
68
68
- def test_scan (self ):
69
+ @patch ("py_sonar_scanner.scanner.Thread" )
70
+ def test_scan (self , mock_thread ):
69
71
scanner = Scanner (Configuration ())
70
-
71
- scanner .execute_command = Mock ()
72
+ process = Mock ()
73
+ scanner .execute_command = Mock (return_value = process )
72
74
scanner .process_output = Mock ()
73
75
74
76
scanner .scan ()
75
77
76
78
scanner .execute_command .assert_called_once ()
79
+ call_thread_stdout = call (target = scanner ._log_output , args = (process .stdout ,))
80
+ call_thread_stderr = call (target = scanner ._log_output , args = (process .stderr ,))
81
+ mock_thread .assert_has_calls ([call_thread_stdout , call_thread_stderr ])
82
+ self .assertEqual (mock_thread .call_count , 2 )
77
83
scanner .process_output .assert_called_once ()
84
+
85
+ @patch ("py_sonar_scanner.scanner.Popen" )
86
+ def test_execute_command (self , mock_popen ):
87
+ from subprocess import PIPE
88
+
89
+ scanner = Scanner (Configuration ())
90
+ command = "test"
91
+ scanner .compute_command = Mock (return_value = command )
92
+
93
+ scanner .execute_command ()
94
+
95
+ scanner .compute_command .assert_called_once ()
96
+ mock_popen .assert_called_once_with (command , stdout = PIPE , stderr = PIPE )
97
+
98
+ def test_log_output (self ):
99
+ scanner = Scanner (Configuration ())
100
+ input_lines = [
101
+ bytes ("test\n " , encoding = "utf-8" ),
102
+ bytes ("\n other line\n \n " , encoding = "utf-8" ),
103
+ bytes ("" , encoding = "utf-8" ),
104
+ bytes ("last \n line" , encoding = "utf-8" ),
105
+ ]
106
+ with self .assertLogs (scanner .log ) as log :
107
+ scanner ._log_output (input_lines )
108
+ self .assertEqual (log .records [0 ].getMessage (), "test" )
109
+ self .assertEqual (log .records [1 ].getMessage (), "\n other line" )
110
+ self .assertEqual (log .records [2 ].getMessage (), "" )
111
+ self .assertEqual (log .records [3 ].getMessage (), "last \n line" )
0 commit comments