11import argparse
2-
2+ import subprocess
33import pytest
44
55import imagej
6+ from scyjava import config
67
78
89def pytest_addoption (parser ):
@@ -39,12 +40,16 @@ def ij(request):
3940 Create an ImageJ instance to be used by the whole testing environment
4041 :param request: Pytest variable passed in to fixtures
4142 """
43+ # get test configuration
4244 ij_dir = request .config .getoption ("--ij" )
4345 legacy = request .config .getoption ("--legacy" )
4446 headless = request .config .getoption ("--headless" )
45-
47+ # add the nashorn (JavaScript) endpoint if needed,
48+ # nashorn was bundled with the JDK from Java 8 to 14
49+ if capture_java_version () > 14 :
50+ config .endpoints .append ("org.openjdk.nashorn:nashorn-core" )
4651 imagej .when_imagej_starts (lambda ij : setattr (ij , "_testing" , True ))
47-
52+ # initialize the ImageJ gateway
4853 mode = "headless" if headless else "interactive"
4954 ij = imagej .init (ij_dir , mode = mode , add_legacy = legacy )
5055
@@ -53,6 +58,33 @@ def ij(request):
5358 ij .dispose ()
5459
5560
61+ def capture_java_version () -> int :
62+ """Capture the installed Java version.
63+
64+ This function captures the JDK version installed in the current
65+ venv without starting the JVM by parsing the Java "-version"
66+ output string.
67+
68+ :return: The major Java version (8, 11, 21 etc...).
69+ """
70+ try :
71+ # capture the Java version string
72+ java_ver_str = subprocess .run (
73+ ["java" , "-version" ], capture_output = True , text = True
74+ )
75+ # extract the Java version from the string
76+ java_ver = java_ver_str .stderr .split ("\n " )[0 ].split (" " )[2 ]
77+ java_ver = java_ver .strip ('"' ).split ("." )
78+ major_ver_arr = [int (java_ver [i ]) for i in range (2 )]
79+ # find major Java version
80+ if major_ver_arr [0 ] == 1 :
81+ return major_ver_arr [1 ] # Java 8-10
82+ else :
83+ return major_ver_arr [0 ] # Java 11+
84+ except FileNotFoundError :
85+ raise RuntimeError ("No Java installation found." )
86+
87+
5688def str2bool (v ):
5789 """
5890 Convert string inputs into bool
0 commit comments