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

Change subject: [test] Test replace.handle_pairs()
......................................................................

[test] Test replace.handle_pairs()

Check the following conditions:
- non existing file
- odd lines
- file with BOM
- file without BOM

Use utf-8-sig for encoding to ignore the byte order mark when reading.
The other way with lstrip() does not work.

Bug: T378647
Change-Id: Ic81f6a9485e125906358c3f2c08e1248169a791a
---
M .pre-commit-config.yaml
M scripts/replace.py
A tests/data/pairsfile BOM.txt
A tests/data/pairsfile.txt
M tests/replacebot_tests.py
5 files changed, 38 insertions(+), 4 deletions(-)

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




diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 1199d40..670c485 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -29,6 +29,7 @@
       - id: destroyed-symlinks
       - id: end-of-file-fixer
       - id: fix-byte-order-marker
+        exclude: '^tests/data/'
       - id: fix-encoding-pragma
         args:
           - --remove
diff --git a/scripts/replace.py b/scripts/replace.py
index d17bb8a..0a1daa6 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -848,20 +848,20 @@
             'Please enter the filename to read replacements from:')

     try:
-        replacements = Path(filename).read_text(encoding='utf-8').splitlines()
-        if not replacements:
+        # use utf-8-sig to ignore BOM
+        content = Path(filename).read_text(encoding='utf-8-sig')
+        if not content:
             raise OSError(f'{filename} is empty.')
     except OSError as e:
         pywikibot.error(f'Error loading {filename}: {e}')
         return None

+    replacements = content.splitlines()
     if len(replacements) % 2:
         pywikibot.error(f'{filename} contains an incomplete pattern '
                         f'replacement pair:\n{replacements}')
         return None

-    # Strip BOM from first line
-    replacements[0].lstrip('\uFEFF')
     return replacements


diff --git a/tests/data/pairsfile BOM.txt b/tests/data/pairsfile BOM.txt
new file mode 100644
index 0000000..8096d66
--- /dev/null
+++ b/tests/data/pairsfile BOM.txt
@@ -0,0 +1,4 @@
+Category:Notice Board Quests
+Категория:Задания с Доски объявлений
+Windbluff Tower Key
+Ключ от Крепости Ветров
diff --git a/tests/data/pairsfile.txt b/tests/data/pairsfile.txt
new file mode 100644
index 0000000..927e608
--- /dev/null
+++ b/tests/data/pairsfile.txt
@@ -0,0 +1,4 @@
+Category:Notice Board Quests
+Категория:Задания с Доски объявлений
+Windbluff Tower Key
+Ключ от Крепости Ветров
diff --git a/tests/replacebot_tests.py b/tests/replacebot_tests.py
index f7abe98..4b22ed1 100755
--- a/tests/replacebot_tests.py
+++ b/tests/replacebot_tests.py
@@ -303,6 +303,31 @@
             '"no-msg-callable" (all replacements)',
         ], pywikibot.bot.ui.pop_output())

+    def test_pairs_file(self):
+        """Test handle_pairsfile."""
+        result = replace.handle_pairsfile('non existing file')
+        self.assertIsNone(result)
+        self.assertIn("No such file or directory: 'non existing file'",
+                      pywikibot.bot.ui.pop_output()[0])
+
+        result = replace.handle_pairsfile('tests/data/pagelist-lines.txt')
+        self.assertIsNone(result)
+        self.assertIn('pagelist-lines.txt contains an incomplete pattern '
+                      "replacement pair:\n['file', 'bracket', ",
+                      pywikibot.bot.ui.pop_output()[0])
+
+        # check file with and without BOM
+        for variant in ('', ' BOM'):
+            result = replace.handle_pairsfile(
+                f'tests/data/pairsfile{variant}.txt')
+            self.assertIsEmpty(pywikibot.bot.ui.pop_output())
+            self.assertEqual(result, [
+                'Category:Notice Board Quests',
+                'Категория:Задания с Доски объявлений',
+                'Windbluff Tower Key',
+                'Ключ от Крепости Ветров'
+            ])
+

 if __name__ == '__main__':
     with suppress(SystemExit):

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1085374?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: Ic81f6a9485e125906358c3f2c08e1248169a791a
Gerrit-Change-Number: 1085374
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: D3r1ck01 <dalangi-...@wikimedia.org>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to