-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Fix test failures & add Windows CI tests (#206)
- Loading branch information
1 parent
df5ef40
commit fbf8b3f
Showing
4 changed files
with
110 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) | ||
|
||
with self.assertRaises(ValueError): | ||
os.path.isfile("en/") | ||
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 to exist and contents to match | ||
""" | ||
if expect_contents is None: | ||
self.assertFalse(os.path.isfile(filename), | ||
f"Expected {filename} to not exist.") | ||
else: | ||
self.assertTrue(os.path.isfile(filename), | ||
f"Expected {filename} to exist.") | ||
with codecs.open(filename, "r", "utf-8") as f: | ||
self.assertEqual(f.read(), expect_contents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,50 @@ | ||
"""Utilities for testing.""" | ||
|
||
import textwrap | ||
from pathlib import PureWindowsPath, PurePosixPath | ||
from unittest import mock | ||
from io import StringIO | ||
import functools | ||
|
||
|
||
def dedent_ftl(text): | ||
return textwrap.dedent(f"{text.rstrip()}\n") | ||
|
||
|
||
# Needed in test_falllback.py because it uses dict + string compare to make a virtual file structure | ||
def _normalize_file_path(path): | ||
"""Note: Does not support absolute paths or paths that | ||
contain '.' or '..' parts.""" | ||
# Cannot use os.path or PurePath, because they only recognize | ||
# one kind of path separator | ||
if PureWindowsPath(path).is_absolute() or PurePosixPath(path).is_absolute(): | ||
raise ValueError(f"Unsupported path: {path}") | ||
parts = path.replace("\\", "/").split("/") | ||
if "." in parts or ".." in parts: | ||
raise ValueError(f"Unsupported path: {path}") | ||
if parts and parts[-1] == "": | ||
# path ends with a trailing pathsep | ||
raise ValueError(f"Path appears to be a directory, not a file: {path}") | ||
return "/".join(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. | ||
""" | ||
|
||
# Here it is possible to validate file names, but skipped | ||
|
||
def then(func): | ||
@mock.patch("os.path.isfile", side_effect=lambda p: _normalize_file_path(p) in files) | ||
@mock.patch("codecs.open", side_effect=lambda p, _, __: StringIO(files[_normalize_file_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 |