Repository: DakaraProject/dakara-base

Issue:
Mutualize check_version

Hints from issue comments:

Solution patch:
diff --git a/README.md b/README.md
index befc6d5..883bbc9 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,7 @@ This project is a collection of tools and helper modules for the Dakara Project.
 * `progress_bar`: a collection of progress bars;
 * `safe_workers`: a library to facilitate the manipulation of threads;
 * `utils`: other various helpers;
+- `version_check`: a checker for the version of a project;
 * `websocket_client`: a Websocket client.
 
 ## Install
diff --git a/src/dakara_base/version_check.py b/src/dakara_base/version_check.py
new file mode 100644
index 0000000..13525fc
--- /dev/null
+++ b/src/dakara_base/version_check.py
@@ -0,0 +1,33 @@
+"""
+Version checker module.
+
+This module provides the `check_version` function that displays the current
+version and checks if it is not a prerelease.
+"""
+
+import logging
+
+from packaging.version import parse
+
+logger_default = logging.getLogger(__name__)
+
+
+def check_version(project, version, date, logger=None):
+    """Display version number and check if on release.
+
+    Args:
+        project (str): Name of the project (without "Dakara" prefix).
+        version (str): Version of the project.
+        date (str): Date of the version.
+        logger (logging.logger): Logger to use, default to this file's logger.
+    """
+    if logger is None:
+        logger = logger_default
+
+    # log version
+    logger.info("Dakara %s %s (%s)", project, version, date)
+
+    # check version is a release
+    version_parsed = parse(version)
+    if version_parsed.is_prerelease:
+        logger.warning("You are running a dev version, use it at your own risks!")


Test patch:
diff --git a/tests/test_version_check.py b/tests/test_version_check.py
new file mode 100644
index 0000000..01e9726
--- /dev/null
+++ b/tests/test_version_check.py
@@ -0,0 +1,48 @@
+import logging
+from unittest import TestCase
+
+from dakara_base.version_check import check_version
+
+
+class CheckVersionTestCase(TestCase):
+    """Test the version checker."""
+
+    def test_check_version_release(self):
+        """Test to display the version for a release."""
+        with self.assertLogs("dakara_base.version_check", "DEBUG") as logger:
+            check_version("my project", "0.0.0", "1970-01-01")
+
+        # assert effect on logs
+        self.assertListEqual(
+            logger.output,
+            ["INFO:dakara_base.version_check:Dakara my project 0.0.0 (1970-01-01)"],
+        )
+
+    def test_check_version_release_logger(self):
+        """Test to display the version for a release using a custom logger."""
+        local_logger = logging.getLogger("my_logger")
+
+        with self.assertLogs("my_logger", "DEBUG") as logger:
+            check_version("my project", "0.0.0", "1970-01-01", local_logger)
+
+        # assert effect on logs
+        self.assertListEqual(
+            logger.output,
+            ["INFO:my_logger:Dakara my project 0.0.0 (1970-01-01)"],
+        )
+
+    def test_check_version_non_release(self):
+        """Test to display the version for a non release."""
+        with self.assertLogs("dakara_base.version_check", "DEBUG") as logger:
+            check_version("my project", "0.1.0-dev", "1970-01-01")
+
+        # assert effect on logs
+        self.assertListEqual(
+            logger.output,
+            [
+                "INFO:dakara_base.version_check:Dakara my project 0.1.0-dev "
+                "(1970-01-01)",
+                "WARNING:dakara_base.version_check:"
+                "You are running a dev version, use it at your own risks!",
+            ],
+        )