3434)
3535from pysonar_scanner .exceptions import ChecksumException , NoJreAvailableException , UnsupportedArchiveFormat
3636from pysonar_scanner .jre import JREProvisioner , JREResolvedPath , JREResolver , JREResolverConfiguration
37+ from pysonar_scanner .utils import Os , Arch
3738from tests import sq_api_utils
3839
3940import zipfile
4041
4142
42- @patch ("pysonar_scanner.utils.get_os" , return_value = "linux" )
43- @patch ("pysonar_scanner.utils.get_arch" , return_value = "aarch64" )
43+ @patch ("pysonar_scanner.utils.get_os" , return_value = Os . LINUX )
44+ @patch ("pysonar_scanner.utils.get_arch" , return_value = Arch . AARCH64 )
4445class TestJREProvisioner (pyfakefs .TestCase ):
4546 def setUp (self ):
4647 self .setUpPyfakefs (allow_root_user = False )
@@ -56,7 +57,7 @@ def setUp(self):
5657 filename = "fake_jre.zip" ,
5758 sha256 = "fakechecksum" ,
5859 java_path = "fake_java" ,
59- os = "windows" ,
60+ os = Os . WINDOWS . value ,
6061 arch = "x64" ,
6162 download_url = "http://example.com/fake_jre.zip" ,
6263 )
@@ -74,8 +75,8 @@ def __setup_zip_file(self):
7475 filename = self .zip_name ,
7576 sha256 = self .zip_checksum ,
7677 java_path = "java" ,
77- os = "linux" ,
78- arch = "aarch64" ,
78+ os = Os . LINUX . value ,
79+ arch = Arch . AARCH64 . value ,
7980 download_url = None ,
8081 )
8182
@@ -94,8 +95,8 @@ def __setup_tar_file(self):
9495 filename = self .tar_gz_name ,
9596 sha256 = self .tar_gz_checksum ,
9697 java_path = "java" ,
97- os = "linux" ,
98- arch = "aarch64" ,
98+ os = Os . LINUX . value ,
99+ arch = Arch . AARCH64 . value ,
99100 download_url = None ,
100101 )
101102
@@ -107,14 +108,14 @@ def __setup_tar_file(self):
107108 filename = self .tgz_name ,
108109 sha256 = self .tgz_checksum ,
109110 java_path = "java" ,
110- os = "linux" ,
111- arch = "aarch64" ,
111+ os = Os . LINUX . value ,
112+ arch = Arch . AARCH64 . value ,
112113 download_url = None ,
113114 )
114115
115116 def test_if_patching_worked (self , get_os_mock , get_arch_mock ):
116- self .assertEqual (utils .get_os (), "linux" )
117- self .assertEqual (utils .get_arch (), "aarch64" )
117+ self .assertEqual (utils .get_os (), Os . LINUX )
118+ self .assertEqual (utils .get_arch (), Arch . AARCH64 )
118119
119120 def test_successfully_downloading_jre (self , get_os_mock , get_arch_mock ):
120121 class JRETestCase (TypedDict ):
@@ -137,11 +138,10 @@ class JRETestCase(TypedDict):
137138 testcase_jre = testcase ["jre" ]
138139 jres = [testcase_jre , self .other_jre ]
139140 with self .subTest (jre = testcase ), sq_api_utils .sq_api_mocker () as mocker :
140- mocker .mock_analysis_jres (
141- body = [sq_api_utils .jre_to_dict (jre ) for jre in jres ])
141+ mocker .mock_analysis_jres (body = [sq_api_utils .jre_to_dict (jre ) for jre in jres ])
142142 mocker .mock_analysis_jre_download (id = testcase_jre .id , body = testcase ["bytes" ], status = 200 )
143143
144- provisioner = JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ())
144+ provisioner = JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value )
145145 jre_path = provisioner .provision ()
146146
147147 cache_file = self .cache .get_file (testcase_jre .filename , testcase ["checksum" ])
@@ -161,7 +161,7 @@ def test_invalid_checksum(self, *args):
161161 mocker .mock_analysis_jres (body = [jre_dict ])
162162 mocker .mock_analysis_jre_download (id = "zip_jre" , body = self .zip_bytes , status = 200 )
163163
164- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
164+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
165165
166166 def test_retry_mechanism (self , * args ):
167167 with sq_api_utils .sq_api_mocker () as mocker :
@@ -173,7 +173,7 @@ def test_retry_mechanism(self, *args):
173173 mocker .mock_analysis_jres (body = [jre_dict ])
174174 mocker .mock_analysis_jre_download (id = "zip_jre" , body = self .zip_bytes , status = 200 )
175175
176- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
176+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
177177
178178 cache_file = self .cache .get_file (self .zip_jre .filename , self .zip_checksum )
179179 self .assertTrue (cache_file .is_valid ())
@@ -187,7 +187,7 @@ def test_already_cached(self, *args):
187187 with self .cache .get_file (self .zip_jre .filename , self .zip_checksum ).open (mode = "wb" ) as f :
188188 f .write (self .zip_bytes )
189189
190- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
190+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
191191
192192 cache_file = self .cache .get_file (self .zip_jre .filename , self .zip_checksum )
193193 self .assertTrue (cache_file .is_valid ())
@@ -208,19 +208,18 @@ def test_file_already_exists_with_invalid_checksum(self, *args):
208208 cache_file .is_valid (), msg = "Cache file should have invalid checksum before provisioner ran"
209209 )
210210
211- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
211+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
212212
213213 self .assertTrue (cache_file .is_valid (), msg = "Cache file should have valid checksum after provisioner ran" )
214214
215215 def test_no_jre_available (self , * args ):
216216 with self .assertRaises (NoJreAvailableException ), sq_api_utils .sq_api_mocker () as mocker :
217217 mocker .mock_analysis_jres (body = [])
218- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
218+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
219219
220220 def test_unzip_dir_already_exists (self , * args ):
221221 with sq_api_utils .sq_api_mocker () as mocker :
222- mocker .mock_analysis_jres (
223- body = [sq_api_utils .jre_to_dict (self .zip_jre )])
222+ mocker .mock_analysis_jres (body = [sq_api_utils .jre_to_dict (self .zip_jre )])
224223 mocker .mock_analysis_jre_download (id = "zip_jre" , body = self .zip_bytes , status = 200 )
225224
226225 unzip_dir = self .cache .get_file_path ("jre.zip_unzip" )
@@ -229,7 +228,7 @@ def test_unzip_dir_already_exists(self, *args):
229228 old_text_file = unzip_dir / "subdir/test.txt"
230229 old_text_file .write_text ("test" )
231230
232- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
231+ JREProvisioner (self .api , self .cache , utils .get_os (). value , utils .get_arch (). value ).provision ()
233232
234233 self .assertTrue (unzip_dir .exists ())
235234 self .assertTrue ((unzip_dir / "readme.md" ).exists ())
@@ -242,17 +241,15 @@ def test_unsupported_jre(self, *args):
242241 filename = "jre.txt" ,
243242 sha256 = self .zip_checksum ,
244243 java_path = "java" ,
245- os = "linux" ,
246- arch = "aarch64" ,
244+ os = Os . LINUX . value ,
245+ arch = Arch . AARCH64 . value ,
247246 download_url = None ,
248247 )
249248
250249 with self .assertRaises (UnsupportedArchiveFormat ), sq_api_utils .sq_api_mocker () as mocker :
251- mocker .mock_analysis_jres (
252- body = [sq_api_utils .jre_to_dict (unsupported_archive_jre )])
250+ mocker .mock_analysis_jres (body = [sq_api_utils .jre_to_dict (unsupported_archive_jre )])
253251 mocker .mock_analysis_jre_download (id = "unsupported" , body = self .zip_bytes , status = 200 )
254-
255- JREProvisioner (self .api , self .cache , utils .get_os (), utils .get_arch ()).provision ()
252+ JREProvisioner (self .api , self .cache , utils .get_os ().value , utils .get_arch ().value ).provision ()
256253
257254
258255class TestJREResolvedPath (unittest .TestCase ):
@@ -279,13 +276,13 @@ def test(self):
279276 {
280277 SONAR_SCANNER_JAVA_EXE_PATH : "a/b" ,
281278 SONAR_SCANNER_SKIP_JRE_PROVISIONING : True ,
282- SONAR_SCANNER_OS : "windows" ,
279+ SONAR_SCANNER_OS : Os . WINDOWS . value ,
283280 }
284281 )
285282
286283 self .assertEqual (config .sonar_scanner_java_exe_path , "a/b" )
287284 self .assertTrue (config .sonar_scanner_skip_jre_provisioning )
288- self .assertEqual (config .sonar_scanner_os , "windows" )
285+ self .assertEqual (config .sonar_scanner_os , Os . WINDOWS . value )
289286
290287
291288class TestJREResolver (unittest .TestCase ):
@@ -339,7 +336,7 @@ class TestCaseDict(TypedDict):
339336 {
340337 "name" : "if skip_jre_provisioning is True and java_home is not set return the default for windows" ,
341338 "config" : JREResolverConfiguration (
342- sonar_scanner_os = "windows" ,
339+ sonar_scanner_os = Os . WINDOWS . value ,
343340 sonar_scanner_skip_jre_provisioning = True ,
344341 sonar_scanner_java_exe_path = None ,
345342 ),
0 commit comments