Skip to content

Commit ec1f5df

Browse files
vajraskybrettcannon
authored andcommitted
bpo-19791: Use functions from test support to check the symlink support. (GH-822)
1 parent 1cf93a7 commit ec1f5df

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

Lib/test/test_pathlib.py

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,25 +1176,10 @@ def test_different_flavours_unordered(self):
11761176
join = lambda *x: os.path.join(BASE, *x)
11771177
rel_join = lambda *x: os.path.join(TESTFN, *x)
11781178

1179-
def symlink_skip_reason():
1180-
if not pathlib.supports_symlinks:
1181-
return "no system support for symlinks"
1182-
try:
1183-
os.symlink(__file__, BASE)
1184-
except OSError as e:
1185-
return str(e)
1186-
else:
1187-
support.unlink(BASE)
1188-
return None
1189-
1190-
symlink_skip_reason = symlink_skip_reason()
1191-
11921179
only_nt = unittest.skipIf(os.name != 'nt',
11931180
'test requires a Windows-compatible system')
11941181
only_posix = unittest.skipIf(os.name == 'nt',
11951182
'test requires a POSIX-compatible system')
1196-
with_symlinks = unittest.skipIf(symlink_skip_reason, symlink_skip_reason)
1197-
11981183

11991184
@only_posix
12001185
class PosixPathAsPureTest(PurePosixPathTest):
@@ -1256,7 +1241,7 @@ def cleanup():
12561241
with open(join('dirC', 'dirD', 'fileD'), 'wb') as f:
12571242
f.write(b"this is file D\n")
12581243
os.chmod(join('dirE'), 0)
1259-
if not symlink_skip_reason:
1244+
if support.can_symlink():
12601245
# Relative symlinks
12611246
os.symlink('fileA', join('linkA'))
12621247
os.symlink('non-existing', join('brokenLink'))
@@ -1350,7 +1335,7 @@ def test_exists(self):
13501335
self.assertIs(True, (p / 'dirA').exists())
13511336
self.assertIs(True, (p / 'fileA').exists())
13521337
self.assertIs(False, (p / 'fileA' / 'bah').exists())
1353-
if not symlink_skip_reason:
1338+
if support.can_symlink():
13541339
self.assertIs(True, (p / 'linkA').exists())
13551340
self.assertIs(True, (p / 'linkB').exists())
13561341
self.assertIs(True, (p / 'linkB' / 'fileB').exists())
@@ -1393,11 +1378,11 @@ def test_iterdir(self):
13931378
it = p.iterdir()
13941379
paths = set(it)
13951380
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
1396-
if not symlink_skip_reason:
1381+
if support.can_symlink():
13971382
expected += ['linkA', 'linkB', 'brokenLink']
13981383
self.assertEqual(paths, { P(BASE, q) for q in expected })
13991384

1400-
@with_symlinks
1385+
@support.skip_unless_symlink
14011386
def test_iterdir_symlink(self):
14021387
# __iter__ on a symlink to a directory
14031388
P = self.cls
@@ -1426,16 +1411,16 @@ def _check(glob, expected):
14261411
_check(it, ["fileA"])
14271412
_check(p.glob("fileB"), [])
14281413
_check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"])
1429-
if symlink_skip_reason:
1414+
if not support.can_symlink():
14301415
_check(p.glob("*A"), ['dirA', 'fileA'])
14311416
else:
14321417
_check(p.glob("*A"), ['dirA', 'fileA', 'linkA'])
1433-
if symlink_skip_reason:
1418+
if not support.can_symlink():
14341419
_check(p.glob("*B/*"), ['dirB/fileB'])
14351420
else:
14361421
_check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD',
14371422
'linkB/fileB', 'linkB/linkD'])
1438-
if symlink_skip_reason:
1423+
if not support.can_symlink():
14391424
_check(p.glob("*/fileB"), ['dirB/fileB'])
14401425
else:
14411426
_check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])
@@ -1450,7 +1435,7 @@ def _check(glob, expected):
14501435
_check(it, ["fileA"])
14511436
_check(p.rglob("fileB"), ["dirB/fileB"])
14521437
_check(p.rglob("*/fileA"), [])
1453-
if symlink_skip_reason:
1438+
if not support.can_symlink():
14541439
_check(p.rglob("*/fileB"), ["dirB/fileB"])
14551440
else:
14561441
_check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB",
@@ -1461,7 +1446,7 @@ def _check(glob, expected):
14611446
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
14621447
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
14631448

1464-
@with_symlinks
1449+
@support.skip_unless_symlink
14651450
def test_rglob_symlink_loop(self):
14661451
# Don't get fooled by symlink loops (Issue #26012)
14671452
P = self.cls
@@ -1494,7 +1479,7 @@ def _check_resolve(self, p, expected, strict=True):
14941479
# this can be used to check both relative and absolute resolutions
14951480
_check_resolve_relative = _check_resolve_absolute = _check_resolve
14961481

1497-
@with_symlinks
1482+
@support.skip_unless_symlink
14981483
def test_resolve_common(self):
14991484
P = self.cls
15001485
p = P(BASE, 'foo')
@@ -1551,7 +1536,7 @@ def test_resolve_common(self):
15511536
# resolves to 'dirB/..' first before resolving to parent of dirB.
15521537
self._check_resolve_relative(p, P(BASE, 'foo'), False)
15531538

1554-
@with_symlinks
1539+
@support.skip_unless_symlink
15551540
def test_resolve_dot(self):
15561541
# See https://bitbucket.org/pitrou/pathlib/issue/9/pathresolve-fails-on-complex-symlinks
15571542
p = self.cls(BASE)
@@ -1603,7 +1588,7 @@ def test_stat(self):
16031588
self.addCleanup(p.chmod, st.st_mode)
16041589
self.assertNotEqual(p.stat(), st)
16051590

1606-
@with_symlinks
1591+
@support.skip_unless_symlink
16071592
def test_lstat(self):
16081593
p = self.cls(BASE)/ 'linkA'
16091594
st = p.stat()
@@ -1816,7 +1801,7 @@ def test_mkdir_no_parents_file(self):
18161801
p.mkdir(exist_ok=True)
18171802
self.assertEqual(cm.exception.errno, errno.EEXIST)
18181803

1819-
@with_symlinks
1804+
@support.skip_unless_symlink
18201805
def test_symlink_to(self):
18211806
P = self.cls(BASE)
18221807
target = P / 'fileA'
@@ -1846,7 +1831,7 @@ def test_is_dir(self):
18461831
self.assertFalse((P / 'fileA').is_dir())
18471832
self.assertFalse((P / 'non-existing').is_dir())
18481833
self.assertFalse((P / 'fileA' / 'bah').is_dir())
1849-
if not symlink_skip_reason:
1834+
if support.can_symlink():
18501835
self.assertFalse((P / 'linkA').is_dir())
18511836
self.assertTrue((P / 'linkB').is_dir())
18521837
self.assertFalse((P/ 'brokenLink').is_dir())
@@ -1857,7 +1842,7 @@ def test_is_file(self):
18571842
self.assertFalse((P / 'dirA').is_file())
18581843
self.assertFalse((P / 'non-existing').is_file())
18591844
self.assertFalse((P / 'fileA' / 'bah').is_file())
1860-
if not symlink_skip_reason:
1845+
if support.can_symlink():
18611846
self.assertTrue((P / 'linkA').is_file())
18621847
self.assertFalse((P / 'linkB').is_file())
18631848
self.assertFalse((P/ 'brokenLink').is_file())
@@ -1868,7 +1853,7 @@ def test_is_symlink(self):
18681853
self.assertFalse((P / 'dirA').is_symlink())
18691854
self.assertFalse((P / 'non-existing').is_symlink())
18701855
self.assertFalse((P / 'fileA' / 'bah').is_symlink())
1871-
if not symlink_skip_reason:
1856+
if support.can_symlink():
18721857
self.assertTrue((P / 'linkA').is_symlink())
18731858
self.assertTrue((P / 'linkB').is_symlink())
18741859
self.assertTrue((P/ 'brokenLink').is_symlink())
@@ -1991,15 +1976,15 @@ def _check_complex_symlinks(self, link0_target):
19911976
finally:
19921977
os.chdir(old_path)
19931978

1994-
@with_symlinks
1979+
@support.skip_unless_symlink
19951980
def test_complex_symlinks_absolute(self):
19961981
self._check_complex_symlinks(BASE)
19971982

1998-
@with_symlinks
1983+
@support.skip_unless_symlink
19991984
def test_complex_symlinks_relative(self):
20001985
self._check_complex_symlinks('.')
20011986

2002-
@with_symlinks
1987+
@support.skip_unless_symlink
20031988
def test_complex_symlinks_relative_dot_dot(self):
20041989
self._check_complex_symlinks(os.path.join('dirA', '..'))
20051990

@@ -2062,7 +2047,7 @@ def test_touch_mode(self):
20622047
st = os.stat(join('masked_new_file'))
20632048
self.assertEqual(stat.S_IMODE(st.st_mode), 0o750)
20642049

2065-
@with_symlinks
2050+
@support.skip_unless_symlink
20662051
def test_resolve_loop(self):
20672052
# Loops with relative symlinks
20682053
os.symlink('linkX/inside', join('linkX'))

0 commit comments

Comments
 (0)