PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/helpers.py

https://github.com/mcfletch/webassets
Python | 55 lines | 35 code | 11 blank | 9 comment | 14 complexity | 5fbe5acd7f9156e65058a33f5ec8e111 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. from __future__ import with_statement
  2. import re
  3. from webassets.test import TempDirHelper, TempEnvironmentHelper
  4. __all__ = ('TempDirHelper', 'TempEnvironmentHelper', 'noop',
  5. 'assert_raises_regexp', 'check_warnings')
  6. # Define a noop filter; occasionally in tests we need to define
  7. # a filter to be able to test a certain piece of functionality,.
  8. noop = lambda _in, out: out.write(_in.read())
  9. try:
  10. # Python 3
  11. from nose.tools import assert_raises_regex
  12. except ImportError:
  13. try:
  14. # Python >= 2.7
  15. from nose.tools import assert_raises_regexp as assert_raises_regex
  16. except:
  17. # Python < 2.7
  18. def assert_raises_regex(expected, regexp, callable, *a, **kw):
  19. try:
  20. callable(*a, **kw)
  21. except expected as e:
  22. if isinstance(regexp, basestring):
  23. regexp = re.compile(regexp)
  24. if not regexp.search(str(e.message)):
  25. raise self.failureException('"%s" does not match "%s"' %
  26. (regexp.pattern, str(e.message)))
  27. else:
  28. if hasattr(expected,'__name__'): excName = expected.__name__
  29. else: excName = str(expected)
  30. raise AssertionError("%s not raised" % excName)
  31. try:
  32. from test.support import check_warnings # Python 3
  33. except ImportError:
  34. try:
  35. from test.test_support import check_warnings # Python 2
  36. except ImportError:
  37. # Python < 2.6
  38. import contextlib
  39. @contextlib.contextmanager
  40. def check_warnings(*filters, **kwargs):
  41. # We cannot reasonably support this, we'd have to copy to much code.
  42. # (or write our own). Since this is only testing warnings output,
  43. # we might slide by ignoring it.
  44. yield