8000 [3.14] Clean up test_posixpath (GH-134315) by miss-islington · Pull Request #134316 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.14] Clean up test_posixpath (GH-134315) #134316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 41 additions & 45 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from test import test_genericpath
from test.support import import_helper
from test.support import os_helper
from test.support.os_helper import FakePath
from test.support.os_helper import FakePath, TESTFN
from unittest import mock

try:
Expand All @@ -21,7 +21,7 @@
# An absolute path to a temporary filename for testing. We can't rely on TESTFN
# being an absolute path, so we need this.

ABSTFN = abspath(os_helper.TESTFN)
ABSTFN = abspath(TESTFN)

def skip_if_ABSTFN_contains_backslash(test):
"""
Expand All @@ -33,21 +33,11 @@ def skip_if_ABSTFN_contains_backslash(test):
msg = "ABSTFN is not a posix path - tests fail"
return [test, unittest.skip(msg)(test)][found_backslash]

def safe_rmdir(dirname):
try:
os.rmdir(dirname)
except OSError:
pass

class PosixPathTest(unittest.TestCase):

def setUp(self):
self.tearDown()

def tearDown(self):
for suffix in ["", "1", "2"]:
os_helper.unlink(os_helper.TESTFN + suffix)
safe_rmdir(os_helper.TESTFN + suffix)
self.assertFalse(posixpath.lexists(ABSTFN + suffix))

def test_join(self):
fn = posixpath.join
Expand Down Expand Up @@ -194,25 +184,28 @@ def test_dirname(self):
self.assertEqual(posixpath.dirname(b"//foo//bar"), b"//foo")

def test_islink(self):
self.assertIs(posixpath.islink(os_helper.TESTFN + "1"), False)
self.assertIs(posixpath.lexists(os_helper.TESTFN + "2"), False)
self.assertIs(posixpath.islink(TESTFN + "1"), False)
self.assertIs(posixpath.lexists(TESTFN + "2"), False)

with open(os_helper.TESTFN + "1", "wb") as f:
self.addCleanup(os_helper.unlink, TESTFN + "1")
with open(TESTFN + "1", "wb") as f:
f.write(b"foo")
self.assertIs(posixpath.islink(os_helper.TESTFN + "1"), False)
self.assertIs(posixpath.islink(TESTFN + "1"), False)

if os_helper.can_symlink():
os.symlink(os_helper.TESTFN + "1", os_helper.TESTFN + "2")
self.assertIs(posixpath.islink(os_helper.TESTFN + "2"), True)
os.remove(os_helper.TESTFN + "1")
self.assertIs(posixpath.islink(os_helper.TESTFN + "2"), True)
self.assertIs(posixpath.exists(os_helper.TESTFN + "2"), False)
self.assertIs(posixpath.lexists(os_helper.TESTFN + "2"), True)

self.assertIs(posixpath.islink(os_helper.TESTFN + "\udfff"), False)
self.assertIs(posixpath.islink(os.fsencode(os_helper.TESTFN) + b"\xff"), False)
self.assertIs(posixpath.islink(os_helper.TESTFN + "\x00"), False)
self.assertIs(posixpath.islink(os.fsencode(os_helper.TESTFN) + b"\x00"), False)
self.addCleanup(os_helper.unlink, TESTFN + "2")
os.symlink(TESTFN + "1", TESTFN + "2")
self.assertIs(posixpath.islink(TESTFN + "2"), True)
os.remove(TESTFN + "1")
self.assertIs(posixpath.islink(TESTFN + "2"), True)
self.assertIs(posixpath.exists(TESTFN + "2"), False)
self.assertIs(posixpath.lexists(TESTFN + "2"), True)

def test_islink_invalid_paths(self):
self.assertIs(posixpath.islink(TESTFN + "\udfff"), False)
self.assertIs(posixpath.islink(os.fsencode(TESTFN) + b"\xff"), False)
self.assertIs(posixpath.islink(TESTFN + "\x00"), False)
self.assertIs(posixpath.islink(os.fsencode(TESTFN) + b"\x00"), False)

def test_ismount(self):
self.assertIs(posixpath.ismount("/"), True)
Expand All @@ -227,7 +220,7 @@ def test_ismount_non_existent(self):
os.mkdir(ABSTFN)
self.assertIs(posixpath.ismount(ABSTFN), False)
finally:
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN)

self.assertIs(posixpath.ismount('/\udfff'), False)
self.assertIs(posixpath.ismount(b'/\xff'), False)
Expand All @@ -241,7 +234,7 @@ def test_ismount_symlinks(self):
os.symlink("/", ABSTFN)
self.assertIs(posixpath.ismount(ABSTFN), False)
finally:
os.unlink(ABSTFN)
os_helper.unlink(ABSTFN)

@unittest.skipIf(posix is None, "Test requires posix module")
def test_ismount_different_device(self):
Expand Down Expand Up @@ -502,10 +495,10 @@ def test_realpath_relative(self):
@skip_if_ABSTFN_contains_backslash
def test_realpath_missing_pardir(self):
try:
os.symlink(os_helper.TESTFN + "1", os_helper.TESTFN)
self.assertEqual(realpath("nonexistent/../" + os_helper.TESTFN), ABSTFN + "1")
os.symlink(TESTFN + "1", TESTFN)
self.assertEqual(realpath("nonexistent/../" + TESTFN), ABSTFN + "1")
finally:
os_helper.unlink(os_helper.TESTFN)
os_helper.unlink(TESTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand Down Expand Up @@ -601,7 +594,7 @@ def test_realpath_repeated_indirect_symlinks(self):
finally:
os_helper.unlink(ABSTFN + '/self')
os_helper.unlink(ABSTFN + '/link')
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -620,7 +613,7 @@ def test_realpath_deep_recursion(self):
finally:
for i in range(depth + 1):
os_helper.unlink(ABSTFN + '/%d' % i)
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -638,8 +631,8 @@ def test_realpath_resolve_parents(self):
self.assertEqual(realpath("a"), ABSTFN + "/y/a")
finally:
os_helper.unlink(ABSTFN + "/k")
safe_rmdir(ABSTFN + "/y")
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN + "/y")
os_helper.rmdir(ABSTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -665,9 +658,9 @@ def test_realpath_resolve_before_normalizing(self):
ABSTFN + "/k")
finally:
os_helper.unlink(ABSTFN + "/link-y")
safe_rmdir(ABSTFN + "/k/y")
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN + "/k/y")
os_helper.rmdir(ABSTFN + "/k")
os_helper.rmdir(ABSTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -685,8 +678,8 @@ def test_realpath_resolve_first(self):
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
finally:
os_helper.unlink(ABSTFN + "link")
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
os_helper.rmdir(ABSTFN + "/k")
os_helper.rmdir(ABSTFN)

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -704,7 +697,7 @@ def test_realpath_unreadable_symlink(self):
realpath(ABSTFN, strict=True)
finally:
os.chmod(ABSTFN, 0o755, follow_symlinks=False)
os.unlink(ABSTFN)
os_helper.unlink(ABSTFN)

@skip_if_ABSTFN_contains_backslash
def test_realpath_nonterminal_file(self):
Expand Down Expand Up @@ -743,6 +736,7 @@ def test_realpath_nonterminal_symlink_to_file(self):
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
finally:
os_helper.unlink(ABSTFN)
os_helper.unlink(ABSTFN + "1")

@os_helper.skip_unless_symlink
@skip_if_ABSTFN_contains_backslash
Expand All @@ -764,6 +758,8 @@ def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
finally:
os_helper.unlink(ABSTFN)
os_helper.unlink(ABSTFN + "1")
os_helper.unlink(ABSTFN + "2")

def test_relpath(self):
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
Expand Down Expand Up @@ -889,8 +885,8 @@ class PathLikeTests(unittest.TestCase):
path = posixpath

def setUp(self):
self.file_name = os_helper.TESTFN
self.file_path = FakePath(os_helper.TESTFN)
self.file_name = TESTFN
self.file_path = FakePath(TESTFN)
self.addCleanup(os_helper.unlink, self.file_name)
with open(self.file_name, 'xb', 0) as file:
file.write(b"test_posixpath.PathLikeTests")
Expand Down
Loading
0