Skip to content

Commit

Permalink
Use a cross-compat trick to squash chaining, stolen from pyparsing.py…
Browse files Browse the repository at this point in the history
… (thanks PaulMcG)
  • Loading branch information
wimglenn authored Apr 4, 2019
1 parent aedd73f commit 2fb434c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
16 changes: 4 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@ python:
- "2.7"
- "3.5"
- "3.6"
- "3.7-dev"
- "pypy"
- "pypy3.5"
- "pypy3"
- "nightly"

matrix:
fast_finish: true
allow_failures:
- python: "nightly"
include:
- python: 3.7
dist: xenial
sudo: true

before_install:
- sudo apt-get -y install python3-pip python3-setuptools
- sudo pip3 install flit

install:
- pip install --upgrade pytest flit6
- flit install
- pip install "pytest>=3.3"

script:
- pytest
- python -m pytest

notifications:
email: false
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
pytest-raisin 0.3 (2019-04-03)
==============================

- Improved cross-compat code, simplifying into a single .py module distribution.


pytest-raisin 0.2 (2019-02-11)
==============================

Expand Down
20 changes: 17 additions & 3 deletions pytest_raisin/__init__.py → pytest_raisin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

import pytest

from pytest_raisin.compat import default_compare


__version__ = "0.2"
__version__ = "0.3"


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -68,6 +66,22 @@ def raises(expected_exception, *args, **kwargs):
return original(expected_exception, *args, **kwargs)


def default_compare(exc_actual, exc_expected):
__tracebackhide__ = True
actual_args = getattr(exc_actual, "args", None)
expected_args = getattr(exc_expected, "args", None)
if actual_args == expected_args:
return
msg = "{} args do not match!\n Actual: {}\n Expected: {}"
msg = msg.format(type(exc_expected).__name__, actual_args, expected_args)
err = AssertionError(msg)
err.__cause__ = None
# as a side-effect, this also sets __suppress_context__
# that's a cross-compatible way of disabling chaining i.e. squashing the message
# "During handling of the above exception, another exception occurred"
raise err


class RaisesContext(type(pytest.raises(Exception))):

def __exit__(self, *tp):
Expand Down
9 changes: 0 additions & 9 deletions pytest_raisin/comparison_py2.py

This file was deleted.

9 changes: 0 additions & 9 deletions pytest_raisin/comparison_py3.py

This file was deleted.

6 changes: 0 additions & 6 deletions pytest_raisin/compat.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest_plugins = ("pytester", "pytest_raisin")
17 changes: 17 additions & 0 deletions tests/test_no_chaining.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def test_spurious_chaining_is_suppressed(testdir):
testdir.makepyfile("""
import pytest
def test_the_thing():
with pytest.raises(Exception("boom")):
raise Exception("bang")
""")
result = testdir.runpytest()
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines([
"E * AssertionError: Exception args do not match!",
"E * Actual: ('bang',)",
"E * Expected: ('boom',)",
])
text = result.stdout.str()
assert "During handling of the above exception, another exception occurred" not in text
3 changes: 0 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pytest_plugins = ["pytester"]


def test_using_exception_class(testdir):
testdir.makepyfile("""
import pytest
Expand Down
3 changes: 0 additions & 3 deletions tests/test_register.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pytest_plugins = ["pytester", "pytest-raisin"]


def test_user_registered_error_hook_pass(testdir):
testdir.makepyfile("""
import pytest
Expand Down

1 comment on commit 2fb434c

@wimglenn
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ptmcg this greatly simplified the packaging - thanks again for the tip

Please sign in to comment.