1717# along with this program; if not, write to the Free Software Foundation,
1818# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919#
20- import unittest
2120
2221from unittest .mock import patch
2322
24- from pysonar_scanner .configuration import ConfigurationLoader
23+ import pyfakefs .fake_filesystem_unittest as pyfakefs
24+
2525from pysonar_scanner import configuration
2626from pysonar_scanner .configuration .properties import (
2727 SONAR_PROJECT_KEY ,
28+ SONAR_PROJECT_NAME ,
2829 SONAR_SCANNER_APP ,
2930 SONAR_SCANNER_APP_VERSION ,
3031 SONAR_SCANNER_BOOTSTRAP_START_TIME ,
3435 SONAR_SCANNER_SKIP_JRE_PROVISIONING ,
3536 SONAR_SCANNER_SOCKET_TIMEOUT ,
3637 SONAR_SCANNER_TRUSTSTORE_PASSWORD ,
38+ SONAR_EXCLUSIONS ,
39+ SONAR_SOURCES ,
40+ SONAR_TESTS ,
3741 SONAR_TOKEN ,
3842 SONAR_USER_HOME ,
3943 SONAR_VERBOSE ,
4044)
45+ from pysonar_scanner .configuration import ConfigurationLoader , SONAR_PROJECT_BASE_DIR
4146from pysonar_scanner .exceptions import MissingKeyException
4247
4348
44- class TestConfigurationLoader (unittest .TestCase ):
49+ class TestConfigurationLoader (pyfakefs .TestCase ):
4550 def setUp (self ):
4651 self .maxDiff = None
52+ self .setUpPyfakefs ()
4753
4854 @patch ("sys.argv" , ["myscript.py" , "--token" , "myToken" , "--sonar-project-key" , "myProjectKey" ])
4955 def test_defaults (self ):
@@ -65,7 +71,7 @@ def test_defaults(self):
6571 }
6672 self .assertDictEqual (configuration , expected_configuration )
6773
68- @patch ("pysonar_scanner.configuration.get_static_default_properties" , result = {})
74+ @patch ("pysonar_scanner.configuration.get_static_default_properties" , return_value = {})
6975 @patch ("sys.argv" , ["myscript.py" ])
7076 def test_no_defaults_in_configuration_loaders (self , get_static_default_properties_mock ):
7177 config = ConfigurationLoader .load ()
@@ -77,3 +83,85 @@ def test_get_token(self):
7783
7884 with self .subTest ("Token is absent" ), self .assertRaises (MissingKeyException ):
7985 configuration .get_token ({})
86+
87+ @patch ("sys.argv" , ["myscript.py" , "--token" , "myToken" , "--sonar-project-key" , "myProjectKey" ])
88+ def test_load_sonar_project_properties (self ):
89+
90+ self .fs .create_file (
91+ "sonar-project.properties" ,
92+ contents = (
93+ """
94+ sonar.projectKey=overwritten-project-key
95+ sonar.projectName=My Project\n
96+ sonar.sources=src # my sources\n
97+ sonar.exclusions=**/generated/**/*,**/deprecated/**/*,**/testdata/**/*\n
98+ """
99+ ),
100+ )
101+ configuration = ConfigurationLoader .load ()
102+ expected_configuration = {
103+ SONAR_TOKEN : "myToken" ,
104+ SONAR_PROJECT_KEY : "myProjectKey" ,
105+ SONAR_PROJECT_NAME : "My Project" ,
106+ SONAR_SOURCES : "src # my sources" ,
107+ SONAR_EXCLUSIONS : "**/generated/**/*,**/deprecated/**/*,**/testdata/**/*" ,
108+ SONAR_SCANNER_APP : "python" ,
109+ SONAR_SCANNER_APP_VERSION : "1.0" ,
110+ SONAR_SCANNER_BOOTSTRAP_START_TIME : configuration [SONAR_SCANNER_BOOTSTRAP_START_TIME ],
111+ SONAR_VERBOSE : False ,
112+ SONAR_SCANNER_SKIP_JRE_PROVISIONING : False ,
113+ SONAR_USER_HOME : "~/.sonar" ,
114+ SONAR_SCANNER_CONNECT_TIMEOUT : 5 ,
115+ SONAR_SCANNER_SOCKET_TIMEOUT : 60 ,
116+ SONAR_SCANNER_RESPONSE_TIMEOUT : 0 ,
117+ SONAR_SCANNER_KEYSTORE_PASSWORD : "changeit" ,
118+ SONAR_SCANNER_TRUSTSTORE_PASSWORD : "changeit" ,
119+ }
120+ self .assertDictEqual (configuration , expected_configuration )
121+
122+ @patch (
123+ "sys.argv" ,
124+ [
125+ "myscript.py" ,
126+ "--token" ,
127+ "myToken" ,
128+ "--sonar-project-key" ,
129+ "myProjectKey" ,
130+ "--sonar-project-base-dir" ,
131+ "custom/path" ,
132+ ],
133+ )
134+ def test_load_sonar_project_properties_from_custom_path (self ):
135+ self .fs .create_dir ("custom/path" )
136+ self .fs .create_file (
137+ "custom/path/sonar-project.properties" ,
138+ contents = (
139+ """
140+ sonar.projectKey=custom-path-project-key
141+ sonar.projectName=Custom Path Project
142+ sonar.sources=src/main
143+ sonar.tests=src/test
144+ """
145+ ),
146+ )
147+ configuration = ConfigurationLoader .load ()
148+ expected_configuration = {
149+ SONAR_TOKEN : "myToken" ,
150+ SONAR_PROJECT_KEY : "myProjectKey" ,
151+ SONAR_PROJECT_NAME : "Custom Path Project" ,
152+ SONAR_SOURCES : "src/main" ,
153+ SONAR_PROJECT_BASE_DIR : "custom/path" ,
154+ SONAR_TESTS : "src/test" ,
155+ SONAR_SCANNER_APP : "python" ,
156+ SONAR_SCANNER_APP_VERSION : "1.0" ,
157+ SONAR_SCANNER_BOOTSTRAP_START_TIME : configuration [SONAR_SCANNER_BOOTSTRAP_START_TIME ],
158+ SONAR_VERBOSE : False ,
159+ SONAR_SCANNER_SKIP_JRE_PROVISIONING : False ,
160+ SONAR_USER_HOME : "~/.sonar" ,
161+ SONAR_SCANNER_CONNECT_TIMEOUT : 5 ,
162+ SONAR_SCANNER_SOCKET_TIMEOUT : 60 ,
163+ SONAR_SCANNER_RESPONSE_TIMEOUT : 0 ,
164+ SONAR_SCANNER_KEYSTORE_PASSWORD : "changeit" ,
165+ SONAR_SCANNER_TRUSTSTORE_PASSWORD : "changeit" ,
166+ }
167+ self .assertDictEqual (configuration , expected_configuration )
0 commit comments