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

Change subject: IMPR: use pathlib methods in add_text script
......................................................................

IMPR: use pathlib methods in add_text script

Also update tests accordingly

Bug: T395187
Change-Id: Ifb48e9663590b05cf305a38a6f8ba636f09c1f78
---
M scripts/add_text.py
M tests/add_text_tests.py
2 files changed, 28 insertions(+), 12 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/scripts/add_text.py b/scripts/add_text.py
index 9e84fd1..92639f8 100755
--- a/scripts/add_text.py
+++ b/scripts/add_text.py
@@ -63,14 +63,14 @@
    -summary:"Bot: Aggiungo template Categorizzare"
 """
 #
-# (C) Pywikibot team, 2007-2024
+# (C) Pywikibot team, 2007-2025
 #
 # Distributed under the terms of the MIT license.
 #
 from __future__ import annotations

-import codecs
 import re
+from pathlib import Path

 import pywikibot
 from pywikibot import config, pagegenerators, textlib
@@ -122,9 +122,8 @@
     def setup(self) -> None:
         """Read text to be added from file."""
         if self.opt.textfile:
-            with codecs.open(self.opt.textfile, 'r',
-                             config.textfile_encoding) as f:
-                self.opt.text = f.read()
+            pth = Path(self.opt.textfile)
+            self.opt.text = pth.read_text(encoding=config.textfile_encoding)
         else:
             # Translating the \\n into binary \n if given from command line
             self.opt.text = self.opt.text.replace('\\n', '\n')
diff --git a/tests/add_text_tests.py b/tests/add_text_tests.py
index bab5a14..6d1d237 100755
--- a/tests/add_text_tests.py
+++ b/tests/add_text_tests.py
@@ -1,21 +1,26 @@
 #!/usr/bin/env python3
 """Test add_text script."""
 #
-# (C) Pywikibot team, 2016-2022
+# (C) Pywikibot team, 2016-2025
 #
 # Distributed under the terms of the MIT license.
 #
 from __future__ import annotations

 import unittest
-from unittest.mock import ANY, MagicMock, Mock, mock_open, patch
+from pathlib import Path
+from unittest.mock import MagicMock, Mock, mock_open, patch

 import pywikibot
 import pywikibot.pagegenerators
+from pywikibot.tools import PYTHON_VERSION
 from scripts.add_text import AddTextBot, main, parse
 from tests.aspects import TestCase


+PYTHON_310 = PYTHON_VERSION[:2] == (3, 10)
+
+
 def _mock_page(exists=True, redirect=False, talk=False, url='wikipedia.org'):
     """Provides a page with these attributes."""
     page = MagicMock()
@@ -136,18 +141,30 @@
         bot.setup()
         self.assertEqual('hello\nworld', bot.opt.text)

-    @patch('builtins.open', new_callable=mock_open, read_data=b'file data')
-    def test_setup_with_textfile(self, mock_file):
+    def common_setup_test_with_textfile(self, mock_file):
         """Exercise both with a -textfile argument."""
         bot = AddTextBot(textfile='/path/to/my/file.txt')
-
         # setup reads the file content
-
         self.assertEqual('', bot.opt.text)
         bot.setup()
         self.assertEqual('file data', bot.opt.text)
+        mock_file.assert_called_once()
+        self.assertEqual(mock_file.call_args.args[:3],
+                         (Path('/path/to/my/file.txt'), 'r', -1))

-        mock_file.assert_called_with('/path/to/my/file.txt', 'rb', ANY)
+    @unittest.skipUnless(PYTHON_310,
+                         f'Test for Python 3.10 but {PYTHON_VERSION} given')
+    @patch('pathlib.Path._accessor.open', new_callable=mock_open,
+           read_data='file data')
+    def test_textfile_py10(self, mock_file):
+        """Test with a -textfile argument for Python 3.10."""
+        self.common_setup_test_with_textfile(mock_file)
+
+    @unittest.skipIf(PYTHON_310, 'Test except for Python 3.10')
+    @patch('io.open', new_callable=mock_open, read_data='file data')
+    def test_textfile_other(self, mock_file):
+        """Test with a -textfile argument for Python != 3.10."""
+        self.common_setup_test_with_textfile(mock_file)

     def test_not_skipped(self):
         """Exercise skip_page() with a page we should accept."""

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

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

Reply via email to