jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1026462?usp=email )

Change subject: [tests] Add version_tests.py
......................................................................

[tests] Add version_tests.py

Currently test_nightly_version method will fail until T363943 is solved.

Bug: T363943
Change-Id: Ie3c26c84927d4561fadb2b23bcd94126f8216b6f
---
M docs/tests_ref/index.rst
A docs/tests_ref/version_tests.rst
M pywikibot/version.py
A tests/data/version
A tests/version_tests.py
5 files changed, 88 insertions(+), 3 deletions(-)

Approvals:
  Zhuyifei1999: Looks good to me, approved
  Framawiki: Looks good to me, but someone else must approve
  jenkins-bot: Verified




diff --git a/docs/tests_ref/index.rst b/docs/tests_ref/index.rst
index 7340817..2072f0e 100644
--- a/docs/tests_ref/index.rst
+++ b/docs/tests_ref/index.rst
@@ -81,6 +81,7 @@
     ui<./ui_tests>
     user<./user_tests>
     utils
+    version<./version_tests>
     wikibase_edit<./wikibase_edit_tests>
     wikibase<./wikibase_tests>
     wikistats<./wikistats_tests>
diff --git a/docs/tests_ref/version_tests.rst b/docs/tests_ref/version_tests.rst
new file mode 100644
index 0000000..d08e1be
--- /dev/null
+++ b/docs/tests_ref/version_tests.rst
@@ -0,0 +1,8 @@
+***************************
+tests.version\_tests module
+***************************
+
+.. automodule:: tests.version_tests
+    :members:
+    :undoc-members:
+    :show-inheritance:
diff --git a/pywikibot/version.py b/pywikibot/version.py
index 542c480..db6a084 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -241,7 +241,7 @@
     return (tag, rev, date, hsh)


-def getversion_nightly(path: str | Path | None = None):  # pragma: no cover
+def getversion_nightly(path: str | Path | None = None):
     """Get version info for a nightly release.

     .. hint::
@@ -256,7 +256,10 @@
         - hash (git hash for the current revision)
     :rtype: ``tuple`` of three ``str`` and a ``time.struct_time``
     """
-    file = Path(path or _get_program_dir()) / 'pywikibot' / 'version'
+    file = Path(path or _get_program_dir())
+    if not path:
+        file /= 'pywikibot'
+    file /= 'version'

     with file.open() as data:
         (tag, rev, date, hsh) = data.readlines()
@@ -287,7 +290,7 @@
     return (tag, rev, date, hsh)


-def getversion_onlinerepo(path: str = 'branches/master'):
+def getversion_onlinerepo(path: str = 'branches/master') -> str:
     """Retrieve current framework git hash from Gerrit."""
     # Gerrit API responses include )]}' at the beginning,
     # make sure to strip it out
diff --git a/tests/data/version b/tests/data/version
new file mode 100644
index 0000000..91f8372
--- /dev/null
+++ b/tests/data/version
@@ -0,0 +1,4 @@
+nightly/core_stable
+1
+2024-05-02T01:04:51
+e8f64f2
diff --git a/tests/version_tests.py b/tests/version_tests.py
new file mode 100644
index 0000000..c9364f5
--- /dev/null
+++ b/tests/version_tests.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+"""Test cases for the :mod:`version` module.
+
+.. versionadded:: 9.2
+"""
+#
+# (C) Pywikibot team, 2024
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import annotations
+
+import time
+import unittest
+from contextlib import suppress
+from pathlib import Path
+
+from pywikibot import version
+from tests.aspects import TestCase
+
+
+class LocalVersionTestCase(TestCase):
+
+    """Test local version infomation."""
+
+    net = False
+
+    @unittest.expectedFailure  # T363943
+    def test_nightly_version(self):
+        """Test version file of nightly dump."""
+        path = Path(__file__).parent / 'data'
+        tag, rev, date, hsh, *dummy = version.getversion_nightly(path)
+        self.assertEqual(tag, 'nightly/core')
+        self.assertEqual(rev, '1')
+        self.assertIsInstance(date, time.struct_time)
+        self.assertEqual(hsh, '567a31d')
+        self.assertEqual(dummy, [])
+
+    def test_package_version(self):
+        """Test package version."""
+        tag, rev, date, hsh, *dummy = version.getversion_package()
+        self.assertEqual(tag, 'pywikibot/__init__.py')
+        self.assertEqual(rev, '-1 (unknown)')
+        self.assertIsInstance(date, time.struct_time)
+        self.assertEqual(hsh, '')
+        self.assertEqual(dummy, [])
+
+
+class RemoteVersionTestCase(TestCase):
+
+    """Test remote version infomation."""
+
+    net = True
+
+    def test_onlinerepo_version(self):
+        """Test online repository hash."""
+        for branch in ('master', 'stable'):
+            with self.subTest(branch=branch):
+                hsh = version.getversion_onlinerepo('branches/' + branch)
+                try:
+                    int(hsh, 16)
+                except ValueError:
+                    self.fail(
+                        f'{hsh!r} is not a valid hash of {branch} branch')
+
+
+if __name__ == '__main__':
+    with suppress(SystemExit):
+        unittest.main()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1026462?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie3c26c84927d4561fadb2b23bcd94126f8216b6f
Gerrit-Change-Number: 1026462
Gerrit-PatchSet: 6
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Framawiki <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to