44
44
import subprocess
45
45
import tempfile
46
46
import unittest
47
+ import pathlib
48
+ import util
47
49
48
50
is_enabled = 'ENABLE_JBANG_INTEGRATION_UNITTESTS' in os .environ and os .environ ['ENABLE_JBANG_INTEGRATION_UNITTESTS' ] == "true"
49
51
MAVEN_REPO_LOCAL_URL = os .environ .get ('org.graalvm.maven.downloader.repository' )
@@ -161,19 +163,28 @@ def addLocalMavenRepo(self, file):
161
163
162
164
def registerCatalog (self , catalog_file ):
163
165
# we need to be sure that the current dir is not dir, where is the catalog defined
164
- os . chdir ( WORK_DIR )
166
+
165
167
# find if the catalog is not registered
166
168
command = [JBANG_CMD , "catalog" , "list" ]
167
- out , result = run_cmd (command )
169
+ out , result = run_cmd (command , cwd = WORK_DIR )
168
170
if result == 0 :
169
171
if CATALOG_ALIAS not in out :
170
172
# registering our catalog
171
173
command = [JBANG_CMD , "catalog" , "add" , "--name" , CATALOG_ALIAS , catalog_file ]
172
- out , result = run_cmd (command )
174
+ out , result = run_cmd (command , cwd = WORK_DIR )
173
175
if result != 0 :
174
176
self .fail (f"Problem during registering catalog: { out } " )
175
177
else :
176
- self .fail (f"Problem during registering catalog: { out } " )
178
+ self .fail (f"Problem during registering catalog: { out } " )
179
+
180
+ def prepare_hello_example (self , work_dir ):
181
+ hello_java_file = os .path .join (work_dir , "hello.java" )
182
+ self .prepare_template (os .path .join (os .path .dirname (os .path .abspath (__file__ )), "../../../../graalpy-jbang/examples/hello.java" ), hello_java_file )
183
+ return hello_java_file
184
+
185
+ def prepare_template (self , template , target ):
186
+ shutil .copyfile (template , target )
187
+ self .addLocalMavenRepo (target )
177
188
178
189
@unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
179
190
def test_catalog (self ):
@@ -193,10 +204,9 @@ def test_graalpy_template(self):
193
204
template_name = "graalpy"
194
205
test_file = "graalpy_test.java"
195
206
work_dir = self .tmpdir
196
- os .chdir (work_dir )
197
207
198
208
command = [JBANG_CMD , "--verbose" , "init" , f"--template={ template_name } @{ CATALOG_ALIAS } " , test_file ]
199
- out , result = run_cmd (command )
209
+ out , result = run_cmd (command , cwd = work_dir )
200
210
self .assertTrue (result == 0 , f"Creating template { template_name } failed" )
201
211
202
212
# add local maven repo to the deps
@@ -205,7 +215,7 @@ def test_graalpy_template(self):
205
215
206
216
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
207
217
command = [JBANG_CMD , "--verbose" , test_file_path , tested_code ]
208
- out , result = run_cmd (command )
218
+ out , result = run_cmd (command , cwd = work_dir )
209
219
210
220
self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } \n " )
211
221
self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor\n but in stdout was:\n { out } " )
@@ -219,17 +229,16 @@ def test_graalpy_template_native(self):
219
229
template_name = "graalpy"
220
230
test_file = "graalpy_test.java"
221
231
work_dir = self .tmpdir
222
- os .chdir (work_dir )
223
232
224
233
command = [JBANG_CMD , "--verbose" , "init" , f"--template={ template_name } @{ CATALOG_ALIAS } " , test_file ]
225
- out , result = run_cmd (command )
234
+ out , result = run_cmd (command , cwd = work_dir )
226
235
self .assertTrue (result == 0 , f"Creating template { template_name } failed" )
227
236
228
237
test_file_path = os .path .join (work_dir , test_file )
229
238
self .addLocalMavenRepo (test_file_path )
230
239
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
231
240
command = [JBANG_CMD , "--verbose" , "--native" , test_file_path , tested_code ]
232
- out , result = run_cmd (command )
241
+ out , result = run_cmd (command , cwd = work_dir )
233
242
234
243
self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
235
244
self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor" )
@@ -241,20 +250,168 @@ def test_graalpy_local_repo_template(self):
241
250
template_name = "graalpy_local_repo"
242
251
test_file = "graalpy_test_local_repo.java"
243
252
work_dir = self .tmpdir
244
- os .chdir (work_dir )
245
253
246
254
command = [JBANG_CMD , "--verbose" , "init" , f"--template={ template_name } @{ CATALOG_ALIAS } " , f"-Dpath_to_local_repo={ MAVEN_REPO_LOCAL_URL } " , test_file ]
247
- out , result = run_cmd (command )
255
+ out , result = run_cmd (command , cwd = work_dir )
248
256
self .assertTrue (result == 0 , f"Creating template { template_name } failed" )
249
257
250
258
test_file_path = os .path .join (work_dir , test_file )
251
259
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
252
260
command = [JBANG_CMD , "--verbose" , test_file_path , tested_code ]
253
- out , result = run_cmd (command )
261
+ out , result = run_cmd (command , cwd = work_dir )
262
+
263
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
264
+ self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor" )
265
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
266
+
267
+ @unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
268
+ def test_hello_example (self ):
269
+ work_dir = self .tmpdir
270
+ hello_java_file = self .prepare_hello_example (work_dir )
271
+
272
+ tested_code = "print('hello java')"
273
+ command = [JBANG_CMD , "--verbose" , hello_java_file , tested_code ]
274
+ out , result = run_cmd (command , cwd = work_dir )
275
+
276
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
277
+ self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor" )
278
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
279
+
280
+ if not 'win32' in sys .platform :
281
+ command = [JBANG_CMD , "--verbose" , "--native" , hello_java_file , tested_code ]
282
+ out , result = run_cmd (command , cwd = work_dir )
283
+
284
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
285
+ self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor" )
286
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
287
+
288
+ @unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
289
+ def test_external_dir (self ):
290
+ work_dir = self .tmpdir
291
+ hello_java_file = self .prepare_hello_example (work_dir )
292
+
293
+ # patch hello.java file to use external dir for resources
294
+ resources_dir = os .path .join (work_dir , "python-resources" )
295
+ src_dir = os .path .join (resources_dir , "src" )
296
+ os .makedirs (src_dir , exist_ok = True )
297
+ with open (os .path .join (src_dir , "hello.py" ), "w" , encoding = "utf-8" ) as f :
298
+ f .writelines ("""
299
+ from termcolor import colored
300
+ def hello():
301
+ print(print(colored('hello java', 'red', attrs=['reverse', 'blink'])))
302
+ """ )
303
+ util .replace_in_file (hello_java_file ,
304
+ "//PIP termcolor==2.2" ,
305
+ f"//PIP termcolor==2.2\n //PYTHON_RESOURCES_DIRECTORY { resources_dir } " )
306
+ rd = resources_dir .replace ("\\ " , "\\ \\ " )
307
+ util .replace_in_file (hello_java_file ,
308
+ "GraalPyResources.createContext()" ,
309
+ f"GraalPyResources.contextBuilder(java.nio.file.Path.of(\" { rd } \" )).build()" )
310
+
311
+ tested_code = "import hello; hello.hello()"
312
+ command = [JBANG_CMD , "--verbose" , hello_java_file , tested_code ]
313
+ out , result = run_cmd (command , cwd = work_dir )
254
314
255
315
self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
256
316
self .assertTrue ("Successfully installed termcolor" in out , f"Expected text:\n Successfully installed termcolor" )
257
317
self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
258
318
319
+ # add ujson to PIP comment
320
+ util .replace_in_file (hello_java_file ,
321
+ "//PIP termcolor==2.2" ,
322
+ "//PIP termcolor==2.2 ujson" )
323
+ tested_code = "import hello; hello.hello()"
324
+ command = [JBANG_CMD , "--verbose" , hello_java_file , tested_code ]
325
+ out , result = run_cmd (command , cwd = work_dir )
326
+
327
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
328
+ self .assertTrue ("Successfully installed ujson" in out , f"Expected text:\n Successfully installed ujson" )
329
+ self .assertFalse ("Successfully installed termcolor" in out , f"Did not expect text:\n Successfully installed termcolor" )
330
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
331
+
332
+ # remove ujson from PIP comment
333
+ util .replace_in_file (hello_java_file ,
334
+ "//PIP termcolor==2.2 ujson" ,
335
+ "//PIP termcolor==2.2\n " )
336
+ tested_code = "import hello; hello.hello()"
337
+ command = [JBANG_CMD , "--verbose" , hello_java_file , tested_code ]
338
+ out , result = run_cmd (command , cwd = work_dir )
339
+
340
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
341
+ self .assertTrue ("Uninstalling ujson" in out , f"Expected text:\n Uninstalling ujson" )
342
+ self .assertFalse ("Successfully installed termcolor" in out , f"Did not expect text:\n Successfully installed termcolor" )
343
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
344
+
345
+ # add ujson in additional PIP comment
346
+ util .replace_in_file (hello_java_file ,
347
+ "//PIP termcolor==2.2" ,
348
+ "//PIP termcolor==2.2\n //PIP ujson" )
349
+ tested_code = "import hello; hello.hello()"
350
+ command = [JBANG_CMD , "--verbose" , hello_java_file , tested_code ]
351
+ out , result = run_cmd (command , cwd = work_dir )
352
+
353
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
354
+ self .assertTrue ("Successfully installed ujson" in out , f"Expected text:\n Successfully installed ujson" )
355
+ self .assertFalse ("Successfully installed termcolor" in out , f"Did not expect text:\n Successfully installed termcolor" )
356
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
357
+
358
+ if not 'win32' in sys .platform :
359
+ command = [JBANG_CMD , "--verbose" , "--native" , hello_java_file , tested_code ]
360
+ out , result = run_cmd (command , cwd = work_dir )
361
+
362
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
363
+ self .assertFalse ("Successfully installed termcolor" in out , f"Did not expect text:\n Successfully installed termcolor" )
364
+ self .assertFalse ("Successfully installed ujson" in out , f"Did not expect text:\n Successfully installed ujson" )
365
+ self .assertTrue ("hello java" in out , f"Expected text:\n hello java\n but in stdout was:\n { out } " )
366
+
367
+ def check_empty_comments (self , work_dir , java_file ):
368
+ command = [JBANG_CMD , "--verbose" , java_file ]
369
+ out , result = run_cmd (command , cwd = work_dir )
370
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
371
+ self .assertFalse ("[graalpy jbang integration]" in out , f"Did not expect text:\n [graalpy jbang integration]" )
372
+
373
+ @unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
374
+ def test_malformed_tag_formats (self ):
375
+ jbang_templates_dir = os .path .join (os .path .dirname (__file__ ), "jbang" )
376
+ work_dir = self .tmpdir
377
+
378
+ java_file = os .path .join (work_dir , "EmptyPIPComments.java" )
379
+ self .prepare_template (os .path .join (jbang_templates_dir , "EmptyPIPComments.j" ), java_file )
380
+ self .check_empty_comments (work_dir , java_file )
381
+
382
+ java_file = os .path .join (work_dir , "EmptyPythonResourceComment.java" )
383
+ self .prepare_template (os .path .join (jbang_templates_dir , "EmptyPythonResourceComment.j" ), java_file )
384
+ self .check_empty_comments (work_dir , java_file )
385
+
386
+ java_file = os .path .join (work_dir , "EmptyPythonResourceCommentWithBlanks.java" )
387
+ self .prepare_template (os .path .join (jbang_templates_dir , "EmptyPythonResourceCommentWithBlanks.j" ), java_file )
388
+ self .check_empty_comments (work_dir , java_file )
389
+
390
+ @unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
391
+ def test_no_pkgs_but_resource_dir (self ):
392
+ jbang_templates_dir = os .path .join (os .path .dirname (__file__ ), "jbang" )
393
+ work_dir = self .tmpdir
394
+
395
+ java_file = os .path .join (work_dir , "NoPackagesResourcesDir.java" )
396
+ self .prepare_template (os .path .join (jbang_templates_dir , "NoPackagesResourcesDir.j" ), java_file )
397
+ command = [JBANG_CMD , "--verbose" , java_file ]
398
+ out , result = run_cmd (command , cwd = work_dir )
399
+ self .assertTrue (result == 0 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
400
+ self .assertFalse ("[graalpy jbang integration] python packages" in out , f"Did not expect text:\n [graalpy jbang integration] python packages" )
401
+ self .assertTrue ("[graalpy jbang integration] python resources directory: python-resources" in out , f"Expected text:\n [graalpy jbang integration] python resources directory: python-resources" )
402
+ self .assertTrue ("-m ensurepip" in out , f"-m ensurepip" )
403
+
404
+ @unittest .skipUnless (is_enabled , "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true" )
405
+ def test_two_resource_dirs (self ):
406
+ jbang_templates_dir = os .path .join (os .path .dirname (__file__ ), "jbang" )
407
+ work_dir = self .tmpdir
408
+
409
+ java_file = os .path .join (work_dir , "TwoPythonResourceComments.java" )
410
+ self .prepare_template (os .path .join (jbang_templates_dir , "TwoPythonResourceComments.j" ), java_file )
411
+ command = [JBANG_CMD , "--verbose" , java_file ]
412
+ out , result = run_cmd (command , cwd = work_dir )
413
+ self .assertTrue (result == 1 , f"Execution failed with code { result } \n command: { command } \n stdout: { out } " )
414
+ self .assertTrue ("only one //PYTHON_RESOURCES_DIRECTORY comment is allowed" in out , f"Expected text:\n only one //PYTHON_RESOURCES_DIRECTORY comment is allowed" )
415
+
259
416
260
417
unittest .skip_deselected_test_functions (globals ())
0 commit comments