mirror of
https://github.com/python/cpython.git
synced 2025-07-23 19:25:40 +00:00
GH-110109: pathlib tests: store base directory as test class attribute (#113221)
Store the test base directory as a class attribute named `base` rather than module constants named `BASE`. The base directory is a local file path, and therefore not ideally suited to the pathlib ABC tests. In a future commit we'll change its value in `test_pathlib_abc.py` such that it points to a totally fictitious path, which will help to ensure we're not touching the local filesystem.
This commit is contained in:
parent
d91e43ed78
commit
2f0ec7fa94
2 changed files with 210 additions and 222 deletions
|
@ -18,16 +18,6 @@ class UnsupportedOperationTest(unittest.TestCase):
|
|||
self.assertTrue(isinstance(pathlib.UnsupportedOperation(), NotImplementedError))
|
||||
|
||||
|
||||
# Make sure any symbolic links in the base test path are resolved.
|
||||
BASE = os.path.realpath(TESTFN)
|
||||
join = lambda *x: os.path.join(BASE, *x)
|
||||
|
||||
only_nt = unittest.skipIf(os.name != 'nt',
|
||||
'test requires a Windows-compatible system')
|
||||
only_posix = unittest.skipIf(os.name == 'nt',
|
||||
'test requires a POSIX-compatible system')
|
||||
|
||||
|
||||
#
|
||||
# Tests for the pure classes.
|
||||
#
|
||||
|
@ -62,6 +52,9 @@ class DummyPurePath(pathlib._abc.PurePathBase):
|
|||
class DummyPurePathTest(unittest.TestCase):
|
||||
cls = DummyPurePath
|
||||
|
||||
# Make sure any symbolic links in the base test path are resolved.
|
||||
base = os.path.realpath(TESTFN)
|
||||
|
||||
# Keys are canonical paths, values are list of tuples of arguments
|
||||
# supposed to produce equal paths.
|
||||
equivalences = {
|
||||
|
@ -859,7 +852,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
cls = DummyPath
|
||||
can_symlink = False
|
||||
|
||||
# (BASE)
|
||||
# (self.base)
|
||||
# |
|
||||
# |-- brokenLink -> non-existing
|
||||
# |-- dirA
|
||||
|
@ -882,7 +875,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def setUp(self):
|
||||
super().setUp()
|
||||
pathmod = self.cls.pathmod
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
p.mkdir(parents=True)
|
||||
p.joinpath('dirA').mkdir()
|
||||
p.joinpath('dirB').mkdir()
|
||||
|
@ -914,7 +907,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
cls._symlinks.clear()
|
||||
|
||||
def tempdir(self):
|
||||
path = self.cls(BASE).with_name('tmp-dirD')
|
||||
path = self.cls(self.base).with_name('tmp-dirD')
|
||||
path.mkdir()
|
||||
return path
|
||||
|
||||
|
@ -927,8 +920,8 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertEqual(os.path.normcase(path_a), os.path.normcase(path_b))
|
||||
|
||||
def test_samefile(self):
|
||||
fileA_path = os.path.join(BASE, 'fileA')
|
||||
fileB_path = os.path.join(BASE, 'dirB', 'fileB')
|
||||
fileA_path = os.path.join(self.base, 'fileA')
|
||||
fileB_path = os.path.join(self.base, 'dirB', 'fileB')
|
||||
p = self.cls(fileA_path)
|
||||
pp = self.cls(fileA_path)
|
||||
q = self.cls(fileB_path)
|
||||
|
@ -937,7 +930,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse(p.samefile(fileB_path))
|
||||
self.assertFalse(p.samefile(q))
|
||||
# Test the non-existent file case
|
||||
non_existent = os.path.join(BASE, 'foo')
|
||||
non_existent = os.path.join(self.base, 'foo')
|
||||
r = self.cls(non_existent)
|
||||
self.assertRaises(FileNotFoundError, p.samefile, r)
|
||||
self.assertRaises(FileNotFoundError, p.samefile, non_existent)
|
||||
|
@ -953,7 +946,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
def test_exists(self):
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
self.assertIs(True, p.exists())
|
||||
self.assertIs(True, (p / 'dirA').exists())
|
||||
self.assertIs(True, (p / 'fileA').exists())
|
||||
|
@ -967,11 +960,11 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs(True, (p / 'brokenLink').exists(follow_symlinks=False))
|
||||
self.assertIs(False, (p / 'foo').exists())
|
||||
self.assertIs(False, P('/xyzzy').exists())
|
||||
self.assertIs(False, P(BASE + '\udfff').exists())
|
||||
self.assertIs(False, P(BASE + '\x00').exists())
|
||||
self.assertIs(False, P(self.base + '\udfff').exists())
|
||||
self.assertIs(False, P(self.base + '\x00').exists())
|
||||
|
||||
def test_open_common(self):
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
with (p / 'fileA').open('r') as f:
|
||||
self.assertIsInstance(f, io.TextIOBase)
|
||||
self.assertEqual(f.read(), "this is file A\n")
|
||||
|
@ -980,7 +973,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertEqual(f.read().strip(), b"this is file A")
|
||||
|
||||
def test_read_write_bytes(self):
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
(p / 'fileA').write_bytes(b'abcdefg')
|
||||
self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
|
||||
# Check that trying to write str does not truncate the file.
|
||||
|
@ -988,7 +981,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
|
||||
|
||||
def test_read_write_text(self):
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
(p / 'fileA').write_text('äbcdefg', encoding='latin-1')
|
||||
self.assertEqual((p / 'fileA').read_text(
|
||||
encoding='utf-8', errors='ignore'), 'bcdefg')
|
||||
|
@ -997,7 +990,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
|
||||
|
||||
def test_read_text_with_newlines(self):
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
# Check that `\n` character change nothing
|
||||
(p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
|
||||
self.assertEqual((p / 'fileA').read_text(newline='\n'),
|
||||
|
@ -1012,7 +1005,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
'abcde\r\nfghlk\n\rmnopq')
|
||||
|
||||
def test_write_text_with_newlines(self):
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
# Check that `\n` character change nothing
|
||||
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
|
||||
self.assertEqual((p / 'fileA').read_bytes(),
|
||||
|
@ -1033,27 +1026,27 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
def test_iterdir(self):
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
it = p.iterdir()
|
||||
paths = set(it)
|
||||
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
|
||||
if self.can_symlink:
|
||||
expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop']
|
||||
self.assertEqual(paths, { P(BASE, q) for q in expected })
|
||||
self.assertEqual(paths, { P(self.base, q) for q in expected })
|
||||
|
||||
def test_iterdir_symlink(self):
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
# __iter__ on a symlink to a directory.
|
||||
P = self.cls
|
||||
p = P(BASE, 'linkB')
|
||||
p = P(self.base, 'linkB')
|
||||
paths = set(p.iterdir())
|
||||
expected = { P(BASE, 'linkB', q) for q in ['fileB', 'linkD'] }
|
||||
expected = { P(self.base, 'linkB', q) for q in ['fileB', 'linkD'] }
|
||||
self.assertEqual(paths, expected)
|
||||
|
||||
def test_iterdir_nodir(self):
|
||||
# __iter__ on something that is not a directory.
|
||||
p = self.cls(BASE, 'fileA')
|
||||
p = self.cls(self.base, 'fileA')
|
||||
with self.assertRaises(OSError) as cm:
|
||||
p.iterdir()
|
||||
# ENOENT or EINVAL under Windows, ENOTDIR otherwise
|
||||
|
@ -1063,9 +1056,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
def test_glob_common(self):
|
||||
def _check(glob, expected):
|
||||
self.assertEqual(set(glob), { P(BASE, q) for q in expected })
|
||||
self.assertEqual(set(glob), { P(self.base, q) for q in expected })
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
it = p.glob("fileA")
|
||||
self.assertIsInstance(it, collections.abc.Iterator)
|
||||
_check(it, ["fileA"])
|
||||
|
@ -1101,9 +1094,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
P = self.cls
|
||||
def _check(path, pattern, case_sensitive, expected):
|
||||
actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)}
|
||||
expected = {str(P(BASE, q)) for q in expected}
|
||||
expected = {str(P(self.base, q)) for q in expected}
|
||||
self.assertEqual(actual, expected)
|
||||
path = P(BASE)
|
||||
path = P(self.base)
|
||||
_check(path, "DIRB/FILE*", True, [])
|
||||
_check(path, "DIRB/FILE*", False, ["dirB/fileB"])
|
||||
_check(path, "dirb/file*", True, [])
|
||||
|
@ -1115,9 +1108,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def _check(path, glob, expected):
|
||||
actual = {path for path in path.glob(glob, follow_symlinks=True)
|
||||
if "linkD" not in path.parent.parts} # exclude symlink loop.
|
||||
self.assertEqual(actual, { P(BASE, q) for q in expected })
|
||||
self.assertEqual(actual, { P(self.base, q) for q in expected })
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
_check(p, "fileB", [])
|
||||
_check(p, "dir*/file*", ["dirB/fileB", "dirC/fileC"])
|
||||
_check(p, "*A", ["dirA", "fileA", "linkA"])
|
||||
|
@ -1140,9 +1133,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.skipTest("symlinks required")
|
||||
def _check(path, glob, expected):
|
||||
actual = {path for path in path.glob(glob, follow_symlinks=False)}
|
||||
self.assertEqual(actual, { P(BASE, q) for q in expected })
|
||||
self.assertEqual(actual, { P(self.base, q) for q in expected })
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
_check(p, "fileB", [])
|
||||
_check(p, "dir*/file*", ["dirB/fileB", "dirC/fileC"])
|
||||
_check(p, "*A", ["dirA", "fileA", "linkA"])
|
||||
|
@ -1160,9 +1153,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
def test_rglob_common(self):
|
||||
def _check(glob, expected):
|
||||
self.assertEqual(set(glob), {P(BASE, q) for q in expected})
|
||||
self.assertEqual(set(glob), {P(self.base, q) for q in expected})
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
it = p.rglob("fileA")
|
||||
self.assertIsInstance(it, collections.abc.Iterator)
|
||||
_check(it, ["fileA"])
|
||||
|
@ -1187,7 +1180,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
])
|
||||
_check(p.rglob(""), ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
|
||||
|
||||
p = P(BASE, "dirC")
|
||||
p = P(self.base, "dirC")
|
||||
_check(p.rglob("*"), ["dirC/fileC", "dirC/novel.txt",
|
||||
"dirC/dirD", "dirC/dirD/fileD"])
|
||||
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
|
||||
|
@ -1207,9 +1200,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def _check(path, glob, expected):
|
||||
actual = {path for path in path.rglob(glob, follow_symlinks=True)
|
||||
if 'linkD' not in path.parent.parts} # exclude symlink loop.
|
||||
self.assertEqual(actual, { P(BASE, q) for q in expected })
|
||||
self.assertEqual(actual, { P(self.base, q) for q in expected })
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
_check(p, "fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"])
|
||||
_check(p, "*/fileA", [])
|
||||
_check(p, "*/fileB", ["dirB/fileB", "dirA/linkC/fileB", "linkB/fileB"])
|
||||
|
@ -1220,7 +1213,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
_check(p, "", ["./", "dirA/", "dirA/linkC/", "dirA/linkC/linkD/", "dirB/", "dirB/linkD/",
|
||||
"dirC/", "dirE/", "dirC/dirD/", "linkB/", "linkB/linkD/"])
|
||||
|
||||
p = P(BASE, "dirC")
|
||||
p = P(self.base, "dirC")
|
||||
_check(p, "*", ["dirC/fileC", "dirC/novel.txt",
|
||||
"dirC/dirD", "dirC/dirD/fileD"])
|
||||
_check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"])
|
||||
|
@ -1236,9 +1229,9 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.skipTest("symlinks required")
|
||||
def _check(path, glob, expected):
|
||||
actual = {path for path in path.rglob(glob, follow_symlinks=False)}
|
||||
self.assertEqual(actual, { P(BASE, q) for q in expected })
|
||||
self.assertEqual(actual, { P(self.base, q) for q in expected })
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
_check(p, "fileB", ["dirB/fileB"])
|
||||
_check(p, "*/fileA", [])
|
||||
_check(p, "*/fileB", ["dirB/fileB"])
|
||||
|
@ -1246,7 +1239,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
_check(p, "*/", ["dirA/", "dirB/", "dirC/", "dirC/dirD/", "dirE/"])
|
||||
_check(p, "", ["./", "dirA/", "dirB/", "dirC/", "dirE/", "dirC/dirD/"])
|
||||
|
||||
p = P(BASE, "dirC")
|
||||
p = P(self.base, "dirC")
|
||||
_check(p, "*", ["dirC/fileC", "dirC/novel.txt",
|
||||
"dirC/dirD", "dirC/dirD/fileD"])
|
||||
_check(p, "file*", ["dirC/fileC", "dirC/dirD/fileD"])
|
||||
|
@ -1262,7 +1255,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
given = set(p.rglob('*'))
|
||||
expect = {'brokenLink',
|
||||
'dirA', 'dirA/linkC',
|
||||
|
@ -1280,7 +1273,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def test_glob_many_open_files(self):
|
||||
depth = 30
|
||||
P = self.cls
|
||||
p = base = P(BASE) / 'deep'
|
||||
p = base = P(self.base) / 'deep'
|
||||
p.mkdir()
|
||||
for _ in range(depth):
|
||||
p /= 'd'
|
||||
|
@ -1299,30 +1292,30 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def test_glob_dotdot(self):
|
||||
# ".." is not special in globs.
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
self.assertEqual(set(p.glob("..")), { P(BASE, "..") })
|
||||
self.assertEqual(set(p.glob("../..")), { P(BASE, "..", "..") })
|
||||
self.assertEqual(set(p.glob("dirA/..")), { P(BASE, "dirA", "..") })
|
||||
self.assertEqual(set(p.glob("dirA/../file*")), { P(BASE, "dirA/../fileA") })
|
||||
p = P(self.base)
|
||||
self.assertEqual(set(p.glob("..")), { P(self.base, "..") })
|
||||
self.assertEqual(set(p.glob("../..")), { P(self.base, "..", "..") })
|
||||
self.assertEqual(set(p.glob("dirA/..")), { P(self.base, "dirA", "..") })
|
||||
self.assertEqual(set(p.glob("dirA/../file*")), { P(self.base, "dirA/../fileA") })
|
||||
self.assertEqual(set(p.glob("dirA/../file*/..")), set())
|
||||
self.assertEqual(set(p.glob("../xyzzy")), set())
|
||||
self.assertEqual(set(p.glob("xyzzy/..")), set())
|
||||
self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(BASE, *[".."] * 50)})
|
||||
self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(self.base, *[".."] * 50)})
|
||||
|
||||
def test_glob_permissions(self):
|
||||
# See bpo-38894
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
P = self.cls
|
||||
base = P(BASE) / 'permissions'
|
||||
base = P(self.base) / 'permissions'
|
||||
base.mkdir()
|
||||
|
||||
for i in range(100):
|
||||
link = base / f"link{i}"
|
||||
if i % 2:
|
||||
link.symlink_to(P(BASE, "dirE", "nonexistent"))
|
||||
link.symlink_to(P(self.base, "dirE", "nonexistent"))
|
||||
else:
|
||||
link.symlink_to(P(BASE, "dirC"))
|
||||
link.symlink_to(P(self.base, "dirC"))
|
||||
|
||||
self.assertEqual(len(set(base.glob("*"))), 100)
|
||||
self.assertEqual(len(set(base.glob("*/"))), 50)
|
||||
|
@ -1333,7 +1326,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
# See gh-87695
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
base = self.cls(BASE) / 'long_symlink'
|
||||
base = self.cls(self.base) / 'long_symlink'
|
||||
base.mkdir()
|
||||
bad_link = base / 'bad_link'
|
||||
bad_link.symlink_to("bad" * 200)
|
||||
|
@ -1343,7 +1336,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
recursion_limit = 50
|
||||
# directory_depth > recursion_limit
|
||||
directory_depth = recursion_limit + 10
|
||||
base = self.cls(BASE, 'deep')
|
||||
base = self.cls(self.base, 'deep')
|
||||
path = base.joinpath(*(['d'] * directory_depth))
|
||||
path.mkdir(parents=True)
|
||||
|
||||
|
@ -1352,7 +1345,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
def test_glob_recursive_no_trailing_slash(self):
|
||||
P = self.cls
|
||||
p = P(BASE)
|
||||
p = P(self.base)
|
||||
with self.assertWarns(FutureWarning):
|
||||
p.glob('**')
|
||||
with self.assertWarns(FutureWarning):
|
||||
|
@ -1366,7 +1359,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def test_readlink(self):
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertEqual((P / 'linkA').readlink(), self.cls('fileA'))
|
||||
self.assertEqual((P / 'brokenLink').readlink(),
|
||||
self.cls('non-existing'))
|
||||
|
@ -1377,7 +1370,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
|
||||
@unittest.skipIf(hasattr(os, "readlink"), "os.readlink() is present")
|
||||
def test_readlink_unsupported(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
p = P / 'fileA'
|
||||
with self.assertRaises(pathlib.UnsupportedOperation):
|
||||
q.readlink(p)
|
||||
|
@ -1393,53 +1386,53 @@ class DummyPathTest(DummyPurePathTest):
|
|||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
P = self.cls
|
||||
p = P(BASE, 'foo')
|
||||
p = P(self.base, 'foo')
|
||||
with self.assertRaises(OSError) as cm:
|
||||
p.resolve(strict=True)
|
||||
self.assertEqual(cm.exception.errno, errno.ENOENT)
|
||||
# Non-strict
|
||||
self.assertEqualNormCase(str(p.resolve(strict=False)),
|
||||
os.path.join(BASE, 'foo'))
|
||||
p = P(BASE, 'foo', 'in', 'spam')
|
||||
os.path.join(self.base, 'foo'))
|
||||
p = P(self.base, 'foo', 'in', 'spam')
|
||||
self.assertEqualNormCase(str(p.resolve(strict=False)),
|
||||
os.path.join(BASE, 'foo', 'in', 'spam'))
|
||||
p = P(BASE, '..', 'foo', 'in', 'spam')
|
||||
os.path.join(self.base, 'foo', 'in', 'spam'))
|
||||
p = P(self.base, '..', 'foo', 'in', 'spam')
|
||||
self.assertEqualNormCase(str(p.resolve(strict=False)),
|
||||
os.path.abspath(os.path.join('foo', 'in', 'spam')))
|
||||
# These are all relative symlinks.
|
||||
p = P(BASE, 'dirB', 'fileB')
|
||||
p = P(self.base, 'dirB', 'fileB')
|
||||
self._check_resolve_relative(p, p)
|
||||
p = P(BASE, 'linkA')
|
||||
self._check_resolve_relative(p, P(BASE, 'fileA'))
|
||||
p = P(BASE, 'dirA', 'linkC', 'fileB')
|
||||
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB'))
|
||||
p = P(BASE, 'dirB', 'linkD', 'fileB')
|
||||
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB'))
|
||||
p = P(self.base, 'linkA')
|
||||
self._check_resolve_relative(p, P(self.base, 'fileA'))
|
||||
p = P(self.base, 'dirA', 'linkC', 'fileB')
|
||||
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB'))
|
||||
p = P(self.base, 'dirB', 'linkD', 'fileB')
|
||||
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB'))
|
||||
# Non-strict
|
||||
p = P(BASE, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam')
|
||||
self._check_resolve_relative(p, P(BASE, 'dirB', 'fileB', 'foo', 'in',
|
||||
p = P(self.base, 'dirA', 'linkC', 'fileB', 'foo', 'in', 'spam')
|
||||
self._check_resolve_relative(p, P(self.base, 'dirB', 'fileB', 'foo', 'in',
|
||||
'spam'), False)
|
||||
p = P(BASE, 'dirA', 'linkC', '..', 'foo', 'in', 'spam')
|
||||
p = P(self.base, 'dirA', 'linkC', '..', 'foo', 'in', 'spam')
|
||||
if os.name == 'nt' and isinstance(p, pathlib.Path):
|
||||
# In Windows, if linkY points to dirB, 'dirA\linkY\..'
|
||||
# resolves to 'dirA' without resolving linkY first.
|
||||
self._check_resolve_relative(p, P(BASE, 'dirA', 'foo', 'in',
|
||||
self._check_resolve_relative(p, P(self.base, 'dirA', 'foo', 'in',
|
||||
'spam'), False)
|
||||
else:
|
||||
# In Posix, if linkY points to dirB, 'dirA/linkY/..'
|
||||
# resolves to 'dirB/..' first before resolving to parent of dirB.
|
||||
self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False)
|
||||
self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False)
|
||||
# Now create absolute symlinks.
|
||||
d = self.tempdir()
|
||||
P(BASE, 'dirA', 'linkX').symlink_to(d)
|
||||
P(BASE, str(d), 'linkY').symlink_to(join('dirB'))
|
||||
p = P(BASE, 'dirA', 'linkX', 'linkY', 'fileB')
|
||||
self._check_resolve_absolute(p, P(BASE, 'dirB', 'fileB'))
|
||||
P(self.base, 'dirA', 'linkX').symlink_to(d)
|
||||
P(self.base, str(d), 'linkY').symlink_to(self.pathmod.join(self.base, 'dirB'))
|
||||
p = P(self.base, 'dirA', 'linkX', 'linkY', 'fileB')
|
||||
self._check_resolve_absolute(p, P(self.base, 'dirB', 'fileB'))
|
||||
# Non-strict
|
||||
p = P(BASE, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam')
|
||||
self._check_resolve_relative(p, P(BASE, 'dirB', 'foo', 'in', 'spam'),
|
||||
p = P(self.base, 'dirA', 'linkX', 'linkY', 'foo', 'in', 'spam')
|
||||
self._check_resolve_relative(p, P(self.base, 'dirB', 'foo', 'in', 'spam'),
|
||||
False)
|
||||
p = P(BASE, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam')
|
||||
p = P(self.base, 'dirA', 'linkX', 'linkY', '..', 'foo', 'in', 'spam')
|
||||
if os.name == 'nt' and isinstance(p, pathlib.Path):
|
||||
# In Windows, if linkY points to dirB, 'dirA\linkY\..'
|
||||
# resolves to 'dirA' without resolving linkY first.
|
||||
|
@ -1447,13 +1440,13 @@ class DummyPathTest(DummyPurePathTest):
|
|||
else:
|
||||
# In Posix, if linkY points to dirB, 'dirA/linkY/..'
|
||||
# resolves to 'dirB/..' first before resolving to parent of dirB.
|
||||
self._check_resolve_relative(p, P(BASE, 'foo', 'in', 'spam'), False)
|
||||
self._check_resolve_relative(p, P(self.base, 'foo', 'in', 'spam'), False)
|
||||
|
||||
def test_resolve_dot(self):
|
||||
# See http://web.archive.org/web/20200623062557/https://bitbucket.org/pitrou/pathlib/issues/9/
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
p = self.cls(BASE)
|
||||
p = self.cls(self.base)
|
||||
p.joinpath('0').symlink_to('.', target_is_directory=True)
|
||||
p.joinpath('1').symlink_to(os.path.join('0', '0'), target_is_directory=True)
|
||||
p.joinpath('2').symlink_to(os.path.join('1', '1'), target_is_directory=True)
|
||||
|
@ -1476,30 +1469,30 @@ class DummyPathTest(DummyPurePathTest):
|
|||
if os.name == 'nt' and issubclass(self.cls, pathlib.Path):
|
||||
self.skipTest("symlink loops work differently with concrete Windows paths")
|
||||
# Loops with relative symlinks.
|
||||
self.cls(BASE, 'linkX').symlink_to('linkX/inside')
|
||||
self._check_symlink_loop(BASE, 'linkX')
|
||||
self.cls(BASE, 'linkY').symlink_to('linkY')
|
||||
self._check_symlink_loop(BASE, 'linkY')
|
||||
self.cls(BASE, 'linkZ').symlink_to('linkZ/../linkZ')
|
||||
self._check_symlink_loop(BASE, 'linkZ')
|
||||
self.cls(self.base, 'linkX').symlink_to('linkX/inside')
|
||||
self._check_symlink_loop(self.base, 'linkX')
|
||||
self.cls(self.base, 'linkY').symlink_to('linkY')
|
||||
self._check_symlink_loop(self.base, 'linkY')
|
||||
self.cls(self.base, 'linkZ').symlink_to('linkZ/../linkZ')
|
||||
self._check_symlink_loop(self.base, 'linkZ')
|
||||
# Non-strict
|
||||
p = self.cls(BASE, 'linkZ', 'foo')
|
||||
p = self.cls(self.base, 'linkZ', 'foo')
|
||||
self.assertEqual(p.resolve(strict=False), p)
|
||||
# Loops with absolute symlinks.
|
||||
self.cls(BASE, 'linkU').symlink_to(join('linkU/inside'))
|
||||
self._check_symlink_loop(BASE, 'linkU')
|
||||
self.cls(BASE, 'linkV').symlink_to(join('linkV'))
|
||||
self._check_symlink_loop(BASE, 'linkV')
|
||||
self.cls(BASE, 'linkW').symlink_to(join('linkW/../linkW'))
|
||||
self._check_symlink_loop(BASE, 'linkW')
|
||||
self.cls(self.base, 'linkU').symlink_to(self.pathmod.join(self.base, 'linkU/inside'))
|
||||
self._check_symlink_loop(self.base, 'linkU')
|
||||
self.cls(self.base, 'linkV').symlink_to(self.pathmod.join(self.base, 'linkV'))
|
||||
self._check_symlink_loop(self.base, 'linkV')
|
||||
self.cls(self.base, 'linkW').symlink_to(self.pathmod.join(self.base, 'linkW/../linkW'))
|
||||
self._check_symlink_loop(self.base, 'linkW')
|
||||
# Non-strict
|
||||
q = self.cls(BASE, 'linkW', 'foo')
|
||||
q = self.cls(self.base, 'linkW', 'foo')
|
||||
self.assertEqual(q.resolve(strict=False), q)
|
||||
|
||||
def test_stat(self):
|
||||
statA = self.cls(BASE).joinpath('fileA').stat()
|
||||
statB = self.cls(BASE).joinpath('dirB', 'fileB').stat()
|
||||
statC = self.cls(BASE).joinpath('dirC').stat()
|
||||
statA = self.cls(self.base).joinpath('fileA').stat()
|
||||
statB = self.cls(self.base).joinpath('dirB', 'fileB').stat()
|
||||
statC = self.cls(self.base).joinpath('dirC').stat()
|
||||
# st_mode: files are the same, directory differs.
|
||||
self.assertIsInstance(statA.st_mode, int)
|
||||
self.assertEqual(statA.st_mode, statB.st_mode)
|
||||
|
@ -1519,29 +1512,29 @@ class DummyPathTest(DummyPurePathTest):
|
|||
def test_stat_no_follow_symlinks(self):
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
p = self.cls(BASE) / 'linkA'
|
||||
p = self.cls(self.base) / 'linkA'
|
||||
st = p.stat()
|
||||
self.assertNotEqual(st, p.stat(follow_symlinks=False))
|
||||
|
||||
def test_stat_no_follow_symlinks_nosymlink(self):
|
||||
p = self.cls(BASE) / 'fileA'
|
||||
p = self.cls(self.base) / 'fileA'
|
||||
st = p.stat()
|
||||
self.assertEqual(st, p.stat(follow_symlinks=False))
|
||||
|
||||
def test_lstat(self):
|
||||
if not self.can_symlink:
|
||||
self.skipTest("symlinks required")
|
||||
p = self.cls(BASE)/ 'linkA'
|
||||
p = self.cls(self.base)/ 'linkA'
|
||||
st = p.stat()
|
||||
self.assertNotEqual(st, p.lstat())
|
||||
|
||||
def test_lstat_nosymlink(self):
|
||||
p = self.cls(BASE) / 'fileA'
|
||||
p = self.cls(self.base) / 'fileA'
|
||||
st = p.stat()
|
||||
self.assertEqual(st, p.lstat())
|
||||
|
||||
def test_is_dir(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertTrue((P / 'dirA').is_dir())
|
||||
self.assertFalse((P / 'fileA').is_dir())
|
||||
self.assertFalse((P / 'non-existing').is_dir())
|
||||
|
@ -1554,7 +1547,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'dirA\x00').is_dir())
|
||||
|
||||
def test_is_dir_no_follow_symlinks(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False))
|
||||
self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False))
|
||||
self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False))
|
||||
|
@ -1567,7 +1560,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False))
|
||||
|
||||
def test_is_file(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertTrue((P / 'fileA').is_file())
|
||||
self.assertFalse((P / 'dirA').is_file())
|
||||
self.assertFalse((P / 'non-existing').is_file())
|
||||
|
@ -1580,7 +1573,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'fileA\x00').is_file())
|
||||
|
||||
def test_is_file_no_follow_symlinks(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertTrue((P / 'fileA').is_file(follow_symlinks=False))
|
||||
self.assertFalse((P / 'dirA').is_file(follow_symlinks=False))
|
||||
self.assertFalse((P / 'non-existing').is_file(follow_symlinks=False))
|
||||
|
@ -1593,7 +1586,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'fileA\x00').is_file(follow_symlinks=False))
|
||||
|
||||
def test_is_mount(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_mount())
|
||||
self.assertFalse((P / 'dirA').is_mount())
|
||||
self.assertFalse((P / 'non-existing').is_mount())
|
||||
|
@ -1602,7 +1595,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'linkA').is_mount())
|
||||
|
||||
def test_is_symlink(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_symlink())
|
||||
self.assertFalse((P / 'dirA').is_symlink())
|
||||
self.assertFalse((P / 'non-existing').is_symlink())
|
||||
|
@ -1618,7 +1611,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs((P / 'linkA\x00').is_file(), False)
|
||||
|
||||
def test_is_junction_false(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_junction())
|
||||
self.assertFalse((P / 'dirA').is_junction())
|
||||
self.assertFalse((P / 'non-existing').is_junction())
|
||||
|
@ -1627,7 +1620,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertFalse((P / 'fileA\x00').is_junction())
|
||||
|
||||
def test_is_fifo_false(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_fifo())
|
||||
self.assertFalse((P / 'dirA').is_fifo())
|
||||
self.assertFalse((P / 'non-existing').is_fifo())
|
||||
|
@ -1636,7 +1629,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs((P / 'fileA\x00').is_fifo(), False)
|
||||
|
||||
def test_is_socket_false(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_socket())
|
||||
self.assertFalse((P / 'dirA').is_socket())
|
||||
self.assertFalse((P / 'non-existing').is_socket())
|
||||
|
@ -1645,7 +1638,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs((P / 'fileA\x00').is_socket(), False)
|
||||
|
||||
def test_is_block_device_false(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_block_device())
|
||||
self.assertFalse((P / 'dirA').is_block_device())
|
||||
self.assertFalse((P / 'non-existing').is_block_device())
|
||||
|
@ -1654,7 +1647,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs((P / 'fileA\x00').is_block_device(), False)
|
||||
|
||||
def test_is_char_device_false(self):
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
self.assertFalse((P / 'fileA').is_char_device())
|
||||
self.assertFalse((P / 'dirA').is_char_device())
|
||||
self.assertFalse((P / 'non-existing').is_char_device())
|
||||
|
@ -1663,7 +1656,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.assertIs((P / 'fileA\x00').is_char_device(), False)
|
||||
|
||||
def test_pickling_common(self):
|
||||
p = self.cls(BASE, 'fileA')
|
||||
p = self.cls(self.base, 'fileA')
|
||||
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
|
||||
dumped = pickle.dumps(p, proto)
|
||||
pp = pickle.loads(dumped)
|
||||
|
@ -1683,7 +1676,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
self.skipTest("symlinks required")
|
||||
|
||||
# Test solving a non-looping chain of symlinks (issue #19887).
|
||||
P = self.cls(BASE)
|
||||
P = self.cls(self.base)
|
||||
P.joinpath('link1').symlink_to(os.path.join('link0', 'link0'), target_is_directory=True)
|
||||
P.joinpath('link2').symlink_to(os.path.join('link1', 'link1'), target_is_directory=True)
|
||||
P.joinpath('link3').symlink_to(os.path.join('link2', 'link2'), target_is_directory=True)
|
||||
|
@ -1692,16 +1685,16 @@ class DummyPathTest(DummyPurePathTest):
|
|||
# Resolve absolute paths.
|
||||
p = (P / 'link0').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = (P / 'link1').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = (P / 'link2').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = (P / 'link3').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
|
||||
# Resolve relative paths.
|
||||
try:
|
||||
|
@ -1709,25 +1702,25 @@ class DummyPathTest(DummyPurePathTest):
|
|||
except pathlib.UnsupportedOperation:
|
||||
return
|
||||
old_path = os.getcwd()
|
||||
os.chdir(BASE)
|
||||
os.chdir(self.base)
|
||||
try:
|
||||
p = self.cls('link0').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = self.cls('link1').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = self.cls('link2').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
p = self.cls('link3').resolve()
|
||||
self.assertEqual(p, P)
|
||||
self.assertEqualNormCase(str(p), BASE)
|
||||
self.assertEqualNormCase(str(p), self.base)
|
||||
finally:
|
||||
os.chdir(old_path)
|
||||
|
||||
def test_complex_symlinks_absolute(self):
|
||||
self._check_complex_symlinks(BASE)
|
||||
self._check_complex_symlinks(self.base)
|
||||
|
||||
def test_complex_symlinks_relative(self):
|
||||
self._check_complex_symlinks('.')
|
||||
|
@ -1750,7 +1743,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
# broken_link2
|
||||
# TEST2/
|
||||
# tmp4 a lone file
|
||||
self.walk_path = self.cls(BASE, "TEST1")
|
||||
self.walk_path = self.cls(self.base, "TEST1")
|
||||
self.sub1_path = self.walk_path / "SUB1"
|
||||
self.sub11_path = self.sub1_path / "SUB11"
|
||||
self.sub2_path = self.walk_path / "SUB2"
|
||||
|
@ -1758,8 +1751,8 @@ class DummyPathTest(DummyPurePathTest):
|
|||
tmp2_path = self.sub1_path / "tmp2"
|
||||
tmp3_path = self.sub2_path / "tmp3"
|
||||
self.link_path = self.sub2_path / "link"
|
||||
t2_path = self.cls(BASE, "TEST2")
|
||||
tmp4_path = self.cls(BASE, "TEST2", "tmp4")
|
||||
t2_path = self.cls(self.base, "TEST2")
|
||||
tmp4_path = self.cls(self.base, "TEST2", "tmp4")
|
||||
broken_link_path = self.sub2_path / "broken_link"
|
||||
broken_link2_path = self.sub2_path / "broken_link2"
|
||||
|
||||
|
@ -1886,7 +1879,7 @@ class DummyPathTest(DummyPurePathTest):
|
|||
recursion_limit = 40
|
||||
# directory_depth > recursion_limit
|
||||
directory_depth = recursion_limit + 10
|
||||
base = self.cls(BASE, 'deep')
|
||||
base = self.cls(self.base, 'deep')
|
||||
path = base.joinpath(*(['d'] * directory_depth))
|
||||
path.mkdir(parents=True)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue