From e1b55ec23cced14562980f83929a50b8358e99f1 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Fri, 25 Oct 2024 10:56:11 -0400 Subject: [PATCH] add unit test --- tests/common.py | 32 +++++++++++++++++++++++++++++++ tests/test_lib/data/cfg/uni1.conf | 7 +++++++ tests/test_lib/test_utils.py | 24 ++++++++++++++++++----- 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 tests/common.py create mode 100644 tests/test_lib/data/cfg/uni1.conf diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 00000000..47c4fdb7 --- /dev/null +++ b/tests/common.py @@ -0,0 +1,32 @@ +import locale +from functools import wraps + + +class mylocale: + """Locale context manager. Resets to user default at exit.""" + + def __init__(self, value=None): + self.value = value + + def __enter__(self): + locale.setlocale(locale.LC_ALL, locale=self.value) + + def __exit__(self, _type, value, traceback): + # This resets to user default. Implementing a proper save and restore + # for locale is quite tricky, and this serves our testing needs. + locale.setlocale(locale.LC_ALL, "") + # return false so we don't eat exceptions + return False + + def __call__(self, func): + """When called, act as a decorator""" + + @wraps(func) + def wrapped(*args, **kwargs): + with self: + return func(*args, **kwargs) + + return wrapped + + +# the end diff --git a/tests/test_lib/data/cfg/uni1.conf b/tests/test_lib/data/cfg/uni1.conf new file mode 100644 index 00000000..e5b76ea9 --- /dev/null +++ b/tests/test_lib/data/cfg/uni1.conf @@ -0,0 +1,7 @@ +# comment ěščřčž +[koji] +# key_file = äöüß +; test comment with unicode 🫠 +# test comment with unicode ☢️ +setting = hello +value = world 🌍 diff --git a/tests/test_lib/test_utils.py b/tests/test_lib/test_utils.py index 42dd145e..9d59ba1d 100644 --- a/tests/test_lib/test_utils.py +++ b/tests/test_lib/test_utils.py @@ -3,7 +3,6 @@ from __future__ import absolute_import import calendar from datetime import datetime import errno -import locale import logging import tempfile import threading @@ -26,6 +25,7 @@ import six import koji import koji.util +from ..common import mylocale class EnumTestCase(unittest.TestCase): @@ -407,6 +407,23 @@ class ConfigFileTestCase(unittest.TestCase): self.assertEqual(listdir_mock.call_count, 2) +class ConfigFileTestCase2(unittest.TestCase): + """Additional tests for config file reading functions""" + + def setUp(self): + self.datadir = os.path.dirname(__file__) + '/data/cfg' + + def tearDown(self): + mock.patch.stopall() + + def test_unicode(self): + fn = self.datadir + '/uni1.conf' + if not os.path.exists(fn): + raise Exception('missing config') + with mylocale(value='C'): + koji.read_config_files(fn) + + class MavenUtilTestCase(unittest.TestCase): """Test maven relative functions""" maxDiff = None @@ -750,10 +767,9 @@ class MavenUtilTestCase(unittest.TestCase): config.read_file(conf_file) return config + @mylocale(('en_US', 'UTF-8')) def test_formatChangelog(self): """Test formatChangelog function""" - # force locale to compare 'expect' value - locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8')) data = [ { 'author': 'Happy Koji User - 1.1-1', @@ -787,8 +803,6 @@ class MavenUtilTestCase(unittest.TestCase): result = koji.util.formatChangelog(data) self.assertMultiLineEqual(expect, result) - locale.setlocale(locale.LC_ALL, "") - def test_parseTime(self): """Test parseTime function""" now = datetime.now()