summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2022-01-28 23:40:55 (GMT)
committerGitHub <noreply@github.com>2022-01-28 23:40:55 (GMT)
commit18cb2ef46c9998480f7182048435bc58265c88f2 (patch)
tree6a57bd91442c285bc4580d7ce66f7457a653cc78 /Lib/test/test_pathlib.py
parent1f036ede59e2c4befc07714cf76603c591d5c972 (diff)
downloadcpython-18cb2ef46c9998480f7182048435bc58265c88f2.zip
cpython-18cb2ef46c9998480f7182048435bc58265c88f2.tar.gz
cpython-18cb2ef46c9998480f7182048435bc58265c88f2.tar.bz2
bpo-29688: document and test `pathlib.Path.absolute()` (GH-26153)
Co-authored-by: Brett Cannon <brett@python.org> Co-authored-by: Brian Helba <brian.helba@kitware.com>
Diffstat (limited to 'Lib/test/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 1bf2112..3fbf1d1 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1456,6 +1456,28 @@ class _BasePathTest(object):
p = self.cls.cwd()
self._test_cwd(p)
+ def test_absolute_common(self):
+ P = self.cls
+
+ with mock.patch("pathlib._normal_accessor.getcwd") as getcwd:
+ getcwd.return_value = BASE
+
+ # Simple relative paths.
+ self.assertEqual(str(P().absolute()), BASE)
+ self.assertEqual(str(P('.').absolute()), BASE)
+ self.assertEqual(str(P('a').absolute()), os.path.join(BASE, 'a'))
+ self.assertEqual(str(P('a', 'b', 'c').absolute()), os.path.join(BASE, 'a', 'b', 'c'))
+
+ # Symlinks should not be resolved.
+ self.assertEqual(str(P('linkB', 'fileB').absolute()), os.path.join(BASE, 'linkB', 'fileB'))
+ self.assertEqual(str(P('brokenLink').absolute()), os.path.join(BASE, 'brokenLink'))
+ self.assertEqual(str(P('brokenLinkLoop').absolute()), os.path.join(BASE, 'brokenLinkLoop'))
+
+ # '..' entries should be preserved and not normalised.
+ self.assertEqual(str(P('..').absolute()), os.path.join(BASE, '..'))
+ self.assertEqual(str(P('a', '..').absolute()), os.path.join(BASE, 'a', '..'))
+ self.assertEqual(str(P('..', 'b').absolute()), os.path.join(BASE, '..', 'b'))
+
def _test_home(self, p):
q = self.cls(os.path.expanduser('~'))
self.assertEqual(p, q)
@@ -2463,6 +2485,17 @@ class PathTest(_BasePathTest, unittest.TestCase):
class PosixPathTest(_BasePathTest, unittest.TestCase):
cls = pathlib.PosixPath
+ def test_absolute(self):
+ P = self.cls
+ self.assertEqual(str(P('/').absolute()), '/')
+ self.assertEqual(str(P('/a').absolute()), '/a')
+ self.assertEqual(str(P('/a/b').absolute()), '/a/b')
+
+ # '//'-prefixed absolute path (supported by POSIX).
+ self.assertEqual(str(P('//').absolute()), '//')
+ self.assertEqual(str(P('//a').absolute()), '//a')
+ self.assertEqual(str(P('//a/b').absolute()), '//a/b')
+
def _check_symlink_loop(self, *args, strict=True):
path = self.cls(*args)
with self.assertRaises(RuntimeError):
@@ -2628,6 +2661,31 @@ class PosixPathTest(_BasePathTest, unittest.TestCase):
class WindowsPathTest(_BasePathTest, unittest.TestCase):
cls = pathlib.WindowsPath
+ def test_absolute(self):
+ P = self.cls
+
+ # Simple absolute paths.
+ self.assertEqual(str(P('c:\\').absolute()), 'c:\\')
+ self.assertEqual(str(P('c:\\a').absolute()), 'c:\\a')
+ self.assertEqual(str(P('c:\\a\\b').absolute()), 'c:\\a\\b')
+
+ # UNC absolute paths.
+ share = '\\\\server\\share\\'
+ self.assertEqual(str(P(share).absolute()), share)
+ self.assertEqual(str(P(share + 'a').absolute()), share + 'a')
+ self.assertEqual(str(P(share + 'a\\b').absolute()), share + 'a\\b')
+
+ # UNC relative paths.
+ with mock.patch("pathlib._normal_accessor.getcwd") as getcwd:
+ getcwd.return_value = share
+
+ self.assertEqual(str(P().absolute()), share)
+ self.assertEqual(str(P('.').absolute()), share)
+ self.assertEqual(str(P('a').absolute()), os.path.join(share, 'a'))
+ self.assertEqual(str(P('a', 'b', 'c').absolute()),
+ os.path.join(share, 'a', 'b', 'c'))
+
+
def test_glob(self):
P = self.cls
p = P(BASE)