test_import_isolation.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Authors: pietro
  3. Copyright: (C) 2015 pietro
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. Created on Wed Jul 15 11:34:32 2015
  8. """
  9. from __future__ import absolute_import
  10. import sys
  11. import fnmatch
  12. from grass.gunittest.case import TestCase
  13. from grass.gunittest.main import test
  14. def check(*patterns):
  15. """Return a set of the imported libraries that soddisfies several patterns."""
  16. result = []
  17. imports = sorted(sys.modules.keys())
  18. for pattern in patterns:
  19. result.extend(fnmatch.filter(imports, pattern))
  20. return set(result)
  21. class TestImportIsolation(TestCase):
  22. patterns = ["grass.lib*"]
  23. def test_import_isolation(self):
  24. """Check that modules classes are not using ctypes"""
  25. isolate = set()
  26. self.assertEqual(
  27. isolate, check(*self.patterns), msg="Test isolation before any import."
  28. )
  29. # same import done in __init__ file
  30. from grass.pygrass.modules.interface import Module, ParallelModuleQueue
  31. from grass.pygrass.modules import shortcuts
  32. self.assertEqual(
  33. isolate, check(*self.patterns), msg="Test isolation after import Module."
  34. )
  35. # test the other way round
  36. from grass.pygrass.vector import VectorTopo
  37. self.assertNotEqual(
  38. isolate,
  39. check(*self.patterns),
  40. msg=(
  41. "Test the isolation is broken, therefore "
  42. "the defined patterns are correct"
  43. ),
  44. )
  45. if __name__ == "__main__":
  46. test()