Skip to content

Commit

Permalink
Improve exception handling in subprocess setup. Fix broken output han…
Browse files Browse the repository at this point in the history
…dling in #144. Add a test for this.
  • Loading branch information
ionelmc committed Jan 5, 2017
1 parent 609c1be commit 4b9978c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/pytest-cov.embed
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ if 'COV_CORE_SOURCE' in os.environ:
try:
from pytest_cov.embed import init
init()
except ImportError as exc:
except Exception as exc:
sys.stderr.write(
"Failed to setup coverage."
"Sources: {[COV_CORE_SOURCE]!r}"
"Config: {[COV_CORE_CONFIG]!r}"
"Exception: {!r}\n".format(os.environ, exc))
"pytest-cov: Failed to setup subprocess coverage. "
"Environ: {0!r} "
"Exception: {1!r}\n".format(
dict((k, v) for k, v in os.environ.items() if k.startswith('COV_CORE')),
exc
)
)
2 changes: 1 addition & 1 deletion src/pytest-cov.pth
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import os, sys;exec('if \'COV_CORE_SOURCE\' in os.environ:\n try:\n from pytest_cov.embed import init\n init()\n except ImportError as exc:\n sys.stderr.write(\n "Failed to setup coverage."\n "Sources: {[COV_CORE_SOURCE]!r}"\n "Config: {[COV_CORE_CONFIG]!r}"\n "Exception: {!r}\\n".format(os.environ, exc))\n')
import os, sys;exec('if \'COV_CORE_SOURCE\' in os.environ:\n try:\n from pytest_cov.embed import init\n init()\n except Exception as exc:\n sys.stderr.write(\n "pytest-cov: Failed to setup subprocess coverage. "\n "Environ: {0!r} "\n "Exception: {1!r}\\n".format(\n dict((k, v) for k, v in os.environ.items() if k.startswith(\'COV_CORE\')),\n exc\n )\n )\n')
23 changes: 23 additions & 0 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import sys
from distutils.version import StrictVersion
from io import StringIO
from itertools import chain

import coverage
Expand Down Expand Up @@ -1169,3 +1170,25 @@ def test_do_not_append_coverage(testdir, opts):
'test_1* 0%',
'test_2* %s*' % SCRIPT2_RESULT,
])


def test_pth_failure(monkeypatch):
with open('src/pytest-cov.pth') as fh:
payload = fh.read()

class SpecificError(Exception):
pass

def bad_init():
raise SpecificError()

buff = StringIO()

from pytest_cov import embed

monkeypatch.setattr(embed, 'init', bad_init)
monkeypatch.setattr(sys, 'stderr', buff)
monkeypatch.setitem(os.environ, 'COV_CORE_SOURCE', 'foobar')
exec(payload)
assert buff.getvalue() == '''pytest-cov: Failed to setup subprocess coverage. Environ: {'COV_CORE_SOURCE': 'foobar'} Exception: SpecificError()
'''

0 comments on commit 4b9978c

Please sign in to comment.