Skip to content

Commit

Permalink
Safe conversion to unicode and double decode
Browse files Browse the repository at this point in the history
- Unicode characters would fail the assertion otherwise
- there may be a better way to do this, but using py.builtin._totext
  was the easiest I could found without external dependencies
- add a test for the unicode scenario (including checking for
  python version specific (2 and 3) unicode representation)
  • Loading branch information
asfaltboy committed Sep 7, 2016
1 parent f4e3606 commit 3c9a4f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pytest_mock.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from pprint import pformat
import inspect

import py
import pytest

try:
import mock as mock_module
except ImportError:
import unittest.mock as mock_module

u = py.builtin._totext

version = '1.2'


Expand Down Expand Up @@ -152,12 +155,12 @@ def mock(mocker):
_mock_module_originals = {}


DETAILED_ASSERTION = """{original!s}
DETAILED_ASSERTION = u("""{original!s}
... pytest introspection follows:
{detailed!s}
"""
FULL_ANY_CALLS_DIFF = "{call} in {calls_list}"
""")
FULL_ANY_CALLS_DIFF = u("{call} in {calls_list}")


def pytest_assertrepr_compare(config, op, left, right):
Expand Down Expand Up @@ -187,12 +190,12 @@ def safe_unpack_args(call):
try:
assert largs == rargs
except AssertionError as e:
msg.extend(['args introspection:', str(e)])
msg.extend(['args introspection:', u(e)])

try:
assert lkwargs == rkwargs
except AssertionError as e:
msg.extend(['kwargs introspection:', str(e)])
msg.extend(['kwargs introspection:', u(e)])
return msg

if (isinstance(left, tuple) and
Expand All @@ -216,8 +219,8 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
assert assert_call == __mock_self.call_args
except AssertionError as diff:
# raise a new detailed exception, appending to existing
msg = DETAILED_ASSERTION.format(original=e, detailed=diff)
err = AssertionError(msg.encode().decode('unicode_escape'))
msg = DETAILED_ASSERTION.format(original=u(e), detailed=u(diff))
err = AssertionError(msg.replace('\\n', '\n').encode('unicode_escape').decode('unicode_escape'))
err._msg_updated = True
raise err
raise AssertionError(*e.args)
Expand Down
38 changes: 38 additions & 0 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# coding=utf-8
import os
import platform
import sys
from contextlib import contextmanager

import py
import py.code

import pytest

u = py.builtin._totext
pytest_plugins = 'pytester'

# could not make some of the tests work on PyPy, patches are welcome!
Expand Down Expand Up @@ -561,3 +564,38 @@ def test_assertion_error_is_descriptive(mocker):

assert mocker_any_call.startswith(mock_error_message)
assert "assert call(1, 2) in [call(" in mocker_any_call


def test_assertion_wrap_unicode(mocker):
"""Verify assert_wrapper properly handles unicode call parts"""
mocker_mock = mocker.patch('os.remove')
mocker_mock(u('א', 'utf-8'), b=u('ב', 'utf-8'))

# arguments assertion for last call
try:
mocker_mock.assert_called_with(u('ג', 'utf-8'), b=u('ד', 'utf-8'))
except AssertionError as e:
called_with_msg = e.msg

verbose = any(a.startswith('-v') for a in sys.argv)
if verbose:
assert u("('ג',) == ('א',)", 'utf-8') in called_with_msg
assert u("assert {'b': 'ד'} == {'b': 'ב'}", 'utf-8') in called_with_msg
assert u("Use -v to get the full diff", 'utf-8') not in called_with_msg
else:
assert (
(u("Expected call: remove(u'\\u05d2', b=u'\\u05d3')", 'utf-8') in called_with_msg) or
(u("Expected call: remove('\u05d2', b='\u05d3')", 'utf-8') in called_with_msg)
)
assert u("assert {'b': 'ד'} == {'b': 'ב'}", 'utf-8') in called_with_msg
assert u('Use -v to get the full diff', 'utf-8') in called_with_msg

try:
mocker_mock.assert_any_call(1, 2)
except AssertionError as e:
any_call_msg = e.msg

assert (
u("assert call(1, 2) in [call(u'\\u05d0'", 'utf-8') in any_call_msg or
u("assert call(1, 2) in [call('\u05d0'", 'utf-8') in any_call_msg
)

0 comments on commit 3c9a4f9

Please sign in to comment.