test_import_isolation.py 1.6 KB

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