test_inotify.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.python._inotify}.
  5. """
  6. from twisted.trial.unittest import TestCase
  7. from twisted.python.filepath import FilePath
  8. from twisted.python.runtime import platform
  9. if platform.supportsINotify():
  10. from ctypes import c_int, c_uint32, c_char_p
  11. from twisted.python import _inotify
  12. from twisted.python._inotify import (
  13. INotifyError, initializeModule, init, add)
  14. else:
  15. _inotify = None
  16. class INotifyTests(TestCase):
  17. """
  18. Tests for L{twisted.python._inotify}.
  19. """
  20. if _inotify is None:
  21. skip = "This platform doesn't support INotify."
  22. def test_missingInit(self):
  23. """
  24. If the I{libc} object passed to L{initializeModule} has no
  25. C{inotify_init} attribute, L{ImportError} is raised.
  26. """
  27. class libc:
  28. def inotify_add_watch(self):
  29. pass
  30. def inotify_rm_watch(self):
  31. pass
  32. self.assertRaises(ImportError, initializeModule, libc())
  33. def test_missingAdd(self):
  34. """
  35. If the I{libc} object passed to L{initializeModule} has no
  36. C{inotify_add_watch} attribute, L{ImportError} is raised.
  37. """
  38. class libc:
  39. def inotify_init(self):
  40. pass
  41. def inotify_rm_watch(self):
  42. pass
  43. self.assertRaises(ImportError, initializeModule, libc())
  44. def test_missingRemove(self):
  45. """
  46. If the I{libc} object passed to L{initializeModule} has no
  47. C{inotify_rm_watch} attribute, L{ImportError} is raised.
  48. """
  49. class libc:
  50. def inotify_init(self):
  51. pass
  52. def inotify_add_watch(self):
  53. pass
  54. self.assertRaises(ImportError, initializeModule, libc())
  55. def test_setTypes(self):
  56. """
  57. If the I{libc} object passed to L{initializeModule} has all of the
  58. necessary attributes, it sets the C{argtypes} and C{restype} attributes
  59. of the three ctypes methods used from libc.
  60. """
  61. class libc:
  62. def inotify_init(self):
  63. pass
  64. inotify_init = staticmethod(inotify_init)
  65. def inotify_rm_watch(self):
  66. pass
  67. inotify_rm_watch = staticmethod(inotify_rm_watch)
  68. def inotify_add_watch(self):
  69. pass
  70. inotify_add_watch = staticmethod(inotify_add_watch)
  71. c = libc()
  72. initializeModule(c)
  73. self.assertEqual(c.inotify_init.argtypes, [])
  74. self.assertEqual(c.inotify_init.restype, c_int)
  75. self.assertEqual(c.inotify_rm_watch.argtypes, [c_int, c_int])
  76. self.assertEqual(c.inotify_rm_watch.restype, c_int)
  77. self.assertEqual(
  78. c.inotify_add_watch.argtypes, [c_int, c_char_p, c_uint32])
  79. self.assertEqual(c.inotify_add_watch.restype, c_int)
  80. def test_failedInit(self):
  81. """
  82. If C{inotify_init} returns a negative number, L{init} raises
  83. L{INotifyError}.
  84. """
  85. class libc:
  86. def inotify_init(self):
  87. return -1
  88. self.patch(_inotify, 'libc', libc())
  89. self.assertRaises(INotifyError, init)
  90. def test_failedAddWatch(self):
  91. """
  92. If C{inotify_add_watch} returns a negative number, L{add}
  93. raises L{INotifyError}.
  94. """
  95. class libc:
  96. def inotify_add_watch(self, fd, path, mask):
  97. return -1
  98. self.patch(_inotify, 'libc', libc())
  99. self.assertRaises(INotifyError, add, 3, FilePath('/foo'), 0)