GH-128520: pathlib ABCs tests: use explicit text encoding (#133105)

Follow-up to fbffd70. Set `encoding='utf-8'` when reading and writing text
in the tests for the private pathlib ABCs, which allows the tests to run
with `-W error -X warn_default_encoding`
This commit is contained in:
Barney Gale 2025-04-28 20:18:56 +01:00 committed by GitHub
parent 606003ffa9
commit 336322b341
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 13 deletions

View file

@ -32,7 +32,7 @@ class ReadTestBase:
def test_open_r(self):
p = self.root / 'fileA'
with magic_open(p, 'r') as f:
with magic_open(p, 'r', encoding='utf-8') as f:
self.assertIsInstance(f, io.TextIOBase)
self.assertEqual(f.read(), 'this is file A\n')
@ -61,7 +61,7 @@ class ReadTestBase:
def test_read_text(self):
p = self.root / 'fileA'
self.assertEqual(p.read_text(), 'this is file A\n')
self.assertEqual(p.read_text(encoding='utf-8'), 'this is file A\n')
q = self.root / 'abc'
self.ground.create_file(q, b'\xe4bcdefg')
self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg')
@ -81,11 +81,11 @@ class ReadTestBase:
p = self.root / 'abc'
self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq')
# Check that `\n` character change nothing
self.assertEqual(p.read_text(newline='\n'), 'abcde\r\nfghlk\n\rmnopq')
self.assertEqual(p.read_text(encoding='utf-8', newline='\n'), 'abcde\r\nfghlk\n\rmnopq')
# Check that `\r` character replaces `\n`
self.assertEqual(p.read_text(newline='\r'), 'abcde\r\nfghlk\n\rmnopq')
self.assertEqual(p.read_text(encoding='utf-8', newline='\r'), 'abcde\r\nfghlk\n\rmnopq')
# Check that `\r\n` character replaces `\n`
self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq')
self.assertEqual(p.read_text(encoding='utf-8', newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq')
def test_iterdir(self):
expected = ['dirA', 'dirB', 'dirC', 'fileA']