asserts.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import os
  2. import stat
  3. try:
  4. from pathlib import Path
  5. except ImportError:
  6. try:
  7. # Python 2 backport
  8. from pathlib2 import Path
  9. except ImportError:
  10. class Path(object):
  11. """Dummy for isinstance checks"""
  12. pass
  13. __all__ = ['assert_path_exists', 'assert_not_path_exists',
  14. 'assert_isfile', 'assert_not_isfile',
  15. 'assert_isdir', 'assert_not_isdir',
  16. 'assert_islink', 'assert_not_islink',
  17. 'assert_ispipe', 'assert_not_ispipe',
  18. 'assert_issocket', 'assert_not_issocket',
  19. ]
  20. if hasattr(os, 'fspath'):
  21. _strpath = os.fspath
  22. else:
  23. def _strpath(p):
  24. if hasattr(p, '__fspath__'):
  25. return p.__fspath__()
  26. elif isinstance(p, Path):
  27. return str(p)
  28. return p
  29. def _stat_for_assert(path, follow_symlinks=True, msg=None):
  30. stat = os.stat if follow_symlinks else os.lstat
  31. try:
  32. return stat(path)
  33. except OSError:
  34. if msg is None:
  35. msg = "Path does not exist, or can't be stat-ed: %r" % path
  36. raise AssertionError(msg)
  37. def assert_path_exists(path, msg=None):
  38. """Assert that something exists at the given path.
  39. """
  40. _stat_for_assert(_strpath(path), True, msg)
  41. def assert_not_path_exists(path, msg=None):
  42. """Assert that nothing exists at the given path.
  43. """
  44. path = _strpath(path)
  45. if os.path.exists(path):
  46. if msg is None:
  47. msg = "Path exists: %r" % path
  48. raise AssertionError(msg)
  49. def assert_isfile(path, follow_symlinks=True, msg=None):
  50. """Assert that path exists and is a regular file.
  51. With follow_symlinks=True, the default, this will pass if path is a symlink
  52. to a regular file. With follow_symlinks=False, it will fail in that case.
  53. """
  54. path = _strpath(path)
  55. st = _stat_for_assert(path, follow_symlinks, msg)
  56. if not stat.S_ISREG(st.st_mode):
  57. if msg is None:
  58. msg = "Path exists, but is not a regular file: %r" % path
  59. raise AssertionError(msg)
  60. def assert_not_isfile(path, follow_symlinks=True, msg=None):
  61. """Assert that path exists but is not a regular file.
  62. With follow_symlinks=True, the default, this will fail if path is a symlink
  63. to a regular file. With follow_symlinks=False, it will pass in that case.
  64. """
  65. path = _strpath(path)
  66. st = _stat_for_assert(path, follow_symlinks, msg)
  67. if stat.S_ISREG(st.st_mode):
  68. if msg is None:
  69. msg = "Path is a regular file: %r" % path
  70. raise AssertionError(msg)
  71. def assert_isdir(path, follow_symlinks=True, msg=None):
  72. """Assert that path exists and is a directory.
  73. With follow_symlinks=True, the default, this will pass if path is a symlink
  74. to a directory. With follow_symlinks=False, it will fail in that case.
  75. """
  76. path = _strpath(path)
  77. st = _stat_for_assert(path, follow_symlinks, msg)
  78. if not stat.S_ISDIR(st.st_mode):
  79. if msg is None:
  80. msg = "Path exists, but is not a directory: %r" % path
  81. raise AssertionError(msg)
  82. def assert_not_isdir(path, follow_symlinks=True, msg=None):
  83. """Assert that path exists but is not a directory.
  84. With follow_symlinks=True, the default, this will fail if path is a symlink
  85. to a directory. With follow_symlinks=False, it will pass in that case.
  86. """
  87. path = _strpath(path)
  88. st = _stat_for_assert(path, follow_symlinks, msg)
  89. if stat.S_ISDIR(st.st_mode):
  90. if msg is None:
  91. msg = "Path is a directory: %r" % path
  92. raise AssertionError(msg)
  93. _link_target_msg = """Symlink target of:
  94. {path}
  95. Expected:
  96. {expected}
  97. Actual:
  98. {actual}
  99. """
  100. def assert_islink(path, to=None, msg=None):
  101. """Assert that path exists and is a symlink.
  102. If to is specified, also check that it is the target of the symlink.
  103. """
  104. path = _strpath(path)
  105. st = _stat_for_assert(path, False, msg)
  106. if not stat.S_ISLNK(st.st_mode):
  107. if msg is None:
  108. msg = "Path exists, but is not a symlink: %r" % path
  109. raise AssertionError(msg)
  110. if to is not None:
  111. to = _strpath(to)
  112. target = os.readlink(path)
  113. # TODO: Normalise the target to an absolute path?
  114. if target != to:
  115. if msg is None:
  116. msg = _link_target_msg.format(path=path, expected=to, actual=target)
  117. raise AssertionError(msg)
  118. def assert_not_islink(path, msg=None):
  119. """Assert that path exists but is not a symlink.
  120. """
  121. path = _strpath(path)
  122. st = _stat_for_assert(path, False, msg)
  123. if stat.S_ISLNK(st.st_mode):
  124. if msg is None:
  125. msg = "Path is a symlink: %r" % path
  126. raise AssertionError(msg)
  127. def assert_ispipe(path, follow_symlinks=True, msg=None):
  128. """Assert that path exists and is a named pipe (FIFO).
  129. With follow_symlinks=True, the default, this will pass if path is a symlink
  130. to a named pipe. With follow_symlinks=False, it will fail in that case.
  131. """
  132. path = _strpath(path)
  133. st = _stat_for_assert(path, follow_symlinks, msg)
  134. if not stat.S_ISFIFO(st.st_mode):
  135. if msg is None:
  136. msg = "Path exists, but is not a named pipe: %r" % path
  137. raise AssertionError(msg)
  138. def assert_not_ispipe(path, follow_symlinks=True, msg=None):
  139. """Assert that path exists but is not a named pipe (FIFO).
  140. With follow_symlinks=True, the default, this will fail if path is a symlink
  141. to a named pipe. With follow_symlinks=False, it will pass in that case.
  142. """
  143. path = _strpath(path)
  144. st = _stat_for_assert(path, follow_symlinks, msg)
  145. if stat.S_ISFIFO(st.st_mode):
  146. if msg is None:
  147. msg = "Path is a named pipe: %r" % path
  148. raise AssertionError(msg)
  149. def assert_issocket(path, follow_symlinks=True, msg=None):
  150. """Assert that path exists and is a Unix domain socket.
  151. With follow_symlinks=True, the default, this will pass if path is a symlink
  152. to a Unix domain socket. With follow_symlinks=False, it will fail in that case.
  153. """
  154. path = _strpath(path)
  155. st = _stat_for_assert(path, follow_symlinks, msg)
  156. if not stat.S_ISSOCK(st.st_mode):
  157. if msg is None:
  158. msg = "Path exists, but is not a socket: %r" % path
  159. raise AssertionError(msg)
  160. def assert_not_issocket(path, follow_symlinks=True, msg=None):
  161. """Assert that path exists but is not a Unix domain socket.
  162. With follow_symlinks=True, the default, this will fail if path is a symlink
  163. to a Unix domain socket. With follow_symlinks=False, it will pass in that case.
  164. """
  165. path = _strpath(path)
  166. st = _stat_for_assert(path, follow_symlinks, msg)
  167. if stat.S_ISSOCK(st.st_mode):
  168. if msg is None:
  169. msg = "Path is a socket: %r" % path
  170. raise AssertionError(msg)