Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
081cbc2
bpo-41109: separate object creation by Path and PurePath member varia…
websurfer5 Aug 16, 2020
0e4d53e
bpo-41109: Windows PurePath tests
websurfer5 Aug 17, 2020
53643f4
bpo-41109: use correct class in PureWindowsSubclassNewAndInitTest
websurfer5 Aug 17, 2020
c418958
bpo-41109: fix comment
websurfer5 Aug 17, 2020
34683c4
bpo-41109: fix class names and comments
websurfer5 Aug 17, 2020
19e0a40
bpo-41109: WindowsPath test suite
websurfer5 Aug 17, 2020
1853c6d
bpo-41109: add __init__() to Path and PurePath
websurfer5 Aug 17, 2020
17c141c
bpo-41109: remove extraneous **kwargs parameter from Path.__init__()
websurfer5 Aug 17, 2020
505cab0
bpo-41109: remove extraneous whitespace
websurfer5 Aug 17, 2020
c47bf06
bpo-41109: remove double-initialization in Path.__new__()
websurfer5 Aug 18, 2020
6607e77
bpo-41109: no longer need to call _init() from PurePath.__init__()
websurfer5 Aug 18, 2020
cf6afb0
bpo-41109: reorganize tests using a common base class for common tests
websurfer5 Aug 18, 2020
d25fdd7
bpo-41109: fix readlink test for Windows
websurfer5 Aug 18, 2020
4be7b7a
bpo-41109: cleanup test_resolve
websurfer5 Aug 18, 2020
1660fa6
bpo-41109: cleanup test_glob
websurfer5 Aug 18, 2020
2a27016
bpo-41109: test all initialization cases
websurfer5 Aug 18, 2020
4014a16
bpo-41109: news blurb
websurfer5 Aug 19, 2020
31b449e
bpo-41109: What's New entry and better news blurb
websurfer5 Aug 19, 2020
ebc8371
bpo-4119: Merge branch 'master' of github.com:python/cpython into fix…
websurfer5 Aug 19, 2020
86bb25b
bpo-41109: improve What's New and News entries
websurfer5 Aug 19, 2020
b2c62a6
bpo-41109: simplify _new_from_parts() and _new_from_parsed_parts() in…
websurfer5 Aug 19, 2020
6f363b9
Merge branch 'master' of github.com:python/cpython into fix-issue-41109
websurfer5 Aug 23, 2020
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
Prev Previous commit
Next Next commit
bpo-41109: WindowsPath test suite
  • Loading branch information
websurfer5 committed Aug 17, 2020
commit 19e0a405b9be3022b4be40120558f518895ddcf0
95 changes: 95 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2797,5 +2797,100 @@ def test_readlink(self):
self.validate_object(path, join(d, 'fileA'))


@only_nt
class WindowsPathSubclassNewAndInitTest(unittest.TestCase):
"""
Test that the __init__() and __new__() functions of subclasses
of WindowsPath get called when WindowsPath functions
and properties instantiate new objects of the subclass.
"""
cls = pathlib.WindowsPath

class ASubclass(cls):
new_called = False
init_called = False

def __new__(cls, *args, **kwargs):
cls.new_called = True
return super().__new__(cls, *args, **kwargs)

def __init__(self, *args, **kwargs):
self.init_called = True
super().__init__()

def validate_object(self, path, value):
self.assertIs(type(path), self.ASubclass)
self.assertTrue(path.new_called)
self.assertTrue(path.init_called)
self.assertEqual(str(path), value)

def test_class_initialization(self):
self.validate_object(self.ASubclass('c:\\a\\b\\c.foo'), 'c:\\a\\b\\c.foo')

def test_absolute(self):
relpath = 'a\\b\\c.foo'
self.validate_object(self.ASubclass(relpath).absolute(),
join(os.getcwd(), relpath))

def test_expanduser(self):
import_helper.import_module('pwd')
import pwd
pwdent = pwd.getpwuid(os.getuid())
userhome = pwdent.pw_dir.rstrip('\\') or '\\'
path = self.ASubclass('~\\Documents')
with os_helper.EnvironmentVarGuard() as env:
env.pop('HOME', None)
self.validate_object(path.expanduser(), join(userhome, 'Documents'))

def test_glob_rglob_iterdir(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
filename = 'fileA'
with open(join(d, filename), 'wb') as f:
f.write(b"this is file A\n")

# create an Asubclass object for testing
path = self.ASubclass(d)
self.validate_object(path, d)

# verify __new__ and __init__ are called
# in the subclass for each created instance
# and the subclass value is correct
cases = [(path.glob, 'f*', join(d, filename)), # _WildcardSelector
(path.glob, 'fileA', join(d, filename)), # _PreciseSelector
(path.glob, '**', d), # _RecursiveWildcardSelector
(path.rglob, 'f*', join(d, filename)),
(path.iterdir, None, join(d, filename))]

for func, param, value in cases:
n = 0
for name in func() if param is None else func(param):
self.validate_object(name, value)
n += 1
self.assertEqual(n, 1)

def test_resolve(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
filename = 'fileA'
with open(join(d, filename), 'wb') as f:
f.write(b"this is file A\n")

self.validate_object(self.ASubclass(d, filename).resolve(strict=True),
join(d, filename))

@os_helper.skip_unless_symlink
def test_readlink(self):
# create a file in a temp directory
d = tempfile.mkdtemp()
with open(join(d, 'fileA'), 'wb') as f:
f.write(b"this is file A\n")
# create a symlink to the file
os.symlink('fileA', join(d, 'linkA'))

path = self.ASubclass(d, 'linkA').readlink()
self.validate_object(path, 'fileA')


if __name__ == "__main__":
unittest.main()