33import pytest
44
55import imagej
6+ import imagej .doctor
67from scyjava import config
78
89
910def pytest_addoption (parser ):
1011 """
11- Set up the command line parser for ImageJ location and headless mode
12+ Set up the command line parser for ImageJ2 location and headless mode
1213 :param parser: pytest's parser, passed in automatically
1314 :return: None
1415 """
@@ -32,59 +33,41 @@ def pytest_addoption(parser):
3233 default = True ,
3334 help = "Include the original ImageJ" ,
3435 )
36+ parser .addoption (
37+ "--java" ,
38+ action = "store" ,
39+ default = None ,
40+ help = "version of Java to cache and use (e.g. 8, 11, 17, 21)" ,
41+ )
3542
3643
3744@pytest .fixture (scope = "session" )
3845def ij (request ):
3946 """
40- Create an ImageJ instance to be used by the whole testing environment
47+ Create an ImageJ2 gateway to be used by the whole testing environment
4148 :param request: Pytest variable passed in to fixtures
4249 """
50+ # enable debug logging
51+ imagej .doctor .debug_to_stderr ()
4352 # get test configuration
4453 ij_dir = request .config .getoption ("--ij" )
4554 legacy = request .config .getoption ("--legacy" )
4655 headless = request .config .getoption ("--headless" )
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 :
56+ java_version = request .config .getoption ("--java" )
57+ config .set_java_constraints (fetch = True , vendor = 'zulu' , version = java_version )
58+ # JavaScript is used in the tests. But the Nashorn engine was
59+ # dropped from the JDK at v15, so we readd it here if needed.
60+ if int (java_version ) >= 15 :
5061 config .endpoints .append ("org.openjdk.nashorn:nashorn-core" )
51- imagej .when_imagej_starts (lambda ij : setattr (ij , "_testing " , True ))
52- # initialize the ImageJ gateway
62+ imagej .when_imagej_starts (lambda ij : setattr (ij , "_java_version " , java_version ))
63+ # initialize the ImageJ2 gateway
5364 mode = "headless" if headless else "interactive"
5465 ij = imagej .init (ij_dir , mode = mode , add_legacy = legacy )
55-
5666 yield ij
5767
5868 ij .dispose ()
5969
6070
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-
8871def str2bool (v ):
8972 """
9073 Convert string inputs into bool
0 commit comments