diff --git a/src/napari_imagej/java.py b/src/napari_imagej/java.py index b4a132d3..8bd77c07 100644 --- a/src/napari_imagej/java.py +++ b/src/napari_imagej/java.py @@ -17,8 +17,8 @@ from jpype import JClass from scyjava import config, get_version, is_version_at_least, jimport, jvm_started -from napari_imagej import settings -from napari_imagej.utilities.logging import log_debug +from napari_imagej import __version__, settings +from napari_imagej.utilities.logging import log_debug, warn # -- Constants -- @@ -34,6 +34,8 @@ "sc.fiji:TrackMate": "7.11.0", } +recommended_versions = {} + # -- ImageJ API -- # _ij = None @@ -152,6 +154,28 @@ def _validate_imagej(): ) raise RuntimeError(failure_str) + # Find versions below recommended + violations = [] + for component, cls in component_requirements.items(): + if component not in recommended_versions: + continue + recommended_version = recommended_versions[component] + component_version = get_version(cls) + if not is_version_at_least(component_version, recommended_version): + violations.append( + f"{component} : {recommended_version} (Installed: {component_version})" + ) + + # If there are older versions, warn the user + if violations: + failure_str = ( + f"napari-imagej v{__version__} recommends using " + "the following component versions:" + ) + violations.insert(0, failure_str) + failure_str = "\n\t".join(violations) + warn(failure_str) + def _optional_requirements(): optionals = {} diff --git a/tests/test_java.py b/tests/test_java.py index 2f0f4b22..cc06a623 100644 --- a/tests/test_java.py +++ b/tests/test_java.py @@ -2,8 +2,9 @@ from scyjava import get_version, is_version_at_least, jimport -from napari_imagej import settings -from napari_imagej.java import minimum_versions +from napari_imagej import __version__, settings +from napari_imagej.java import _validate_imagej, minimum_versions +from napari_imagej.utilities.logging import logger version_checks = { "io.scif:scifio": "io.scif.SCIFIO", @@ -57,3 +58,33 @@ def test_endpoint(ij): version = gav[2] exp_version = get_version(jimport(version_checks[ga])) assert is_version_at_least(version, exp_version) + + +def test_recommended_version(ij): + # Save old recommended versions + import napari_imagej.java + + existing = napari_imagej.java.recommended_versions + napari_imagej.java.recommended_versions = {"org.scijava:scijava-common": "999.0.0"} + + # Setup log handler to capture warning + import io + import logging + + log_capture_string = io.StringIO() + ch = logging.StreamHandler(log_capture_string) + ch.setLevel(logging.WARN) + logger().addHandler(ch) + # Validate ImageJ - capture lower-than-recommended version + _validate_imagej() + log_contents = log_capture_string.getvalue() + log_capture_string.close() + # Assert warning given + assert log_contents == ( + f"napari-imagej: napari-imagej v{__version__} recommends using the " + "following component versions:\n\torg.scijava:scijava-common : " + "999.0.0 (Installed: 2.94.1)\n" + ) + + # restore recommended versions + napari_imagej.java.recommended_versions = existing