| 1 |
""" |
| 2 |
Tests for pathlib.types._WritablePath |
| 3 |
""" |
| 4 |
|
| 5 |
import io |
| 6 |
import os |
| 7 |
import sys |
| 8 |
import unittest |
| 9 |
|
| 10 |
from .support import is_pypi |
| 11 |
from .support.local_path import WritableLocalPath, LocalPathGround |
| 12 |
from .support.zip_path import WritableZipPath, ZipPathGround |
| 13 |
|
| 14 |
if is_pypi: |
| 15 |
from pathlib_abc import _WritablePath |
| 16 |
from pathlib_abc._os import magic_open |
| 17 |
else: |
| 18 |
from pathlib.types import _WritablePath |
| 19 |
from pathlib._os import magic_open |
| 20 |
|
| 21 |
|
| 22 |
class WriteTestBase: |
| 23 |
def setUp(self): |
| 24 |
self.root = self.ground.setup() |
| 25 |
|
| 26 |
def tearDown(self): |
| 27 |
self.ground.teardown(self.root) |
| 28 |
|
| 29 |
def test_is_writable(self): |
| 30 |
self.assertIsInstance(self.root, _WritablePath) |
| 31 |
|
| 32 |
def test_open_w(self): |
| 33 |
p = self.root / 'fileA' |
| 34 |
with magic_open(p, 'w', encoding='utf-8') as f: |
| 35 |
self.assertIsInstance(f, io.TextIOBase) |
| 36 |
f.write('this is file A\n') |
| 37 |
self.assertEqual(self.ground.readtext(p), 'this is file A\n') |
| 38 |
|
| 39 |
@unittest.skipIf( |
| 40 |
not getattr(sys.flags, 'warn_default_encoding', 0), |
| 41 |
"Requires warn_default_encoding", |
| 42 |
) |
| 43 |
def test_open_w_encoding_warning(self): |
| 44 |
p = self.root / 'fileA' |
| 45 |
with self.assertWarns(EncodingWarning) as wc: |
| 46 |
with magic_open(p, 'w'): |
| 47 |
pass |
| 48 |
self.assertEqual(wc.filename, __file__) |
| 49 |
|
| 50 |
def test_open_wb(self): |
| 51 |
p = self.root / 'fileA' |
| 52 |
with magic_open(p, 'wb') as f: |
| 53 |
#self.assertIsInstance(f, io.BufferedWriter) |
| 54 |
f.write(b'this is file A\n') |
| 55 |
self.assertEqual(self.ground.readbytes(p), b'this is file A\n') |
| 56 |
self.assertRaises(ValueError, magic_open, p, 'wb', encoding='utf8') |
| 57 |
self.assertRaises(ValueError, magic_open, p, 'wb', errors='strict') |
| 58 |
self.assertRaises(ValueError, magic_open, p, 'wb', newline='') |
| 59 |
|
| 60 |
def test_write_bytes(self): |
| 61 |
p = self.root / 'fileA' |
| 62 |
data = b'abcdefg' |
| 63 |
self.assertEqual(len(data), p.write_bytes(data)) |
| 64 |
self.assertEqual(self.ground.readbytes(p), data) |
| 65 |
# Check that trying to write str does not truncate the file. |
| 66 |
self.assertRaises(TypeError, p.write_bytes, 'somestr') |
| 67 |
self.assertEqual(self.ground.readbytes(p), data) |
| 68 |
|
| 69 |
def test_write_text(self): |
| 70 |
p = self.root / 'fileA' |
| 71 |
data = 'äbcdefg' |
| 72 |
self.assertEqual(len(data), p.write_text(data, encoding='latin-1')) |
| 73 |
self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') |
| 74 |
# Check that trying to write bytes does not truncate the file. |
| 75 |
self.assertRaises(TypeError, p.write_text, b'somebytes', encoding='utf-8') |
| 76 |
self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') |
| 77 |
|
| 78 |
@unittest.skipIf( |
| 79 |
not getattr(sys.flags, 'warn_default_encoding', 0), |
| 80 |
"Requires warn_default_encoding", |
| 81 |
) |
| 82 |
def test_write_text_encoding_warning(self): |
| 83 |
p = self.root / 'fileA' |
| 84 |
with self.assertWarns(EncodingWarning) as wc: |
| 85 |
p.write_text('abcdefg') |
| 86 |
self.assertEqual(wc.filename, __file__) |
| 87 |
|
| 88 |
def test_write_text_with_newlines(self): |
| 89 |
# Check that `\n` character change nothing |
| 90 |
p = self.root / 'fileA' |
| 91 |
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\n') |
| 92 |
self.assertEqual(self.ground.readbytes(p), b'abcde\r\nfghlk\n\rmnopq') |
| 93 |
|
| 94 |
# Check that `\r` character replaces `\n` |
| 95 |
p = self.root / 'fileB' |
| 96 |
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r') |
| 97 |
self.assertEqual(self.ground.readbytes(p), b'abcde\r\rfghlk\r\rmnopq') |
| 98 |
|
| 99 |
# Check that `\r\n` character replaces `\n` |
| 100 |
p = self.root / 'fileC' |
| 101 |
p.write_text('abcde\r\nfghlk\n\rmnopq', encoding='utf-8', newline='\r\n') |
| 102 |
self.assertEqual(self.ground.readbytes(p), b'abcde\r\r\nfghlk\r\n\rmnopq') |
| 103 |
|
| 104 |
# Check that no argument passed will change `\n` to `os.linesep` |
| 105 |
os_linesep_byte = bytes(os.linesep, encoding='ascii') |
| 106 |
p = self.root / 'fileD' |
| 107 |
p.write_text('abcde\nfghlk\n\rmnopq', encoding='utf-8') |
| 108 |
self.assertEqual(self.ground.readbytes(p), |
| 109 |
b'abcde' + os_linesep_byte + |
| 110 |
b'fghlk' + os_linesep_byte + b'\rmnopq') |
| 111 |
|
| 112 |
def test_mkdir(self): |
| 113 |
p = self.root / 'newdirA' |
| 114 |
self.assertFalse(self.ground.isdir(p)) |
| 115 |
p.mkdir() |
| 116 |
self.assertTrue(self.ground.isdir(p)) |
| 117 |
|
| 118 |
def test_symlink_to(self): |
| 119 |
if not self.ground.can_symlink: |
| 120 |
self.skipTest('needs symlinks') |
| 121 |
link = self.root.joinpath('linkA') |
| 122 |
link.symlink_to('fileA') |
| 123 |
self.assertTrue(self.ground.islink(link)) |
| 124 |
self.assertEqual(self.ground.readlink(link), 'fileA') |
| 125 |
|
| 126 |
|
| 127 |
class ZipPathWriteTest(WriteTestBase, unittest.TestCase): |
| 128 |
ground = ZipPathGround(WritableZipPath) |
| 129 |
|
| 130 |
|
| 131 |
class LocalPathWriteTest(WriteTestBase, unittest.TestCase): |
| 132 |
ground = LocalPathGround(WritableLocalPath) |
| 133 |
|
| 134 |
|
| 135 |
if not is_pypi: |
| 136 |
from pathlib import Path |
| 137 |
|
| 138 |
class PathWriteTest(WriteTestBase, unittest.TestCase): |
| 139 |
ground = LocalPathGround(Path) |
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__": |
| 143 |
unittest.main() |