]> Nutra Git (v1) - nutratech/cli.git/commitdiff
make testable function: version_check()
authorShane Jaroch <chown_tee@proton.me>
Fri, 12 Apr 2024 21:49:37 +0000 (17:49 -0400)
committerShane Jaroch <chown_tee@proton.me>
Fri, 12 Apr 2024 21:50:28 +0000 (17:50 -0400)
ntclient/__init__.py
tests/test_init.py

index 21e6d0904b8520b796c2d9acd41a393cef33b962..cf75d8b3bbe6234c23c40877c2703a8fee89cfdc 100644 (file)
@@ -38,17 +38,28 @@ USDA_DB_NAME = "usda.sqlite3"
 NTSQLITE_BUILDPATH = os.path.join(PROJECT_ROOT, "ntsqlite", "sql", NT_DB_NAME)
 NTSQLITE_DESTINATION = os.path.join(NUTRA_HOME, NT_DB_NAME)
 
-# Check Python version
+
+def version_check() -> None:
+    """Check Python version"""
+    # pylint: disable=global-statement
+    global PY_SYS_VER, PY_SYS_STR
+    PY_SYS_VER = sys.version_info[0:3]
+    PY_SYS_STR = ".".join(str(x) for x in PY_SYS_VER)
+
+    if PY_SYS_VER < PY_MIN_VER:
+        # TODO: make this testable with: `class CliConfig`?
+        raise RuntimeError(
+            "ERROR: %s requires Python %s or later to run" % (__title__, PY_MIN_STR),
+            "HINT:  You're running Python %s" % PY_SYS_STR,
+        )
+
+
 PY_MIN_VER = (3, 4, 3)
 PY_SYS_VER = sys.version_info[0:3]
 PY_MIN_STR = ".".join(str(x) for x in PY_MIN_VER)
 PY_SYS_STR = ".".join(str(x) for x in PY_SYS_VER)
-if PY_SYS_VER < PY_MIN_VER:
-    # TODO: make this testable with: `class CliConfig`?
-    raise RuntimeError(
-        "ERROR: %s requires Python %s or later to run" % (__title__, PY_MIN_STR),
-        "HINT:  You're running Python %s" % PY_SYS_STR,
-    )
+# Run the check
+version_check()
 
 # Console size, don't print more than it
 BUFFER_WD = shutil.get_terminal_size()[0]
index 5483b88b2b328267ea1021a56112f71a5ce80e6b..cc938faf6ba69f9723b245ad1da069f19a9125d3 100644 (file)
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
 Created on Fri Apr 12 17:30:01 2024
@@ -9,17 +8,14 @@ from unittest.mock import patch
 
 import pytest
 
+from ntclient import version_check
+
 
 @patch("sys.version_info", (3, 4, 0))
 def test_archaic_python_version_raises_runtime_error() -> None:
     """Test that the correct error is raised when the Python version is too low."""
     with pytest.raises(RuntimeError) as exc_info:
-        # pylint: disable=import-outside-toplevel
-        from ntclient import PY_MIN_VER, PY_SYS_VER, __title__
-
-        assert __title__ == "nutra"
-        assert PY_MIN_VER == (3, 4, 3)
-        assert PY_SYS_VER == (3, 4, 0)
+        version_check()
 
     assert "ERROR: nutra requires Python 3.4.3 or later to run" in str(exc_info.value)
     assert "HINT:  You're running Python 3.4.0" in str(exc_info.value)