Kaydet (Commit) ec1f5df4 authored tarafından Vajrasky Kok's avatar Vajrasky Kok Kaydeden (comit) Brett Cannon

bpo-19791: Use functions from test support to check the symlink support. (GH-822)

üst 1cf93a76
...@@ -1176,25 +1176,10 @@ BASE = os.path.realpath(TESTFN) ...@@ -1176,25 +1176,10 @@ BASE = os.path.realpath(TESTFN)
join = lambda *x: os.path.join(BASE, *x) join = lambda *x: os.path.join(BASE, *x)
rel_join = lambda *x: os.path.join(TESTFN, *x) rel_join = lambda *x: os.path.join(TESTFN, *x)
def symlink_skip_reason():
if not pathlib.supports_symlinks:
return "no system support for symlinks"
try:
os.symlink(__file__, BASE)
except OSError as e:
return str(e)
else:
support.unlink(BASE)
return None
symlink_skip_reason = symlink_skip_reason()
only_nt = unittest.skipIf(os.name != 'nt', only_nt = unittest.skipIf(os.name != 'nt',
'test requires a Windows-compatible system') 'test requires a Windows-compatible system')
only_posix = unittest.skipIf(os.name == 'nt', only_posix = unittest.skipIf(os.name == 'nt',
'test requires a POSIX-compatible system') 'test requires a POSIX-compatible system')
with_symlinks = unittest.skipIf(symlink_skip_reason, symlink_skip_reason)
@only_posix @only_posix
class PosixPathAsPureTest(PurePosixPathTest): class PosixPathAsPureTest(PurePosixPathTest):
...@@ -1256,7 +1241,7 @@ class _BasePathTest(object): ...@@ -1256,7 +1241,7 @@ class _BasePathTest(object):
with open(join('dirC', 'dirD', 'fileD'), 'wb') as f: with open(join('dirC', 'dirD', 'fileD'), 'wb') as f:
f.write(b"this is file D\n") f.write(b"this is file D\n")
os.chmod(join('dirE'), 0) os.chmod(join('dirE'), 0)
if not symlink_skip_reason: if support.can_symlink():
# Relative symlinks # Relative symlinks
os.symlink('fileA', join('linkA')) os.symlink('fileA', join('linkA'))
os.symlink('non-existing', join('brokenLink')) os.symlink('non-existing', join('brokenLink'))
...@@ -1350,7 +1335,7 @@ class _BasePathTest(object): ...@@ -1350,7 +1335,7 @@ class _BasePathTest(object):
self.assertIs(True, (p / 'dirA').exists()) self.assertIs(True, (p / 'dirA').exists())
self.assertIs(True, (p / 'fileA').exists()) self.assertIs(True, (p / 'fileA').exists())
self.assertIs(False, (p / 'fileA' / 'bah').exists()) self.assertIs(False, (p / 'fileA' / 'bah').exists())
if not symlink_skip_reason: if support.can_symlink():
self.assertIs(True, (p / 'linkA').exists()) self.assertIs(True, (p / 'linkA').exists())
self.assertIs(True, (p / 'linkB').exists()) self.assertIs(True, (p / 'linkB').exists())
self.assertIs(True, (p / 'linkB' / 'fileB').exists()) self.assertIs(True, (p / 'linkB' / 'fileB').exists())
...@@ -1393,11 +1378,11 @@ class _BasePathTest(object): ...@@ -1393,11 +1378,11 @@ class _BasePathTest(object):
it = p.iterdir() it = p.iterdir()
paths = set(it) paths = set(it)
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA'] expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
if not symlink_skip_reason: if support.can_symlink():
expected += ['linkA', 'linkB', 'brokenLink'] expected += ['linkA', 'linkB', 'brokenLink']
self.assertEqual(paths, { P(BASE, q) for q in expected }) self.assertEqual(paths, { P(BASE, q) for q in expected })
@with_symlinks @support.skip_unless_symlink
def test_iterdir_symlink(self): def test_iterdir_symlink(self):
# __iter__ on a symlink to a directory # __iter__ on a symlink to a directory
P = self.cls P = self.cls
...@@ -1426,16 +1411,16 @@ class _BasePathTest(object): ...@@ -1426,16 +1411,16 @@ class _BasePathTest(object):
_check(it, ["fileA"]) _check(it, ["fileA"])
_check(p.glob("fileB"), []) _check(p.glob("fileB"), [])
_check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"])
if symlink_skip_reason: if not support.can_symlink():
_check(p.glob("*A"), ['dirA', 'fileA']) _check(p.glob("*A"), ['dirA', 'fileA'])
else: else:
_check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) _check(p.glob("*A"), ['dirA', 'fileA', 'linkA'])
if symlink_skip_reason: if not support.can_symlink():
_check(p.glob("*B/*"), ['dirB/fileB']) _check(p.glob("*B/*"), ['dirB/fileB'])
else: else:
_check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD',
'linkB/fileB', 'linkB/linkD']) 'linkB/fileB', 'linkB/linkD'])
if symlink_skip_reason: if not support.can_symlink():
_check(p.glob("*/fileB"), ['dirB/fileB']) _check(p.glob("*/fileB"), ['dirB/fileB'])
else: else:
_check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
...@@ -1450,7 +1435,7 @@ class _BasePathTest(object): ...@@ -1450,7 +1435,7 @@ class _BasePathTest(object):
_check(it, ["fileA"]) _check(it, ["fileA"])
_check(p.rglob("fileB"), ["dirB/fileB"]) _check(p.rglob("fileB"), ["dirB/fileB"])
_check(p.rglob("*/fileA"), []) _check(p.rglob("*/fileA"), [])
if symlink_skip_reason: if not support.can_symlink():
_check(p.rglob("*/fileB"), ["dirB/fileB"]) _check(p.rglob("*/fileB"), ["dirB/fileB"])
else: else:
_check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB", _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB",
...@@ -1461,7 +1446,7 @@ class _BasePathTest(object): ...@@ -1461,7 +1446,7 @@ class _BasePathTest(object):
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("*/*"), ["dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"])
@with_symlinks @support.skip_unless_symlink
def test_rglob_symlink_loop(self): def test_rglob_symlink_loop(self):
# Don't get fooled by symlink loops (Issue #26012) # Don't get fooled by symlink loops (Issue #26012)
P = self.cls P = self.cls
...@@ -1494,7 +1479,7 @@ class _BasePathTest(object): ...@@ -1494,7 +1479,7 @@ class _BasePathTest(object):
# this can be used to check both relative and absolute resolutions # this can be used to check both relative and absolute resolutions
_check_resolve_relative = _check_resolve_absolute = _check_resolve _check_resolve_relative = _check_resolve_absolute = _check_resolve
@with_symlinks @support.skip_unless_symlink
def test_resolve_common(self): def test_resolve_common(self):
P = self.cls P = self.cls
p = P(BASE, 'foo') p = P(BASE, 'foo')
...@@ -1551,7 +1536,7 @@ class _BasePathTest(object): ...@@ -1551,7 +1536,7 @@ class _BasePathTest(object):
# resolves to 'dirB/..' first before resolving to parent of dirB. # resolves to 'dirB/..' first before resolving to parent of dirB.
self._check_resolve_relative(p, P(BASE, 'foo'), False) self._check_resolve_relative(p, P(BASE, 'foo'), False)
@with_symlinks @support.skip_unless_symlink
def test_resolve_dot(self): def test_resolve_dot(self):
# See https://bitbucket.org/pitrou/pathlib/issue/9/pathresolve-fails-on-complex-symlinks # See https://bitbucket.org/pitrou/pathlib/issue/9/pathresolve-fails-on-complex-symlinks
p = self.cls(BASE) p = self.cls(BASE)
...@@ -1603,7 +1588,7 @@ class _BasePathTest(object): ...@@ -1603,7 +1588,7 @@ class _BasePathTest(object):
self.addCleanup(p.chmod, st.st_mode) self.addCleanup(p.chmod, st.st_mode)
self.assertNotEqual(p.stat(), st) self.assertNotEqual(p.stat(), st)
@with_symlinks @support.skip_unless_symlink
def test_lstat(self): def test_lstat(self):
p = self.cls(BASE)/ 'linkA' p = self.cls(BASE)/ 'linkA'
st = p.stat() st = p.stat()
...@@ -1816,7 +1801,7 @@ class _BasePathTest(object): ...@@ -1816,7 +1801,7 @@ class _BasePathTest(object):
p.mkdir(exist_ok=True) p.mkdir(exist_ok=True)
self.assertEqual(cm.exception.errno, errno.EEXIST) self.assertEqual(cm.exception.errno, errno.EEXIST)
@with_symlinks @support.skip_unless_symlink
def test_symlink_to(self): def test_symlink_to(self):
P = self.cls(BASE) P = self.cls(BASE)
target = P / 'fileA' target = P / 'fileA'
...@@ -1846,7 +1831,7 @@ class _BasePathTest(object): ...@@ -1846,7 +1831,7 @@ class _BasePathTest(object):
self.assertFalse((P / 'fileA').is_dir()) self.assertFalse((P / 'fileA').is_dir())
self.assertFalse((P / 'non-existing').is_dir()) self.assertFalse((P / 'non-existing').is_dir())
self.assertFalse((P / 'fileA' / 'bah').is_dir()) self.assertFalse((P / 'fileA' / 'bah').is_dir())
if not symlink_skip_reason: if support.can_symlink():
self.assertFalse((P / 'linkA').is_dir()) self.assertFalse((P / 'linkA').is_dir())
self.assertTrue((P / 'linkB').is_dir()) self.assertTrue((P / 'linkB').is_dir())
self.assertFalse((P/ 'brokenLink').is_dir()) self.assertFalse((P/ 'brokenLink').is_dir())
...@@ -1857,7 +1842,7 @@ class _BasePathTest(object): ...@@ -1857,7 +1842,7 @@ class _BasePathTest(object):
self.assertFalse((P / 'dirA').is_file()) self.assertFalse((P / 'dirA').is_file())
self.assertFalse((P / 'non-existing').is_file()) self.assertFalse((P / 'non-existing').is_file())
self.assertFalse((P / 'fileA' / 'bah').is_file()) self.assertFalse((P / 'fileA' / 'bah').is_file())
if not symlink_skip_reason: if support.can_symlink():
self.assertTrue((P / 'linkA').is_file()) self.assertTrue((P / 'linkA').is_file())
self.assertFalse((P / 'linkB').is_file()) self.assertFalse((P / 'linkB').is_file())
self.assertFalse((P/ 'brokenLink').is_file()) self.assertFalse((P/ 'brokenLink').is_file())
...@@ -1868,7 +1853,7 @@ class _BasePathTest(object): ...@@ -1868,7 +1853,7 @@ class _BasePathTest(object):
self.assertFalse((P / 'dirA').is_symlink()) self.assertFalse((P / 'dirA').is_symlink())
self.assertFalse((P / 'non-existing').is_symlink()) self.assertFalse((P / 'non-existing').is_symlink())
self.assertFalse((P / 'fileA' / 'bah').is_symlink()) self.assertFalse((P / 'fileA' / 'bah').is_symlink())
if not symlink_skip_reason: if support.can_symlink():
self.assertTrue((P / 'linkA').is_symlink()) self.assertTrue((P / 'linkA').is_symlink())
self.assertTrue((P / 'linkB').is_symlink()) self.assertTrue((P / 'linkB').is_symlink())
self.assertTrue((P/ 'brokenLink').is_symlink()) self.assertTrue((P/ 'brokenLink').is_symlink())
...@@ -1991,15 +1976,15 @@ class _BasePathTest(object): ...@@ -1991,15 +1976,15 @@ class _BasePathTest(object):
finally: finally:
os.chdir(old_path) os.chdir(old_path)
@with_symlinks @support.skip_unless_symlink
def test_complex_symlinks_absolute(self): def test_complex_symlinks_absolute(self):
self._check_complex_symlinks(BASE) self._check_complex_symlinks(BASE)
@with_symlinks @support.skip_unless_symlink
def test_complex_symlinks_relative(self): def test_complex_symlinks_relative(self):
self._check_complex_symlinks('.') self._check_complex_symlinks('.')
@with_symlinks @support.skip_unless_symlink
def test_complex_symlinks_relative_dot_dot(self): def test_complex_symlinks_relative_dot_dot(self):
self._check_complex_symlinks(os.path.join('dirA', '..')) self._check_complex_symlinks(os.path.join('dirA', '..'))
...@@ -2062,7 +2047,7 @@ class PosixPathTest(_BasePathTest, unittest.TestCase): ...@@ -2062,7 +2047,7 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
st = os.stat(join('masked_new_file')) st = os.stat(join('masked_new_file'))
self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) self.assertEqual(stat.S_IMODE(st.st_mode), 0o750)
@with_symlinks @support.skip_unless_symlink
def test_resolve_loop(self): def test_resolve_loop(self):
# Loops with relative symlinks # Loops with relative symlinks
os.symlink('linkX/inside', join('linkX')) os.symlink('linkX/inside', join('linkX'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment