GH-130614: pathlib ABCs: revise test suite for writable paths (#131112)

Test `pathlib.types._WritablePath` in a dedicated test module. These tests
cover `WritableZipPath`, `WritableLocalPath` and `Path`, where the former
two classes are implementations of `_WritablePath` for use in tests.
This commit is contained in:
Barney Gale 2025-03-12 19:06:43 +00:00 committed by GitHub
parent ea57ffa02e
commit db6a998b18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 178 additions and 43 deletions

View file

@ -336,10 +336,6 @@ def test_glob_windows(self):
class WritablePathTest(JoinablePathTest):
cls = DummyWritablePath
def test_is_writable(self):
p = self.cls(self.base)
self.assertIsInstance(p, _WritablePath)
class DummyRWPath(DummyWritablePath, DummyReadablePath):
__slots__ = ()
@ -349,43 +345,6 @@ class RWPathTest(WritablePathTest, ReadablePathTest):
cls = DummyRWPath
can_symlink = False
def test_read_write_bytes(self):
p = self.cls(self.base)
(p / 'fileA').write_bytes(b'abcdefg')
self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
# Check that trying to write str does not truncate the file.
self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr')
self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg')
def test_read_write_text(self):
p = self.cls(self.base)
(p / 'fileA').write_text('äbcdefg', encoding='latin-1')
self.assertEqual((p / 'fileA').read_text(
encoding='utf-8', errors='ignore'), 'bcdefg')
# Check that trying to write bytes does not truncate the file.
self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
def test_write_text_with_newlines(self):
p = self.cls(self.base)
# Check that `\n` character change nothing
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
self.assertEqual((p / 'fileA').read_bytes(),
b'abcde\r\nfghlk\n\rmnopq')
# Check that `\r` character replaces `\n`
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
self.assertEqual((p / 'fileA').read_bytes(),
b'abcde\r\rfghlk\r\rmnopq')
# Check that `\r\n` character replaces `\n`
(p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
self.assertEqual((p / 'fileA').read_bytes(),
b'abcde\r\r\nfghlk\r\n\rmnopq')
# Check that no argument passed will change `\n` to `os.linesep`
os_linesep_byte = bytes(os.linesep, encoding='ascii')
(p / 'fileA').write_text('abcde\nfghlk\n\rmnopq')
self.assertEqual((p / 'fileA').read_bytes(),
b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq')
def test_copy_file(self):
base = self.cls(self.base)
source = base / 'fileA'