From 2c4964d29022c5e991d133427d33690d37cdf9e2 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 19 Jul 2011 21:42:00 -0500 Subject: [PATCH] escape '%' in specialized comparison explanations (fixes #63) --- CHANGELOG | 1 + _pytest/assertion/__init__.py | 8 +++++++- testing/test_assertrewrite.py | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 5fc2943fd..59c39cb3a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Changes between 2.1.0 and 2.1.1.DEV ---------------------------------------------- +- fix assertion rewriting on inserts involving strings containing '%' - fix assertion rewriting on calls with a ** arg - don't cache rewritten modules if bytecode generation is disabled - fix assertion rewriting in read-only directories diff --git a/_pytest/assertion/__init__.py b/_pytest/assertion/__init__.py index 3cc026a27..381713130 100644 --- a/_pytest/assertion/__init__.py +++ b/_pytest/assertion/__init__.py @@ -45,7 +45,13 @@ def pytest_configure(config): config=config, op=op, left=left, right=right) for new_expl in hook_result: if new_expl: - return '\n~'.join(new_expl) + res = '\n~'.join(new_expl) + if mode == "rewrite": + # The result will be fed back a python % formatting + # operation, which will fail if there are extraneous + # '%'s in the string. Escape them here. + res = res.replace("%", "%%") + return res m = monkeypatch() config._cleanup.append(m.undo) m.setattr(py.builtin.builtins, 'AssertionError', diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 366b48c2c..67944162b 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -275,6 +275,11 @@ class TestAssertionRewrite: assert myany(A() < 0) assert " < 0" in getmsg(f) + def test_formatchar(self): + def f(): + assert "%test" == "test" + assert getmsg(f).startswith("assert '%test' == 'test'") + class TestRewriteOnImport: