mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-12-04 08:44:02 +03:00
Use platform-specific path escaping with test evidence
Windows requires manual backslash replacement for repr() matching:
- repr() displays C:\Users as 'C:\Users' (double backslashes)
- Regex needs \\ to match \ (four backslashes in pattern)
- re.escape() only produces \ (matches single backslash)
Test evidence on Windows:
re.escape(path) in repr(path): None (fails)
path.replace('\', r'\\') in repr(path): Match (works)
Solution:
- Windows: Manual replacement for repr() double-escaping
- Unix: re.escape() for special character handling
Addresses @auvipy's review with detailed testing and explanation.
This commit is contained in:
parent
ea2564c418
commit
6e992889ea
|
|
@ -168,6 +168,32 @@ class TestRegularFieldMappings(TestCase):
|
||||||
model = RegularFieldsModel
|
model = RegularFieldsModel
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
# Cross-platform path handling for regex matching against repr() output
|
||||||
|
#
|
||||||
|
# Challenge: repr() output escapes backslashes, so Windows paths like
|
||||||
|
# C:\Users become 'C:\\Users' in the repr string. To match \\ in regex,
|
||||||
|
# we need \\\\ in the pattern.
|
||||||
|
#
|
||||||
|
# Why re.escape() doesn't work for Windows:
|
||||||
|
# - re.escape(r'C:\Users') → 'C:\\Users' (matches single \)
|
||||||
|
# - But repr() shows 'C:\\Users' (double \\)
|
||||||
|
# - We need pattern 'C:\\\\Users' (to match double \\)
|
||||||
|
#
|
||||||
|
# Testing on Windows confirms:
|
||||||
|
# >>> path = r'C:\Users\Temp'
|
||||||
|
# >>> re.search(re.escape(path), repr(path))
|
||||||
|
# None # Fails
|
||||||
|
# >>> re.search(path.replace('\\', r'\\\\'), repr(path))
|
||||||
|
# <re.Match object...> # Works
|
||||||
|
#
|
||||||
|
# For Unix paths (no backslashes), re.escape() works correctly.
|
||||||
|
temp_path = tempfile.gettempdir()
|
||||||
|
if '\\' in temp_path:
|
||||||
|
# Windows: Manual replacement needed for repr() matching
|
||||||
|
escaped_temp_path = temp_path.replace('\\', r'\\\\')
|
||||||
|
else:
|
||||||
|
# Unix: re.escape() handles any special regex characters
|
||||||
|
escaped_temp_path = re.escape(temp_path)
|
||||||
expected = dedent(r"""
|
expected = dedent(r"""
|
||||||
TestSerializer\(\):
|
TestSerializer\(\):
|
||||||
auto_field = IntegerField\(read_only=True\)
|
auto_field = IntegerField\(read_only=True\)
|
||||||
|
|
@ -192,7 +218,7 @@ class TestRegularFieldMappings(TestCase):
|
||||||
url_field = URLField\(max_length=100\)
|
url_field = URLField\(max_length=100\)
|
||||||
custom_field = ModelField\(model_field=<.*CustomField: custom_field>\)
|
custom_field = ModelField\(model_field=<.*CustomField: custom_field>\)
|
||||||
file_path_field = FilePathField\(path='%s'\)
|
file_path_field = FilePathField\(path='%s'\)
|
||||||
""" % tempfile.gettempdir().replace('\\', r'\\\\'))
|
""") % escaped_temp_path
|
||||||
|
|
||||||
assert re.search(expected, repr(TestSerializer()), re.DOTALL) is not None
|
assert re.search(expected, repr(TestSerializer()), re.DOTALL) is not None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user