jenkins-bot merged this change.

View Change

Approvals: Framawiki: Looks good to me, approved jenkins-bot: Verified
[tests] Add tests for generate_user_files.py

- unittest fails with "StringIO instance has no attribute 'encoding'" error
when importing the generate_user_files.py script. Catch this exception.

Bug: T193970
Change-Id: I773d45fa7c7940464e795418057c9fa64041ba37
---
M generate_user_files.py
A tests/generate_user_files_tests.py
2 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/generate_user_files.py b/generate_user_files.py
index 77bd611..2288e4b 100755
--- a/generate_user_files.py
+++ b/generate_user_files.py
@@ -32,7 +32,13 @@
os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = _orig_no_user_config

base_dir = pywikibot.config2.base_dir
-console_encoding = sys.stdout.encoding
+
+try:
+ console_encoding = sys.stdout.encoding
+# unittests fails with "StringIO instance has no attribute 'encoding'"
+except AttributeError:
+ console_encoding = None
+
# the directory in which generate_user_files.py is located
pywikibot_dir = sys.path[0]

diff --git a/tests/generate_user_files_tests.py b/tests/generate_user_files_tests.py
new file mode 100644
index 0000000..743543a
--- /dev/null
+++ b/tests/generate_user_files_tests.py
@@ -0,0 +1,75 @@
+# -*- coding: utf-8 -*-
+"""Test generate_user_files script."""
+#
+# (C) Pywikibot team, 2018
+#
+# Distributed under the terms of the MIT license.
+#
+from __future__ import absolute_import, unicode_literals
+
+import re
+
+from tests.aspects import unittest, TestCase
+
+import generate_user_files as guf
+
+
+class TestGenerateUserFiles(TestCase):
+
+ """Test generate_user_files.py functionality."""
+
+ dry = True
+
+ def test_ask_for_dir_change(self):
+ """Test ask_for_dir_change function."""
+ userfile, passfile = guf.ask_for_dir_change(force=True)
+ self.assertIsInstance(userfile, bool)
+ self.assertIsInstance(passfile, bool)
+
+ def test_base_names(self):
+ """Test basename constants."""
+ self.assertTrue(guf.USER_BASENAME.endswith('.py'))
+ self.assertTrue(guf.PASS_BASENAME.endswith('.py'))
+
+ def test_config_test(self):
+ """Test config text strings."""
+ args = {'main_family': '', 'main_code': '', 'usernames': '',
+ 'botpasswords': ''}
+ config_text = guf.SMALL_CONFIG.format(**args)
+ self.assertEqual(config_text,
+ re.sub('{[a-z_]+}', '', guf.SMALL_CONFIG))
+ args['config_text'] = ''
+ config_text = guf.EXTENDED_CONFIG.format(**args)
+ self.assertEqual(config_text,
+ re.sub('{[a-z_]+}', '', guf.EXTENDED_CONFIG))
+ config_text = guf.PASSFILE_CONFIG.format(botpasswords='')
+ self.assertEqual(config_text,
+ re.sub('{[a-z_]+}', '', guf.PASSFILE_CONFIG))
+
+ def test_file_exists(self):
+ """Test file_exists function."""
+ self.assertFalse(guf.file_exists('This file does not exist'))
+ self.assertTrue(guf.file_exists('pwb.py'))
+
+ def test_default_get_site_and_lang(self):
+ """Test get_site_and_lang function with defaults."""
+ family, code, user = guf.get_site_and_lang(force=True)
+ self.assertEqual(family, 'wikipedia')
+ self.assertEqual(code, 'en')
+ self.assertIsNone(user)
+
+ def test_get_site_and_lang(self):
+ """Test get_site_and_lang function with parameters."""
+ family, code, user = guf.get_site_and_lang(
+ default_family='test', default_lang='foo', default_username='bar',
+ force=True)
+ self.assertEqual(family, 'test')
+ self.assertEqual(code, 'test')
+ self.assertEqual(user, 'bar')
+
+
+if __name__ == '__main__': # pragma: no cover
+ try:
+ unittest.main()
+ except SystemExit:
+ pass

To view, visit change 430088. To unsubscribe, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I773d45fa7c7940464e795418057c9fa64041ba37
Gerrit-Change-Number: 430088
Gerrit-PatchSet: 8
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Framawiki <framawiki@tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444@gmail.com>
Gerrit-Reviewer: jenkins-bot <>