Skip to content

Commit 3913695

Browse files
committed
more jbang tests
1 parent 114ea37 commit 3913695

File tree

8 files changed

+245
-37
lines changed

8 files changed

+245
-37
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:graalpy-jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PIP
5+
// one blank after PIP
6+
//PIP
7+
// three blanks after PIP
8+
//PIP
9+
10+
public class EmptyPIPComments {
11+
public static void main(String[] args) {
12+
}
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:graalpy-jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY
5+
6+
public class EmptyPythonResourceComment {
7+
public static void main(String[] args) {
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:graalpy-jbang:${env.GRAALPY_VERSION:24.2.0}
4+
// resource dir with blanks
5+
//PYTHON_RESOURCES_DIRECTORY
6+
7+
public class EmptyPythonResourceCommentWithBlanks {
8+
public static void main(String[] args) {
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:graalpy-jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY python-resources
5+
6+
public class NoPackagesResourcesDir {
7+
public static void main(String[] args) {
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS org.graalvm.python:graalpy-jbang:${env.GRAALPY_VERSION:24.2.0}
4+
//PYTHON_RESOURCES_DIRECTORY
5+
//PYTHON_RESOURCES_DIRECTORY
6+
7+
public class TwoPythonResourceComments {
8+
public static void main(String[] args) {
9+
}
10+
}

graalpython/com.oracle.graal.python.test/src/tests/standalone/test_jbang_integration.py

Lines changed: 170 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import subprocess
4545
import tempfile
4646
import unittest
47+
import pathlib
48+
import util
4749

4850
is_enabled = 'ENABLE_JBANG_INTEGRATION_UNITTESTS' in os.environ and os.environ['ENABLE_JBANG_INTEGRATION_UNITTESTS'] == "true"
4951
MAVEN_REPO_LOCAL_URL = os.environ.get('org.graalvm.maven.downloader.repository')
@@ -161,19 +163,28 @@ def addLocalMavenRepo(self, file):
161163

162164
def registerCatalog(self, catalog_file):
163165
# we need to be sure that the current dir is not dir, where is the catalog defined
164-
os.chdir(WORK_DIR)
166+
165167
# find if the catalog is not registered
166168
command = [JBANG_CMD, "catalog", "list"]
167-
out, result = run_cmd(command)
169+
out, result = run_cmd(command, cwd=WORK_DIR)
168170
if result == 0:
169171
if CATALOG_ALIAS not in out:
170172
# registering our catalog
171173
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)
173175
if result != 0:
174176
self.fail(f"Problem during registering catalog: {out}")
175177
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)
177188

178189
@unittest.skipUnless(is_enabled, "ENABLE_JBANG_INTEGRATION_UNITTESTS is not true")
179190
def test_catalog(self):
@@ -193,10 +204,9 @@ def test_graalpy_template(self):
193204
template_name = "graalpy"
194205
test_file = "graalpy_test.java"
195206
work_dir = self.tmpdir
196-
os.chdir(work_dir)
197207

198208
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)
200210
self.assertTrue(result == 0, f"Creating template {template_name} failed")
201211

202212
# add local maven repo to the deps
@@ -205,7 +215,7 @@ def test_graalpy_template(self):
205215

206216
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
207217
command = [JBANG_CMD, "--verbose", test_file_path, tested_code]
208-
out, result = run_cmd(command)
218+
out, result = run_cmd(command, cwd=work_dir)
209219

210220
self.assertTrue(result == 0, f"Execution failed with code {result}\n command: {command}\n stdout: {out}\n")
211221
self.assertTrue("Successfully installed termcolor" in out, f"Expected text:\nSuccessfully installed termcolor\nbut in stdout was:\n{out}")
@@ -219,17 +229,16 @@ def test_graalpy_template_native(self):
219229
template_name = "graalpy"
220230
test_file = "graalpy_test.java"
221231
work_dir = self.tmpdir
222-
os.chdir(work_dir)
223232

224233
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)
226235
self.assertTrue(result == 0, f"Creating template {template_name} failed")
227236

228237
test_file_path = os.path.join(work_dir, test_file)
229238
self.addLocalMavenRepo(test_file_path)
230239
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
231240
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)
233242

234243
self.assertTrue(result == 0, f"Execution failed with code {result}\n command: {command}\n stdout: {out}")
235244
self.assertTrue("Successfully installed termcolor" in out, f"Expected text:\nSuccessfully installed termcolor")
@@ -241,20 +250,168 @@ def test_graalpy_local_repo_template(self):
241250
template_name = "graalpy_local_repo"
242251
test_file = "graalpy_test_local_repo.java"
243252
work_dir = self.tmpdir
244-
os.chdir(work_dir)
245253

246254
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)
248256
self.assertTrue(result == 0, f"Creating template {template_name} failed")
249257

250258
test_file_path = os.path.join(work_dir, test_file)
251259
tested_code = "from termcolor import colored; print(colored('hello java', 'red', attrs=['reverse', 'blink']))"
252260
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:\nSuccessfully installed termcolor")
265+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nSuccessfully installed termcolor")
278+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nSuccessfully installed termcolor")
286+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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)
254314

255315
self.assertTrue(result == 0, f"Execution failed with code {result}\n command: {command}\n stdout: {out}")
256316
self.assertTrue("Successfully installed termcolor" in out, f"Expected text:\nSuccessfully installed termcolor")
257317
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut in stdout was:\n{out}")
258318

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:\nSuccessfully installed ujson")
329+
self.assertFalse("Successfully installed termcolor" in out, f"Did not expect text:\nSuccessfully installed termcolor")
330+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nUninstalling ujson")
342+
self.assertFalse("Successfully installed termcolor" in out, f"Did not expect text:\nSuccessfully installed termcolor")
343+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nSuccessfully installed ujson")
355+
self.assertFalse("Successfully installed termcolor" in out, f"Did not expect text:\nSuccessfully installed termcolor")
356+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nSuccessfully installed termcolor")
364+
self.assertFalse("Successfully installed ujson" in out, f"Did not expect text:\nSuccessfully installed ujson")
365+
self.assertTrue("hello java" in out, f"Expected text:\nhello java\nbut 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:\nonly one //PYTHON_RESOURCES_DIRECTORY comment is allowed")
415+
259416

260417
unittest.skip_deselected_test_functions(globals())

0 commit comments

Comments
 (0)