diff -r 976e41a50022 Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Tue Nov 26 01:32:15 2013 +0100 +++ b/Lib/test/test_pathlib.py Tue Nov 26 14:52:12 2013 +0800 @@ -7,13 +7,10 @@ import shutil import socket import stat -import sys import tempfile import unittest -from contextlib import contextmanager - -from test import support -TESTFN = support.TESTFN +from test.support import (can_symlink, skip_unless_symlink, + fs_is_case_insensitive, TESTFN) try: import grp, pwd @@ -1030,24 +1027,10 @@ join = lambda *x: os.path.join(BASE, *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', 'test requires a Windows-compatible system') only_posix = unittest.skipIf(os.name == 'nt', 'test requires a POSIX-compatible system') -with_symlinks = unittest.skipIf(symlink_skip_reason, symlink_skip_reason) @only_posix @@ -1092,7 +1075,7 @@ f.write(b"this is file C\n") with open(join('dirC', 'dirD', 'fileD'), 'wb') as f: f.write(b"this is file D\n") - if not symlink_skip_reason: + if can_symlink(): if os.name == 'nt': # Workaround for http://bugs.python.org/issue13772 def dirlink(src, dest): @@ -1140,7 +1123,7 @@ self.assertIs(True, p.exists()) self.assertIs(True, (p / 'dirA').exists()) self.assertIs(True, (p / 'fileA').exists()) - if not symlink_skip_reason: + if can_symlink(): self.assertIs(True, (p / 'linkA').exists()) self.assertIs(True, (p / 'linkB').exists()) self.assertIs(False, (p / 'foo').exists()) @@ -1164,11 +1147,11 @@ it = p.iterdir() paths = set(it) expected = ['dirA', 'dirB', 'dirC', 'fileA'] - if not symlink_skip_reason: + if can_symlink(): expected += ['linkA', 'linkB', 'brokenLink'] self.assertEqual(paths, { P(BASE, q) for q in expected }) - @with_symlinks + @skip_unless_symlink def test_iterdir_symlink(self): # __iter__ on a symlink to a directory P = self.cls @@ -1197,19 +1180,19 @@ _check(it, ["fileA"]) _check(p.glob("fileB"), []) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) - if symlink_skip_reason: + if can_symlink(): + _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) + else: _check(p.glob("*A"), ['dirA', 'fileA']) - else: - _check(p.glob("*A"), ['dirA', 'fileA', 'linkA']) - if symlink_skip_reason: - _check(p.glob("*B/*"), ['dirB/fileB']) - else: + if can_symlink(): _check(p.glob("*B/*"), ['dirB/fileB', 'dirB/linkD', 'linkB/fileB', 'linkB/linkD']) - if symlink_skip_reason: + else: + _check(p.glob("*B/*"), ['dirB/fileB']) + if can_symlink(): + _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) + else: _check(p.glob("*/fileB"), ['dirB/fileB']) - else: - _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) def test_rglob_common(self): def _check(glob, expected): @@ -1245,7 +1228,7 @@ q = p.resolve() self.assertEqual(q, expected) - @with_symlinks + @skip_unless_symlink def test_resolve_common(self): P = self.cls p = P(BASE, 'foo') @@ -1307,7 +1290,7 @@ self.addCleanup(p.chmod, st.st_mode) self.assertNotEqual(p.stat(), st) - @with_symlinks + @skip_unless_symlink def test_lstat(self): p = self.cls(BASE)/ 'linkA' st = p.stat() @@ -1442,7 +1425,7 @@ self.assertEqual(cm.exception.errno, errno.EEXIST) # XXX test `mode` arg - @with_symlinks + @skip_unless_symlink def test_symlink_to(self): P = self.cls(BASE) target = P / 'fileA' @@ -1471,7 +1454,7 @@ self.assertTrue((P / 'dirA').is_dir()) self.assertFalse((P / 'fileA').is_dir()) self.assertFalse((P / 'non-existing').is_dir()) - if not symlink_skip_reason: + if can_symlink(): self.assertFalse((P / 'linkA').is_dir()) self.assertTrue((P / 'linkB').is_dir()) self.assertFalse((P/ 'brokenLink').is_dir()) @@ -1481,7 +1464,7 @@ self.assertTrue((P / 'fileA').is_file()) self.assertFalse((P / 'dirA').is_file()) self.assertFalse((P / 'non-existing').is_file()) - if not symlink_skip_reason: + if can_symlink(): self.assertTrue((P / 'linkA').is_file()) self.assertFalse((P / 'linkB').is_file()) self.assertFalse((P/ 'brokenLink').is_file()) @@ -1491,7 +1474,7 @@ self.assertFalse((P / 'fileA').is_symlink()) self.assertFalse((P / 'dirA').is_symlink()) self.assertFalse((P / 'non-existing').is_symlink()) - if not symlink_skip_reason: + if can_symlink(): self.assertTrue((P / 'linkA').is_symlink()) self.assertTrue((P / 'linkB').is_symlink()) self.assertTrue((P/ 'brokenLink').is_symlink()) @@ -1621,7 +1604,7 @@ st = os.stat(join('masked_new_file')) self.assertEqual(stat.S_IMODE(st.st_mode), 0o750) - @with_symlinks + @skip_unless_symlink def test_resolve_loop(self): # Loop detection for broken symlinks under POSIX P = self.cls @@ -1644,7 +1627,7 @@ P = self.cls p = P(BASE) given = set(p.glob("FILEa")) - expect = set() if not support.fs_is_case_insensitive(BASE) else given + expect = set() if not fs_is_case_insensitive(BASE) else given self.assertEqual(given, expect) self.assertEqual(set(p.glob("FILEa*")), set()) @@ -1652,7 +1635,7 @@ P = self.cls p = P(BASE, "dirC") given = set(p.rglob("FILEd")) - expect = set() if not support.fs_is_case_insensitive(BASE) else given + expect = set() if not fs_is_case_insensitive(BASE) else given self.assertEqual(given, expect) self.assertEqual(set(p.rglob("FILEd*")), set())