-
Notifications
You must be signed in to change notification settings - Fork 28
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
eemeli
merged 14 commits into
projectfluent:main
from
studyingegret:fix-test-failures-on-windows
Feb 7, 2025
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8862ee3
Fix test failures on Windows
studyingegret bea90b9
Tidy code to prepare for PR
studyingegret 02fa44f
ci: Workaround Python 3.7 no longer supported on GItHub Actions
studyingegret 6e8e716
Fix flake8 errors
studyingegret 096712f
suggestions from code review: Run on windows-2022
studyingegret 3cf815f
Easy refactoring of file simulation
studyingegret 457d698
Refactor & add tests for file simulating code
studyingegret 8d25d2f
Delete unnecessary notice
studyingegret f7eb828
pr review: Apply suggestions
studyingegret c0c988d
Merge remote
studyingegret ad8e7bb
Use context manager
studyingegret 0c9c67b
Try to fix test errors on Unix
studyingegret c981885
(amend) Fix displaced comment
studyingegret 9f903f4
Merge remote
studyingegret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,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) |
This file contains hidden or 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,58 @@ | ||
"""Utilities for testing.""" | ||
|
||
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) | ||
studyingegret marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.