From 16f1b6581573f829b7cbb0cde71ba8b1828e6332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Fri, 31 Jul 2020 15:42:29 +0200 Subject: [PATCH 1/5] Improve default locale path discovering. --- src/humanize/i18n.py | 15 +++++++++------ tests/test_i18n.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/humanize/i18n.py b/src/humanize/i18n.py index d3f51db..b250412 100644 --- a/src/humanize/i18n.py +++ b/src/humanize/i18n.py @@ -7,11 +7,14 @@ _TRANSLATIONS = {None: gettext_module.NullTranslations()} _CURRENT = local() -try: - _DEFAULT_LOCALE_PATH = os.path.join(os.path.dirname(__file__), "locale") -except AttributeError: - # in case that __file__ does not exist - _DEFAULT_LOCALE_PATH = None + +def get_default_locale_path(): + if __file__ is None: + return + try: + return os.path.join(os.path.dirname(__file__), "locale") + except NameError: + return def get_translation(): @@ -26,7 +29,7 @@ def activate(locale, path=None): @param locale: language name, eg 'en_GB' @param path: path to search for locales""" if path is None: - path = _DEFAULT_LOCALE_PATH + path = get_default_locale_path() if path is None: raise Exception( diff --git a/tests/test_i18n.py b/tests/test_i18n.py index fe95d27..1c00b92 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -1,6 +1,8 @@ import datetime as dt +import importlib import humanize +import pytest def test_i18n(): @@ -17,3 +19,38 @@ def test_i18n(): humanize.i18n.deactivate() assert humanize.naturaltime(three_seconds) == "3 seconds ago" assert humanize.ordinal(5) == "5th" + + +def test_i18n_default_locale_path(): + # __file__ defined + i18n = importlib.import_module("humanize.i18n") + assert i18n.get_default_locale_path() is not None + + # __file__ is None + i18n.__file__ = None + assert i18n.get_default_locale_path() is None + + # __file__ not defined + del i18n.__file__ + with pytest.raises(NameError) as excinfo: + i18n.get_default_locale_path() + assert str(excinfo.value) == "name '__file__' is not defined" + + +def test_activate_without_default_locale_path(): + # __file__ is None + i18n = importlib.import_module("humanize.i18n") + i18n.__file__ = None + expected_msg = ( + "Humanize cannot determinate the default location of the" + " 'locale' folder. You need to pass the path explicitly." + ) + with pytest.raises(Exception) as excinfo: + i18n.activate("ru_RU") + assert str(excinfo.value) == expected_msg + + # __file__ not defined + del i18n.__file__ + with pytest.raises(Exception) as excinfo: + i18n.activate("ru_RU") + assert str(excinfo.value) == "name '__file__' is not defined" From 2d891b89ebfb6a33e98ecfdd718fc6e50b4a263f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Fri, 31 Jul 2020 15:59:12 +0200 Subject: [PATCH 2/5] Fix not defined error. --- src/humanize/i18n.py | 4 ++-- tests/test_i18n.py | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/humanize/i18n.py b/src/humanize/i18n.py index b250412..70c05d7 100644 --- a/src/humanize/i18n.py +++ b/src/humanize/i18n.py @@ -9,9 +9,9 @@ def get_default_locale_path(): - if __file__ is None: - return try: + if __file__ is None: + return return os.path.join(os.path.dirname(__file__), "locale") except NameError: return diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 1c00b92..661ec0d 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -32,19 +32,19 @@ def test_i18n_default_locale_path(): # __file__ not defined del i18n.__file__ - with pytest.raises(NameError) as excinfo: - i18n.get_default_locale_path() - assert str(excinfo.value) == "name '__file__' is not defined" + i18n.get_default_locale_path() is None def test_activate_without_default_locale_path(): - # __file__ is None - i18n = importlib.import_module("humanize.i18n") - i18n.__file__ = None expected_msg = ( "Humanize cannot determinate the default location of the" " 'locale' folder. You need to pass the path explicitly." ) + + # __file__ is None + i18n = importlib.import_module("humanize.i18n") + i18n.__file__ = None + with pytest.raises(Exception) as excinfo: i18n.activate("ru_RU") assert str(excinfo.value) == expected_msg @@ -53,4 +53,4 @@ def test_activate_without_default_locale_path(): del i18n.__file__ with pytest.raises(Exception) as excinfo: i18n.activate("ru_RU") - assert str(excinfo.value) == "name '__file__' is not defined" + assert str(excinfo.value) == expected_msg From 6def8ce43dcfa872e1e0af5e5c4bea21e0c238a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Thu, 6 Aug 2020 14:02:22 +0200 Subject: [PATCH 3/5] Return None explicitly. --- src/humanize/i18n.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/humanize/i18n.py b/src/humanize/i18n.py index 70c05d7..2fdb4b2 100644 --- a/src/humanize/i18n.py +++ b/src/humanize/i18n.py @@ -11,10 +11,10 @@ def get_default_locale_path(): try: if __file__ is None: - return + return None return os.path.join(os.path.dirname(__file__), "locale") except NameError: - return + return None def get_translation(): From aba0ea6870559ae9852159c63d66d1a88c4865e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Thu, 6 Aug 2020 14:16:44 +0200 Subject: [PATCH 4/5] Split new i18n tests into separate functions. --- tests/test_i18n.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 661ec0d..a747046 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -21,36 +21,41 @@ def test_i18n(): assert humanize.ordinal(5) == "5th" -def test_i18n_default_locale_path(): - # __file__ defined +def test_default_locale_path_defined__file__(): i18n = importlib.import_module("humanize.i18n") assert i18n.get_default_locale_path() is not None - # __file__ is None + +def test_default_locale_path_null__file__(): + i18n = importlib.import_module("humanize.i18n") i18n.__file__ = None assert i18n.get_default_locale_path() is None - # __file__ not defined + +def test_default_locale_path_undefined__file__(): + i18n = importlib.import_module("humanize.i18n") del i18n.__file__ i18n.get_default_locale_path() is None -def test_activate_without_default_locale_path(): +class TestActivate: expected_msg = ( "Humanize cannot determinate the default location of the" " 'locale' folder. You need to pass the path explicitly." ) - # __file__ is None - i18n = importlib.import_module("humanize.i18n") - i18n.__file__ = None + def test_default_locale_path_null__file__(self): + i18n = importlib.import_module("humanize.i18n") + i18n.__file__ = None - with pytest.raises(Exception) as excinfo: - i18n.activate("ru_RU") - assert str(excinfo.value) == expected_msg + with pytest.raises(Exception) as excinfo: + i18n.activate("ru_RU") + assert str(excinfo.value) == self.expected_msg - # __file__ not defined - del i18n.__file__ - with pytest.raises(Exception) as excinfo: - i18n.activate("ru_RU") - assert str(excinfo.value) == expected_msg + def test_default_locale_path_undefined__file__(self): + i18n = importlib.import_module("humanize.i18n") + del i18n.__file__ + + with pytest.raises(Exception) as excinfo: + i18n.activate("ru_RU") + assert str(excinfo.value) == self.expected_msg From 7de59b64f47440bcfb817cfbb26c119430d2cfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar?= Date: Mon, 10 Aug 2020 17:50:52 +0200 Subject: [PATCH 5/5] Make 'get_default_locale_path' function private. --- src/humanize/i18n.py | 4 ++-- tests/test_i18n.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/humanize/i18n.py b/src/humanize/i18n.py index 2fdb4b2..c044d8d 100644 --- a/src/humanize/i18n.py +++ b/src/humanize/i18n.py @@ -8,7 +8,7 @@ _CURRENT = local() -def get_default_locale_path(): +def _get_default_locale_path(): try: if __file__ is None: return None @@ -29,7 +29,7 @@ def activate(locale, path=None): @param locale: language name, eg 'en_GB' @param path: path to search for locales""" if path is None: - path = get_default_locale_path() + path = _get_default_locale_path() if path is None: raise Exception( diff --git a/tests/test_i18n.py b/tests/test_i18n.py index a747046..f3fe2d4 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -23,19 +23,19 @@ def test_i18n(): def test_default_locale_path_defined__file__(): i18n = importlib.import_module("humanize.i18n") - assert i18n.get_default_locale_path() is not None + assert i18n._get_default_locale_path() is not None def test_default_locale_path_null__file__(): i18n = importlib.import_module("humanize.i18n") i18n.__file__ = None - assert i18n.get_default_locale_path() is None + assert i18n._get_default_locale_path() is None def test_default_locale_path_undefined__file__(): i18n = importlib.import_module("humanize.i18n") del i18n.__file__ - i18n.get_default_locale_path() is None + i18n._get_default_locale_path() is None class TestActivate: