test_import_isolation.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. """
  18. result = []
  19. imports = sorted(sys.modules.keys())
  20. for pattern in patterns:
  21. result.extend(fnmatch.filter(imports, pattern))
  22. return set(result)
  23. class TestImportIsolation(TestCase):
  24. patterns = ['grass.lib*']
  25. def test_import_isolation(self):
  26. """Check that modules classes are not using ctypes"""
  27. isolate = set()
  28. self.assertEqual(isolate, check(*self.patterns),
  29. msg="Test isolation before any import.")
  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(isolate, check(*self.patterns),
  34. msg="Test isolation after import Module.")
  35. # test the other way round
  36. from grass.pygrass.vector import VectorTopo
  37. self.assertNotEqual(isolate, check(*self.patterns),
  38. msg=("Test the isolation is broken, therefore "
  39. "the defined patterns are correct"))
  40. if __name__ == '__main__':
  41. test()