Skip to content

Commit

Permalink
starting to use raises_gathered
Browse files Browse the repository at this point in the history
  • Loading branch information
oconnor663 committed Jul 18, 2016
1 parent d5db16f commit bc9e02f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
25 changes: 25 additions & 0 deletions peru/async.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import atexit
import codecs
import contextlib
import io
import os
import subprocess
Expand Down Expand Up @@ -29,6 +30,7 @@ def run_task(coro):

class GatheredExceptions(PrintableError):
def __init__(self, exceptions):
assert len(exceptions) > 0
self.exceptions = []
for e in exceptions:
# Flatten in the exceptions list of any other GatheredExceptions we
Expand All @@ -39,8 +41,13 @@ def __init__(self, exceptions):
else:
self.exceptions.append(e)

# TODO: Something meaningful here.
self.message = "{0} errors OMGGGGGG".format(len(self.exceptions))

def get_only(self):
assert len(self.exceptions) == 1, "more than one gathered exception"
return self.exceptions[0]


@asyncio.coroutine
def gather_coalescing_exceptions(coros, display, error_str):
Expand Down Expand Up @@ -170,3 +177,21 @@ def safe_communicate(process, input=None):
return (yield from process.communicate())
else:
return (yield from process.communicate(input))


@contextlib.contextmanager
def raises_gathered(error_type):
'''For use in tests. Many tests expect a single error to be thrown, and
want it to be of a specific type. This is a helper method for when that
type is inside a gathered exception.'''
try:
yield
except GatheredExceptions as e:
# Make sure there is exactly one exception.
if len(e.exceptions) != 1:
raise
inner = e.exceptions[0]
# Make sure the exception is the right type.
if not isinstance(inner, error_type):
raise
# Success.
5 changes: 3 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def main():
if var.startswith('PERU_'):
del os.environ[var]

# Turn debugging features on for the asyncio library.
os.environ['PYTHONASYNCIODEBUG'] = '1'
# Turn debugging features on for the asyncio library, if it's unset.
if 'PYTHONASYNCIODEBUG' not in os.environ:
os.environ['PYTHONASYNCIODEBUG'] = '1'

# Make sure the tests don't create any garbage files in the repo. That
# tends to happen when we accidentally run something in the current dir
Expand Down
7 changes: 4 additions & 3 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import textwrap

from peru.async import raises_gathered, GatheredExceptions
import peru.cache
import peru.compat
import peru.error
Expand Down Expand Up @@ -387,7 +388,7 @@ def test_drop_then_pick_is_an_error(self):
imports:
foobar: ./
''', module_dir)
with self.assertRaises(peru.rule.NoMatchingFilesError):
with raises_gathered(peru.rule.NoMatchingFilesError):
run_peru_command(['sync'], self.test_dir)

def test_rule_with_executable(self):
Expand Down Expand Up @@ -430,8 +431,8 @@ def test_rule_with_move_error(self):
try:
self.do_integration_test(['sync'],
{'newa': 'foo', 'newb/c': 'bar'})
except peru.error.PrintableError as e:
assert 'doesntexist' in e.message
except GatheredExceptions as e:
assert 'doesntexist' in e.get_only().message

def test_rule_with_copied_files(self):
content = {
Expand Down

0 comments on commit bc9e02f

Please sign in to comment.