Skip to content

Commit 92b498d

Browse files
committed
Add integration test to validate scripting feature
1 parent fbf8cb5 commit 92b498d

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

bin/test.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ else
2525
fi
2626
jpypeCode=$?
2727

28+
echo
29+
echo "-------------------------------------------"
30+
echo "| Running integration tests (JPype only) |"
31+
echo "-------------------------------------------"
32+
itCode=0
33+
for t in it/*.py
34+
do
35+
python "$t"
36+
code=$?
37+
printf -- "--> %s " "$t"
38+
if [ "$code" -eq 0 ]
39+
then
40+
echo "[OK]"
41+
else
42+
echo "[FAILED]"
43+
itCode=$code
44+
fi
45+
done
46+
2847
echo
2948
echo "-------------------------------------------"
3049
echo "| Testing Jep mode (Python inside Java) |"
@@ -68,5 +87,6 @@ jepCode=$?
6887
rm -f jep_test.py
6988

7089
test "$jpypeCode" -ne 0 && exit "$jpypeCode"
90+
test "$itCode" -ne 0 && exit "$itCode"
7191
test "$jepCode" -ne 0 && exit "$jepCode"
7292
exit 0

it/scripting.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
Test the enable_python_scripting function, and subsequent use of
3+
the SciJava Python script language (org.scijava:scripting-python).
4+
5+
As a side effect, this script also tests Maven dependency resolution.
6+
"""
7+
8+
import sys
9+
import scyjava
10+
11+
scyjava.config.endpoints.extend([
12+
"org.scijava:scijava-common:2.94.2",
13+
"org.scijava:scripting-python:MANAGED"
14+
])
15+
16+
# Create minimal SciJava context with a ScriptService.
17+
Context = scyjava.jimport("org.scijava.Context")
18+
ScriptService = scyjava.jimport("org.scijava.script.ScriptService")
19+
# HACK: Avoid "[ERROR] Cannot create plugin" spam.
20+
WidgetService = scyjava.jimport("org.scijava.widget.WidgetService")
21+
ctx = Context(ScriptService, WidgetService)
22+
23+
# Enable the Python script language.
24+
scyjava.enable_python_scripting(ctx)
25+
26+
# Assert that the Python script language is available.
27+
ss = ctx.service("org.scijava.script.ScriptService")
28+
lang = ss.getLanguageByName("Python")
29+
assert lang is not None and "Python" in lang.getNames()
30+
31+
# Construct a script.
32+
script = """
33+
#@ String name
34+
#@ int age
35+
#@output String statement
36+
statement = f"Hello, {name}! In one year you will be {age + 1} years old."
37+
"A wild return value appears!"
38+
"""
39+
StringReader = scyjava.jimport("java.io.StringReader")
40+
ScriptInfo = scyjava.jimport("org.scijava.script.ScriptInfo")
41+
info = ScriptInfo(ctx, "script.py", StringReader(script))
42+
info.setLanguage(lang)
43+
44+
# Run the script.
45+
future = ss.run(info, True, "name", "Chuckles", "age", 13)
46+
try:
47+
module = future.get()
48+
outputs = module.getOutputs()
49+
statement = outputs["statement"]
50+
return_value = module.getReturnValue()
51+
except Exception as e:
52+
sys.stderr.write("-- SCRIPT EXECUTION FAILED --\n")
53+
trace = scyjava.jstacktrace(e)
54+
if trace:
55+
sys.stderr.write(f"{trace}\n")
56+
raise e
57+
58+
assert statement == "Hello, Chuckles! In one year you will be 14 years old."
59+
assert return_value == "A wild return value appears!"

0 commit comments

Comments
 (0)