Skip to content

Fix test failures on windows #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor & add tests for file simulating code
  • Loading branch information
studyingegret committed Feb 6, 2025
commit 457d6980c8e26882da92f49d9c46d15fb4b3ab9e
7 changes: 0 additions & 7 deletions fluent.runtime/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
import os


# Unify path separator, default path separator on Windows is \ not /
# Needed in test_falllback.py because it uses dict + string compare to make a virtual file structure
def normalize_path(path):
return "/".join(os.path.split(path))
59 changes: 20 additions & 39 deletions fluent.runtime/tests/test_fallback.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import io
import os
import unittest
from unittest import mock

from . import normalize_path
from .utils import patch_files

from fluent.runtime import FluentLocalization, FluentResourceLoader

ISFILE = os.path.isfile


def patch_io(codecs_open, isfile, data):
isfile.side_effect = lambda p: normalize_path(p) in data or ISFILE(p)
codecs_open.side_effect = lambda p, _, __: io.StringIO(data[normalize_path(p)])


class TestLocalization(unittest.TestCase):
def test_init(self):
Expand All @@ -22,17 +11,14 @@ def test_init(self):
)
self.assertTrue(callable(l10n.format_value))

@mock.patch("os.path.isfile")
@mock.patch("codecs.open")
def test_bundles(self, codecs_open, isfile):
data = {
"de/one.ftl": "one = in German",
"de/two.ftl": "two = in German",
"fr/two.ftl": "three = in French",
"en/one.ftl": "four = exists",
"en/two.ftl": "five = exists",
}
patch_io(codecs_open, isfile, data)
@patch_files({
"de/one.ftl": "one = in German",
"de/two.ftl": "two = in German",
"fr/two.ftl": "three = in French",
"en/one.ftl": "four = exists",
"en/two.ftl": "five = exists",
})
def test_bundles(self):
l10n = FluentLocalization(
["de", "fr", "en"], ["one.ftl", "two.ftl"], FluentResourceLoader("{locale}")
)
Expand All @@ -56,35 +42,30 @@ def test_bundles(self, codecs_open, isfile):
self.assertEqual(l10n.format_value("five"), "exists")


@mock.patch("os.path.isfile")
@mock.patch("codecs.open")
class TestResourceLoader(unittest.TestCase):
def test_all_exist(self, codecs_open, isfile):
data = {
"en/one.ftl": "one = exists",
"en/two.ftl": "two = exists",
}
patch_io(codecs_open, isfile, data)
@patch_files({
"en/one.ftl": "one = exists",
"en/two.ftl": "two = exists",
})
def test_all_exist(self):
loader = FluentResourceLoader("{locale}")
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
self.assertEqual(len(resources_list), 1)
resources = resources_list[0]
self.assertEqual(len(resources), 2)

def test_one_exists(self, codecs_open, isfile):
data = {
"en/two.ftl": "two = exists",
}
patch_io(codecs_open, isfile, data)
@patch_files({
"en/two.ftl": "two = exists",
})
def test_one_exists(self):
loader = FluentResourceLoader("{locale}")
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
self.assertEqual(len(resources_list), 1)
resources = resources_list[0]
self.assertEqual(len(resources), 1)

def test_none_exist(self, codecs_open, isfile):
data = {}
patch_io(codecs_open, isfile, data)
@patch_files({})
def test_none_exist(self):
loader = FluentResourceLoader("{locale}")
resources_list = list(loader.resources("en", ["one.ftl", "two.ftl"]))
self.assertEqual(len(resources_list), 0)
39 changes: 39 additions & 0 deletions fluent.runtime/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from .utils import patch_files
import os
import codecs


class TestFileSimulate(unittest.TestCase):
def test_basic(self):
@patch_files({
"the.txt": "The",
"en/one.txt": "One",
"en/two.txt": "Two"
})
def patch_me(a, b):
self.assertEqual(a, 10)
self.assertEqual(b, "b")
self.assertFileIs(os.path.basename(__file__), None)
self.assertFileIs("the.txt", "The")
self.assertFileIs("en/one.txt", "One")
self.assertFileIs("en\\one.txt", "One")
self.assertFileIs("en/two.txt", "Two")
self.assertFileIs("en\\two.txt", "Two")
self.assertFileIs("en/three.txt", None)
self.assertFileIs("en\\three.txt", None)
patch_me(10, "b")

def assertFileIs(self, filename, expect_contents):
"""
expect_contents is None: Expect file does not exist
expect_contents is a str: Expect file contents to match
"""
if expect_contents is None:
self.assertFalse(os.path.isfile(filename),
"Expected " + filename + " to not exist.")
else:
self.assertTrue(os.path.isfile(filename),
"Expected " + filename + " to exist.")
self.assertEqual(codecs.open(filename, "r", "utf-8").read(),
expect_contents)
56 changes: 56 additions & 0 deletions fluent.runtime/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
"""Utilities for testing.

Import this module before patching libraries.
"""

import textwrap
from pathlib import PurePath
from unittest import mock
from io import StringIO
import functools


def dedent_ftl(text):
return textwrap.dedent(f"{text.rstrip()}\n")


# Unify path separator, default path separator on Windows is \ not /
# Supports only relative paths
# Needed in test_falllback.py because it uses dict + string compare to make a virtual file structure
def _normalize_path(path):
path = PurePath(path)
if path.is_absolute():
raise ValueError("Absolute paths are not supported in file simulation yet. ("
+ str(path) + ")")
if "." not in path.parts and ".." not in path.parts:
return "/".join(PurePath(path).parts)
else:
res_parts = []
length = len(path.parts)
i = 0
while i < length:
if path.parts[i] == ".":
i += 1
elif i < length - 1 and path.parts[i+1] == "..":
i += 2
else:
res_parts.append(path.parts[i])
i += 1
return "/".join(res_parts)


def patch_files(files: dict):
"""Decorate a function to simulate files ``files`` during the function.

The keys of ``files`` are file names and must use '/' for path separator.
The values are file contents. Directories or relative paths are not supported.
Example: ``{"en/one.txt": "One", "en/two.txt": "Two"}``

The implementation may be changed to match the mechanism used.
"""
if files is None:
files = {}

def then(func):
@mock.patch("os.path.isfile", side_effect=lambda p: _normalize_path(p) in files)
@mock.patch("codecs.open", side_effect=lambda p, _, __: StringIO(files[_normalize_path(p)]))
@functools.wraps(func) # Make ret look like func to later decorators
def ret(*args, **kwargs):
func(*args[:-2], **kwargs)
return ret
return then