17
17
# along with this program; if not, write to the Free Software Foundation,
18
18
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
#
20
+ import os
20
21
import unittest
21
- from unittest .mock import patch
22
+ from unittest .mock import patch , Mock
22
23
from py_sonar_scanner .configuration import Configuration
23
24
25
+ CURRENT_DIR = os .path .dirname (os .path .abspath (__file__ ))
26
+
24
27
25
28
class TestConfiguration (unittest .TestCase ):
26
29
@patch ("py_sonar_scanner.configuration.sys" )
@@ -47,16 +50,17 @@ def test_argument_parsing(self, mock_sys):
47
50
configuration .scan_arguments , ["-DSomeJVMArg" , "-DAnotherJVMArg" , "-dNotAJVMArg" , "-SomeNonsense" ]
48
51
)
49
52
50
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dtoml.path=tests /resources/pyproject.toml" ]
53
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f "-Dtoml.path={ CURRENT_DIR } /resources/pyproject.toml" ]
51
54
configuration .setup ()
52
55
self .assertListEqual (
53
- configuration .scan_arguments , ["-Dtoml.path=tests/resources/pyproject.toml" , "-Dsonar.a=b" , "-Dsonar.c=d" ]
56
+ configuration .scan_arguments ,
57
+ [f"-Dtoml.path={ CURRENT_DIR } /resources/pyproject.toml" , "-Dsonar.a=b" , "-Dsonar.c=d" ],
54
58
)
55
59
56
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dproject.home=tests /resources/" ]
60
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f "-Dproject.home={ CURRENT_DIR } /resources/" ]
57
61
configuration .setup ()
58
62
self .assertListEqual (
59
- configuration .scan_arguments , ["-Dproject.home=tests /resources/" , "-Dsonar.a=b" , "-Dsonar.c=d" ]
63
+ configuration .scan_arguments , [f "-Dproject.home={ CURRENT_DIR } /resources/" , "-Dsonar.a=b" , "-Dsonar.c=d" ]
60
64
)
61
65
62
66
mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dproject.home=tests2" ]
@@ -66,3 +70,90 @@ def test_argument_parsing(self, mock_sys):
66
70
mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dproject.home=tests=2" ]
67
71
configuration .setup ()
68
72
self .assertListEqual (configuration .scan_arguments , ["-Dproject.home=tests=2" ])
73
+
74
+ @patch ("py_sonar_scanner.configuration.sys" )
75
+ def test_dict_with_no_valid_values (self , mock_sys ):
76
+ configuration = Configuration ()
77
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" ]
78
+
79
+ test_dict = {}
80
+ configuration ._read_toml_file = Mock (return_value = test_dict )
81
+ configuration .setup ()
82
+ self .assertListEqual (configuration .scan_arguments , [])
83
+
84
+ test_dict = {"tool" : "some_property" }
85
+ configuration ._read_toml_file = Mock (return_value = test_dict )
86
+ configuration .setup ()
87
+ self .assertListEqual (configuration .scan_arguments , [])
88
+
89
+ test_dict = {"tool" : {"sonar" : "some_property" }}
90
+ configuration ._read_toml_file = Mock (return_value = test_dict )
91
+ configuration .setup ()
92
+ self .assertListEqual (configuration .scan_arguments , [])
93
+
94
+ @patch ("py_sonar_scanner.configuration.sys" )
95
+ def test_dict_with_valid_values (self , mock_sys ):
96
+ configuration = Configuration ()
97
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" ]
98
+
99
+ test_dict = {"tool" : {"sonar" : {"property1" : "value1" }}}
100
+ configuration ._read_toml_file = Mock (return_value = test_dict )
101
+ configuration .setup ()
102
+ self .assertListEqual (configuration .scan_arguments , ["-Dsonar.property1=value1" ])
103
+
104
+ test_dict = {"tool" : {"sonar" : {"property1" : "value1" , "property2" : "value2" }}}
105
+ configuration ._read_toml_file = Mock (return_value = test_dict )
106
+ configuration .setup ()
107
+ self .assertListEqual (configuration .scan_arguments , ["-Dsonar.property1=value1" , "-Dsonar.property2=value2" ])
108
+
109
+ test_dict = {"tool" : {"sonar" : {"property_class" : {"property1" : "value1" }}}}
110
+ configuration ._read_toml_file = Mock (return_value = test_dict )
111
+ configuration .setup ()
112
+ self .assertListEqual (configuration .scan_arguments , ["-Dsonar.property_class.property1=value1" ])
113
+
114
+ test_dict = {"tool" : {"sonar" : {"property1" : "value1" , "property_class" : {"sub_property" : "sub_value" }}}}
115
+ configuration ._read_toml_file = Mock (return_value = test_dict )
116
+ configuration .setup ()
117
+ self .assertListEqual (
118
+ configuration .scan_arguments , ["-Dsonar.property1=value1" , "-Dsonar.property_class.sub_property=sub_value" ]
119
+ )
120
+
121
+ @patch ("py_sonar_scanner.configuration.sys" )
122
+ def test_toml_with_valid_values (self , mock_sys ):
123
+ configuration = Configuration ()
124
+ toml_file_path = os .path .join (CURRENT_DIR , "resources" , "test_toml_file.toml" )
125
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dtoml.path={ toml_file_path } " ]
126
+ configuration .setup ()
127
+ self .assertListEqual (
128
+ configuration .scan_arguments ,
129
+ [
130
+ f"-Dtoml.path={ CURRENT_DIR } /resources/test_toml_file.toml" ,
131
+ "-Dsonar.property1=value1" ,
132
+ "-Dsonar.property2=value2" ,
133
+ "-Dsonar.property_class.property1=value1" ,
134
+ ],
135
+ )
136
+
137
+ @patch ("py_sonar_scanner.configuration.ApplicationLogger.get_logger" )
138
+ @patch ("builtins.open" )
139
+ @patch ("py_sonar_scanner.configuration.sys" )
140
+ def test_error_while_reading_toml_file (self , mock_sys , mock_open , mock_logger ):
141
+ toml_file_path = os .path .join (CURRENT_DIR , "resources" , "test_toml_file.toml" )
142
+ mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dtoml.path={ toml_file_path } " ]
143
+
144
+ mock_open .side_effect = OSError ("Test error while opening file." )
145
+
146
+ mock_logger_instance = Mock ()
147
+ mock_logger_instance .error = Mock ()
148
+ mock_logger .return_value = mock_logger_instance
149
+
150
+ configuration = Configuration ()
151
+ configuration .setup ()
152
+
153
+ self .assertListEqual (
154
+ configuration .scan_arguments ,
155
+ [
156
+ f"-Dtoml.path={ CURRENT_DIR } /resources/test_toml_file.toml" ,
157
+ ],
158
+ )
159
+ mock_logger_instance .error .assert_called_once_with ("Test error while opening file." )
0 commit comments