Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] Clean up test_posixpath (GH-134315)#134317

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes fromall 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 deletionsLib/test/test_posixpath.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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)

def test_ismount_invalid_paths(self):
self.assertIs(posixpath.ismount('/\udfff'), False)
Expand All@@ -242,7 +235,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 DownExpand Up@@ -576,10 +569,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 DownExpand Up@@ -675,7 +668,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@@ -694,7 +687,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@@ -712,8 +705,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@@ -739,9 +732,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@@ -759,8 +752,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@@ -778,7 +771,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 DownExpand Up@@ -817,6 +810,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@@ -838,6 +832,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 DownExpand Up@@ -963,8 +959,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

[8]ページ先頭

©2009-2025 Movatter.jp