24
24
from py_sonar_scanner .logger import ApplicationLogger
25
25
26
26
CURRENT_DIR = os .path .dirname (os .path .abspath (__file__ ))
27
+ TEST_TOML_FILE = "test_toml_file.toml"
28
+ SAMPLE_SCANNER_PATH = "path/to/scanner/py-sonar-scanner"
27
29
28
30
29
31
class TestConfiguration (unittest .TestCase ):
@@ -32,16 +34,16 @@ def test_argument_parsing(self, mock_sys):
32
34
configuration = Configuration ()
33
35
self .assertFalse (configuration .is_debug ())
34
36
35
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" ]
37
+ mock_sys .argv = [SAMPLE_SCANNER_PATH ]
36
38
configuration .setup ()
37
39
self .assertListEqual (configuration .scan_arguments , [])
38
40
39
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-DSomeJVMArg" ]
41
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , "-DSomeJVMArg" ]
40
42
configuration .setup ()
41
43
self .assertListEqual (configuration .scan_arguments , ["-DSomeJVMArg" ])
42
44
43
45
mock_sys .argv = [
44
- "path/to/scanner/py-sonar-scanner" ,
46
+ SAMPLE_SCANNER_PATH ,
45
47
"-DSomeJVMArg" ,
46
48
"-DAnotherJVMArg" ,
47
49
"-dNotAJVMArg" ,
@@ -52,35 +54,35 @@ def test_argument_parsing(self, mock_sys):
52
54
configuration .scan_arguments , ["-DSomeJVMArg" , "-DAnotherJVMArg" , "-dNotAJVMArg" , "-SomeNonsense" ]
53
55
)
54
56
55
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dtoml.path={ CURRENT_DIR } /resources/pyproject.toml" ]
57
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , f"-Dtoml.path={ CURRENT_DIR } /resources/pyproject.toml" ]
56
58
configuration .setup ()
57
59
self .assertListEqual (
58
60
configuration .scan_arguments ,
59
- [f"-Dtoml.path= { CURRENT_DIR } /resources/pyproject.toml " , "-Dsonar.a=b " , "-Dsonar.c=d " ],
61
+ ["-Dsonar.a=b " , "-Dsonar.c=d " , f"-Dtoml.path= { CURRENT_DIR } /resources/pyproject.toml " ],
60
62
)
61
63
62
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dproject.home={ CURRENT_DIR } /resources/" ]
64
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , f"-Dproject.home={ CURRENT_DIR } /resources/" ]
63
65
configuration .setup ()
64
66
self .assertListEqual (
65
- configuration .scan_arguments , [f"-Dproject.home= { CURRENT_DIR } /resources/ " , "-Dsonar.a=b " , "-Dsonar.c=d " ]
67
+ configuration .scan_arguments , ["-Dsonar.a=b " , "-Dsonar.c=d " , f"-Dproject.home= { CURRENT_DIR } /resources/ " ]
66
68
)
67
69
68
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dproject.home=tests2" ]
70
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , "-Dproject.home=tests2" ]
69
71
configuration .setup ()
70
72
self .assertListEqual (configuration .scan_arguments , ["-Dproject.home=tests2" ])
71
73
72
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-Dproject.home=tests=2" ]
74
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , "-Dproject.home=tests=2" ]
73
75
configuration .setup ()
74
76
self .assertListEqual (configuration .scan_arguments , ["-Dproject.home=tests=2" ])
75
77
76
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , "-X" ]
78
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , "-X" ]
77
79
configuration .setup ()
78
80
self .assertTrue (configuration .is_debug ())
79
81
80
82
@patch ("py_sonar_scanner.configuration.sys" )
81
83
def test_dict_with_no_valid_values (self , mock_sys ):
82
84
configuration = Configuration ()
83
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" ]
85
+ mock_sys .argv = [SAMPLE_SCANNER_PATH ]
84
86
85
87
test_dict = {}
86
88
configuration ._read_toml_file = Mock (return_value = test_dict )
@@ -100,7 +102,7 @@ def test_dict_with_no_valid_values(self, mock_sys):
100
102
@patch ("py_sonar_scanner.configuration.sys" )
101
103
def test_dict_with_valid_values (self , mock_sys ):
102
104
configuration = Configuration ()
103
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" ]
105
+ mock_sys .argv = [SAMPLE_SCANNER_PATH ]
104
106
105
107
test_dict = {"tool" : {"sonar" : {"property1" : "value1" }}}
106
108
configuration ._read_toml_file = Mock (return_value = test_dict )
@@ -127,23 +129,40 @@ def test_dict_with_valid_values(self, mock_sys):
127
129
@patch ("py_sonar_scanner.configuration.sys" )
128
130
def test_toml_with_valid_values (self , mock_sys ):
129
131
configuration = Configuration ()
130
- toml_file_path = os .path .join (CURRENT_DIR , "resources" , "test_toml_file.toml" )
131
- mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dtoml.path={ toml_file_path } " ]
132
+ toml_file_path = os .path .join (CURRENT_DIR , "resources" , TEST_TOML_FILE )
133
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , f"-Dtoml.path={ toml_file_path } " ]
132
134
configuration .setup ()
133
135
self .assertListEqual (
134
136
configuration .scan_arguments ,
135
137
[
138
+ "-Dsonar.property1=value1" ,
139
+ "-Dsonar.property2=value2" ,
140
+ "-Dsonar.property_class.property1=value1" ,
136
141
f"-Dtoml.path={ CURRENT_DIR } /resources/test_toml_file.toml" ,
142
+ ],
143
+ )
144
+
145
+ @patch ("py_sonar_scanner.configuration.sys" )
146
+ def test_duplicate_values_toml_cli (self , mock_sys ):
147
+ configuration = Configuration ()
148
+ toml_file_path = os .path .join (CURRENT_DIR , "resources" , TEST_TOML_FILE )
149
+ mock_sys .argv = [SAMPLE_SCANNER_PATH , f"-Dtoml.path={ toml_file_path } " , "-Dsonar.property1=value1" ]
150
+ configuration .setup ()
151
+ self .assertListEqual (
152
+ configuration .scan_arguments ,
153
+ [
137
154
"-Dsonar.property1=value1" ,
138
155
"-Dsonar.property2=value2" ,
139
156
"-Dsonar.property_class.property1=value1" ,
157
+ f"-Dtoml.path={ CURRENT_DIR } /resources/test_toml_file.toml" ,
158
+ "-Dsonar.property1=value1" ,
140
159
],
141
160
)
142
161
143
162
@patch ("builtins.open" )
144
163
@patch ("py_sonar_scanner.configuration.sys" )
145
164
def test_error_while_reading_toml_file (self , mock_sys , mock_open ):
146
- toml_file_path = os .path .join (CURRENT_DIR , "resources" , "test_toml_file.toml" )
165
+ toml_file_path = os .path .join (CURRENT_DIR , "resources" , TEST_TOML_FILE )
147
166
mock_sys .argv = ["path/to/scanner/py-sonar-scanner" , f"-Dtoml.path={ toml_file_path } " ]
148
167
149
168
mock_open .side_effect = OSError ("Test error while opening file." )
0 commit comments