Skip to content

Commit 8862ee3

Browse files
committed
Fix test failures on Windows
The test failures on Windows (which are not present on Linux) are caused by the test code using simple string compare (through dict) when simulating files. On Windows, paths are formed with "\" instead of "/", which causes inconsistency in the string compare. (Specifically, the "\" were inserted by os.path.join in FluentResourceLoader.)
1 parent df5ef40 commit 8862ee3

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

fluent.runtime/tests/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
3+
# Unify path separator, default path separator on Windows is \ not /
4+
# Needed because many tests uses dict + string compare to make a virtual file structure
5+
def normalize_path(path):
6+
return "/".join(os.path.split(path))

fluent.runtime/tests/test_fallback.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33
import unittest
44
from unittest import mock
55

6+
from . import normalize_path
7+
68
from fluent.runtime import FluentLocalization, FluentResourceLoader
79

810
ISFILE = os.path.isfile
911

12+
def show_bundle(bundle):
13+
for name in ["one", "two", "three", "four", "five"]:
14+
if bundle.has_message(name):
15+
print(name + ":", bundle.format_pattern(bundle.get_message(name).value))
16+
else:
17+
print(name + ": Not present")
18+
1019

1120
class TestLocalization(unittest.TestCase):
1221
def test_init(self):
@@ -25,22 +34,28 @@ def test_bundles(self, codecs_open, isfile):
2534
"en/one.ftl": "four = exists",
2635
"en/two.ftl": "five = exists",
2736
}
28-
isfile.side_effect = lambda p: p in data or ISFILE(p)
29-
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[p])
37+
#isfile.side_effect = lambda p: p in data or ISFILE(p)
38+
isfile.side_effect = lambda p: normalize_path(p) in data or ISFILE(p)
39+
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[normalize_path(p)])
3040
l10n = FluentLocalization(
3141
["de", "fr", "en"], ["one.ftl", "two.ftl"], FluentResourceLoader("{locale}")
3242
)
43+
# Curious
44+
print("\ntest_bundles")
3345
bundles_gen = l10n._bundles()
3446
bundle_de = next(bundles_gen)
47+
show_bundle(bundle_de)
3548
self.assertEqual(bundle_de.locales[0], "de")
3649
self.assertTrue(bundle_de.has_message("one"))
3750
self.assertTrue(bundle_de.has_message("two"))
3851
bundle_fr = next(bundles_gen)
52+
show_bundle(bundle_fr)
3953
self.assertEqual(bundle_fr.locales[0], "fr")
4054
self.assertFalse(bundle_fr.has_message("one"))
4155
self.assertTrue(bundle_fr.has_message("three"))
4256
self.assertListEqual(list(l10n._bundles())[:2], [bundle_de, bundle_fr])
4357
bundle_en = next(bundles_gen)
58+
show_bundle(bundle_en)
4459
self.assertEqual(bundle_en.locales[0], "en")
4560
self.assertEqual(l10n.format_value("one"), "in German")
4661
self.assertEqual(l10n.format_value("two"), "in German")
@@ -57,8 +72,8 @@ def test_all_exist(self, codecs_open, isfile):
5772
"en/one.ftl": "one = exists",
5873
"en/two.ftl": "two = exists",
5974
}
60-
isfile.side_effect = lambda p: p in data
61-
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[p])
75+
isfile.side_effect = lambda p: normalize_path(p) in data
76+
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[normalize_path(p)])
6277
loader = FluentResourceLoader("{locale}")
6378
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
6479
self.assertEqual(len(resources_list), 1)
@@ -69,8 +84,8 @@ def test_one_exists(self, codecs_open, isfile):
6984
data = {
7085
"en/two.ftl": "two = exists",
7186
}
72-
isfile.side_effect = lambda p: p in data
73-
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[p])
87+
isfile.side_effect = lambda p: normalize_path(p) in data
88+
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[normalize_path(p)])
7489
loader = FluentResourceLoader("{locale}")
7590
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
7691
self.assertEqual(len(resources_list), 1)
@@ -79,8 +94,8 @@ def test_one_exists(self, codecs_open, isfile):
7994

8095
def test_none_exist(self, codecs_open, isfile):
8196
data = {}
82-
isfile.side_effect = lambda p: p in data
83-
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[p])
97+
isfile.side_effect = lambda p: normalize_path(p) in data
98+
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[normalize_path(p)])
8499
loader = FluentResourceLoader("{locale}")
85100
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
86101
self.assertEqual(len(resources_list), 0)

0 commit comments

Comments
 (0)