Browse Source

fix ModuleNotFoundError for developers not installing package

https://github.com/fphammerle/freesurfer-surface/issues/22

https://github.com/fphammerle/freesurfer-surface/pull/23
Fabian Peter Hammerle 2 weeks ago
parent
commit
fd1e7e2fa8
2 changed files with 45 additions and 1 deletions
  1. 5 1
      freesurfer_surface/__init__.py
  2. 40 0
      tests/test_version.py

+ 5 - 1
freesurfer_surface/__init__.py

@@ -48,7 +48,11 @@ import typing
 
 import numpy
 
-from freesurfer_surface.version import __version__
+try:
+    from freesurfer_surface.version import __version__
+except ImportError:  # ModuleNotFoundError not available in python<3.6
+    # package is not installed
+    __version__ = None
 
 
 class UnsupportedLocaleSettingError(locale.Error):

+ 40 - 0
tests/test_version.py

@@ -0,0 +1,40 @@
+import re
+import pathlib
+import subprocess
+import sys
+
+import pytest
+
+import freesurfer_surface
+
+_VERSION_MODULE_PATH = pathlib.Path(__file__).parent.parent.joinpath(
+    "freesurfer_surface", "version.py"
+)
+
+
+def test_version():
+    if not _VERSION_MODULE_PATH.exists():
+        pytest.skip("package is not installed")
+    assert re.match(r"^\d+\.\d+\.\d+", freesurfer_surface.__version__)
+
+
+def test_version_missing(tmp_path):
+    temp_module_path = tmp_path.joinpath("version.py")
+    if _VERSION_MODULE_PATH.exists():
+        _VERSION_MODULE_PATH.rename(temp_module_path)
+    try:
+        assert (
+            subprocess.run(
+                [
+                    sys.executable,
+                    "-c",
+                    "import freesurfer_surface; print(freesurfer_surface.__version__)",
+                ],
+                check=True,
+                stdout=subprocess.PIPE,
+            ).stdout.rstrip()
+            == b"None"
+        )
+    finally:
+        if temp_module_path.exists():
+            temp_module_path.rename(_VERSION_MODULE_PATH)