test_imports.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from __future__ import absolute_import
  2. import fnmatch
  3. import os
  4. from subprocess import call
  5. ROOT = os.path.normpath(
  6. os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, 'src'))
  7. def _find_files(root, pattern='*'):
  8. matches = []
  9. for root, _, filenames in os.walk(root):
  10. for filename in fnmatch.filter(filenames, pattern):
  11. matches.append(os.path.join(root, filename))
  12. return matches
  13. def _generate_tests():
  14. string = 'from __future__ import absolute_import'
  15. kwargs = {
  16. 'stdout': open('/dev/null', 'a'),
  17. 'stderr': open('/dev/null', 'a'),
  18. }
  19. def make_test(filename, relpath):
  20. def test():
  21. assert not call(['grep', string, filename], **kwargs), \
  22. "Missing %r in %s" % (string, relpath)
  23. test.__doc__ = relpath
  24. test.__name__ = 'test_' + relpath.replace('/', '_').rstrip('.py')
  25. return test
  26. for filename in _find_files(ROOT, '*.py'):
  27. relpath = filename[len(ROOT) - 3:]
  28. if '/migrations/' in relpath:
  29. continue
  30. if relpath.startswith('src/sentry/static/'):
  31. continue
  32. func = make_test(filename, relpath)
  33. globals()[func.__name__] = func
  34. _generate_tests()