__init__.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import filecmp
  2. import os
  3. import sys
  4. import time
  5. import unittest
  6. from attic import xattr
  7. has_mtime_ns = sys.version >= '3.3'
  8. utime_supports_fd = os.utime in getattr(os, 'supports_fd', {})
  9. class AtticTestCase(unittest.TestCase):
  10. """
  11. """
  12. assert_equal = unittest.TestCase.assertEqual
  13. assert_not_equal = unittest.TestCase.assertNotEqual
  14. assert_raises = unittest.TestCase.assertRaises
  15. def _get_xattrs(self, path):
  16. try:
  17. return xattr.get_all(path)
  18. except EnvironmentError:
  19. return {}
  20. def assert_dirs_equal(self, dir1, dir2, fuse=False):
  21. diff = filecmp.dircmp(dir1, dir2)
  22. self._assert_dirs_equal_cmp(diff, fuse)
  23. def _assert_dirs_equal_cmp(self, diff, fuse=False):
  24. self.assert_equal(diff.left_only, [])
  25. self.assert_equal(diff.right_only, [])
  26. self.assert_equal(diff.diff_files, [])
  27. self.assert_equal(diff.funny_files, [])
  28. for filename in diff.common:
  29. path1 = os.path.join(diff.left, filename)
  30. path2 = os.path.join(diff.right, filename)
  31. s1 = os.lstat(path1)
  32. s2 = os.lstat(path2)
  33. attrs = ['st_mode', 'st_uid', 'st_gid', 'st_rdev']
  34. if not fuse or not os.path.isdir(path1):
  35. # dir nlink is always 1 on our fuse fileystem
  36. attrs.append('st_nlink')
  37. if not os.path.islink(path1) or utime_supports_fd:
  38. # Fuse api is does not support ns precision
  39. attrs.append('st_mtime_ns' if has_mtime_ns and not fuse else 'st_mtime')
  40. d1 = [filename] + [getattr(s1, a) for a in attrs]
  41. d2 = [filename] + [getattr(s2, a) for a in attrs]
  42. # 'st_mtime precision is limited'
  43. if attrs[-1] == 'st_mtime':
  44. d1[-1] = round(d1[-1], 2)
  45. d2[-1] = round(d2[-1], 2)
  46. d1.append(self._get_xattrs(path1))
  47. d2.append(self._get_xattrs(path2))
  48. self.assert_equal(d1, d2)
  49. for sub_diff in diff.subdirs.values():
  50. self._assert_dirs_equal_cmp(sub_diff, fuse)
  51. def wait_for_mount(self, path, timeout=5):
  52. """Wait until a filesystem is mounted on `path`
  53. """
  54. timeout += time.time()
  55. while timeout > time.time():
  56. if os.path.ismount(path):
  57. return
  58. time.sleep(.1)
  59. raise Exception('wait_for_mount(%s) timeout' % path)
  60. def get_tests(suite):
  61. """Generates a sequence of tests from a test suite
  62. """
  63. for item in suite:
  64. try:
  65. # TODO: This could be "yield from..." with Python 3.3+
  66. for i in get_tests(item):
  67. yield i
  68. except TypeError:
  69. yield item
  70. class TestLoader(unittest.TestLoader):
  71. """A customzied test loader that properly detects and filters our test cases
  72. """
  73. def loadTestsFromName(self, pattern, module=None):
  74. suite = self.discover('attic.testsuite', '*.py')
  75. tests = unittest.TestSuite()
  76. for test in get_tests(suite):
  77. if pattern.lower() in test.id().lower():
  78. tests.addTest(test)
  79. return tests