/usr/lib/python3/dist-packages/testpath/asserts.py is in python3-testpath 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import os
import stat
try:
from pathlib import Path
except ImportError:
class Path(object):
"""Dummy for isinstance checks"""
pass
__all__ = ['assert_path_exists', 'assert_not_path_exists',
'assert_isfile', 'assert_not_isfile',
'assert_isdir', 'assert_not_isdir',
'assert_islink', 'assert_not_islink',
]
def _strpath(p):
if isinstance(p, Path):
return str(p)
return p
def _stat_for_assert(path, follow_symlinks=True, msg=None):
stat = os.stat if follow_symlinks else os.lstat
try:
return stat(path)
except OSError:
if msg is None:
msg = "Path does not exist, or can't be stat-ed: %r" % path
raise AssertionError(msg)
def assert_path_exists(path, msg=None):
"""Assert that something exists at the given path.
"""
_stat_for_assert(_strpath(path), True, msg)
def assert_not_path_exists(path, msg=None):
"""Assert that nothing exists at the given path.
"""
path = _strpath(path)
if os.path.exists(path):
if msg is None:
msg = "Path exists: %r" % path
raise AssertionError(msg)
def assert_isfile(path, follow_symlinks=True, msg=None):
"""Assert that path exists and is a regular file.
With follow_symlinks=True, the default, this will pass if path is a symlink
to a regular file. With follow_symlinks=False, it will fail in that case.
"""
path = _strpath(path)
st = _stat_for_assert(path, follow_symlinks, msg)
if not stat.S_ISREG(st.st_mode):
if msg is None:
msg = "Path exists, but is not a regular file: %r" % path
raise AssertionError(msg)
def assert_not_isfile(path, follow_symlinks=True, msg=None):
"""Assert that path exists but is not a regular file.
With follow_symlinks=True, the default, this will fail if path is a symlink
to a regular file. With follow_symlinks=False, it will pass in that case.
"""
path = _strpath(path)
st = _stat_for_assert(path, follow_symlinks, msg)
if stat.S_ISREG(st.st_mode):
if msg is None:
msg = "Path is a regular file: %r" % path
raise AssertionError(msg)
def assert_isdir(path, follow_symlinks=True, msg=None):
"""Assert that path exists and is a directory.
With follow_symlinks=True, the default, this will pass if path is a symlink
to a directory. With follow_symlinks=False, it will fail in that case.
"""
path = _strpath(path)
st = _stat_for_assert(path, follow_symlinks, msg)
if not stat.S_ISDIR(st.st_mode):
if msg is None:
msg = "Path exists, but is not a directory: %r" % path
raise AssertionError(msg)
def assert_not_isdir(path, follow_symlinks=True, msg=None):
"""Assert that path exists but is not a directory.
With follow_symlinks=True, the default, this will fail if path is a symlink
to a directory. With follow_symlinks=False, it will pass in that case.
"""
path = _strpath(path)
st = _stat_for_assert(path, follow_symlinks, msg)
if stat.S_ISDIR(st.st_mode):
if msg is None:
msg = "Path is a directory: %r" % path
raise AssertionError(msg)
_link_target_msg = """Symlink target of:
{path}
Expected:
{expected}
Actual:
{actual}
"""
def assert_islink(path, to=None, msg=None):
"""Assert that path exists and is a symlink.
If to is specified, also check that it is the target of the symlink.
"""
path = _strpath(path)
st = _stat_for_assert(path, False, msg)
if not stat.S_ISLNK(st.st_mode):
if msg is None:
msg = "Path exists, but is not a symlink: %r" % path
raise AssertionError(msg)
if to is not None:
to = _strpath(to)
target = os.readlink(path)
# TODO: Normalise the target to an absolute path?
if target != to:
if msg is None:
msg = _link_target_msg.format(path=path, expected=to, actual=target)
raise AssertionError(msg)
def assert_not_islink(path, msg=None):
"""Assert that path exists but is not a symlink.
"""
path = _strpath(path)
st = _stat_for_assert(path, False, msg)
if stat.S_ISLNK(st.st_mode):
if msg is None:
msg = "Path is a symlink: %r" % path
raise AssertionError(msg)
|