Skip to content

Commit 5a48471

Browse files
authored
GH-130614: pathlib ABCs: revise test suite for Posix path joining (#131017)
Test Posix-flavoured `pathlib.types._JoinablePath` in a dedicated test module. These tests cover `LexicalPosixPath`, `PurePosixPath` and `PosixPath`, where `LexicalPosixPath` is a simple implementation of `_JoinablePath` for use in tests.
1 parent 93fc3d3 commit 5a48471

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

Lib/test/test_pathlib/support/lexical_path.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os.path
66
import pathlib.types
7+
import posixpath
78

89

910
class LexicalPath(pathlib.types._JoinablePath):
@@ -31,3 +32,8 @@ def __repr__(self):
3132

3233
def with_segments(self, *pathsegments):
3334
return type(self)(*pathsegments)
35+
36+
37+
class LexicalPosixPath(LexicalPath):
38+
__slots__ = ()
39+
parser = posixpath
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Tests for Posix-flavoured pathlib.types._JoinablePath
3+
"""
4+
5+
import os
6+
import unittest
7+
8+
from pathlib import PurePosixPath, PosixPath
9+
from test.test_pathlib.support.lexical_path import LexicalPosixPath
10+
11+
12+
class JoinTestBase:
13+
def test_join(self):
14+
P = self.cls
15+
p = P('//a')
16+
pp = p.joinpath('b')
17+
self.assertEqual(pp, P('//a/b'))
18+
pp = P('/a').joinpath('//c')
19+
self.assertEqual(pp, P('//c'))
20+
pp = P('//a').joinpath('/c')
21+
self.assertEqual(pp, P('/c'))
22+
23+
def test_div(self):
24+
# Basically the same as joinpath().
25+
P = self.cls
26+
p = P('//a')
27+
pp = p / 'b'
28+
self.assertEqual(pp, P('//a/b'))
29+
pp = P('/a') / '//c'
30+
self.assertEqual(pp, P('//c'))
31+
pp = P('//a') / '/c'
32+
self.assertEqual(pp, P('/c'))
33+
34+
35+
class LexicalPosixPathJoinTest(JoinTestBase, unittest.TestCase):
36+
cls = LexicalPosixPath
37+
38+
39+
class PurePosixPathJoinTest(JoinTestBase, unittest.TestCase):
40+
cls = PurePosixPath
41+
42+
43+
if os.name != 'nt':
44+
class PosixPathJoinTest(JoinTestBase, unittest.TestCase):
45+
cls = PosixPath
46+
47+
48+
if __name__ == "__main__":
49+
unittest.main()

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,6 @@ def test_str_subclass_windows(self):
108108
self._check_str_subclass('\\\\some\\share\\a')
109109
self._check_str_subclass('\\\\some\\share\\a\\b.txt')
110110

111-
@needs_posix
112-
def test_join_posix(self):
113-
P = self.cls
114-
p = P('//a')
115-
pp = p.joinpath('b')
116-
self.assertEqual(pp, P('//a/b'))
117-
pp = P('/a').joinpath('//c')
118-
self.assertEqual(pp, P('//c'))
119-
pp = P('//a').joinpath('/c')
120-
self.assertEqual(pp, P('/c'))
121-
122111
@needs_windows
123112
def test_join_windows(self):
124113
P = self.cls
@@ -157,18 +146,6 @@ def test_join_windows(self):
157146
pp = P('//./BootPartition').joinpath('Windows')
158147
self.assertEqual(pp, P('//./BootPartition/Windows'))
159148

160-
@needs_posix
161-
def test_div_posix(self):
162-
# Basically the same as joinpath().
163-
P = self.cls
164-
p = P('//a')
165-
pp = p / 'b'
166-
self.assertEqual(pp, P('//a/b'))
167-
pp = P('/a') / '//c'
168-
self.assertEqual(pp, P('//c'))
169-
pp = P('//a') / '/c'
170-
self.assertEqual(pp, P('/c'))
171-
172149
@needs_windows
173150
def test_div_windows(self):
174151
# Basically the same as joinpath().

0 commit comments

Comments
 (0)