|  | 
|  | 1 | +""" | 
|  | 2 | +Tests for pathlib.types._WritablePath | 
|  | 3 | +""" | 
|  | 4 | + | 
|  | 5 | +import io | 
|  | 6 | +import os | 
|  | 7 | +import unittest | 
|  | 8 | + | 
|  | 9 | +from pathlib import Path | 
|  | 10 | +from pathlib.types import _WritablePath | 
|  | 11 | +from pathlib._os import magic_open | 
|  | 12 | + | 
|  | 13 | +from test.test_pathlib.support.local_path import WritableLocalPath, LocalPathGround | 
|  | 14 | +from test.test_pathlib.support.zip_path import WritableZipPath, ZipPathGround | 
|  | 15 | + | 
|  | 16 | + | 
|  | 17 | +class WriteTestBase: | 
|  | 18 | +    def setUp(self): | 
|  | 19 | +        self.root = self.ground.setup() | 
|  | 20 | + | 
|  | 21 | +    def tearDown(self): | 
|  | 22 | +        self.ground.teardown(self.root) | 
|  | 23 | + | 
|  | 24 | +    def test_is_writable(self): | 
|  | 25 | +        self.assertIsInstance(self.root, _WritablePath) | 
|  | 26 | + | 
|  | 27 | +    def test_open_w(self): | 
|  | 28 | +        p = self.root / 'fileA' | 
|  | 29 | +        with magic_open(p, 'w') as f: | 
|  | 30 | +            self.assertIsInstance(f, io.TextIOBase) | 
|  | 31 | +            f.write('this is file A\n') | 
|  | 32 | +        self.assertEqual(self.ground.readtext(p), 'this is file A\n') | 
|  | 33 | + | 
|  | 34 | +    def test_open_wb(self): | 
|  | 35 | +        p = self.root / 'fileA' | 
|  | 36 | +        with magic_open(p, 'wb') as f: | 
|  | 37 | +            #self.assertIsInstance(f, io.BufferedWriter) | 
|  | 38 | +            f.write(b'this is file A\n') | 
|  | 39 | +        self.assertEqual(self.ground.readbytes(p), b'this is file A\n') | 
|  | 40 | + | 
|  | 41 | +    def test_write_bytes(self): | 
|  | 42 | +        p = self.root / 'fileA' | 
|  | 43 | +        p.write_bytes(b'abcdefg') | 
|  | 44 | +        self.assertEqual(self.ground.readbytes(p), b'abcdefg') | 
|  | 45 | +        # Check that trying to write str does not truncate the file. | 
|  | 46 | +        self.assertRaises(TypeError, p.write_bytes, 'somestr') | 
|  | 47 | +        self.assertEqual(self.ground.readbytes(p), b'abcdefg') | 
|  | 48 | + | 
|  | 49 | +    def test_write_text(self): | 
|  | 50 | +        p = self.root / 'fileA' | 
|  | 51 | +        p.write_text('äbcdefg', encoding='latin-1') | 
|  | 52 | +        self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') | 
|  | 53 | +        # Check that trying to write bytes does not truncate the file. | 
|  | 54 | +        self.assertRaises(TypeError, p.write_text, b'somebytes') | 
|  | 55 | +        self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') | 
|  | 56 | + | 
|  | 57 | +    def test_write_text_with_newlines(self): | 
|  | 58 | +        # Check that `\n` character change nothing | 
|  | 59 | +        p = self.root / 'fileA' | 
|  | 60 | +        p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\n') | 
|  | 61 | +        self.assertEqual(self.ground.readbytes(p), b'abcde\r\nfghlk\n\rmnopq') | 
|  | 62 | + | 
|  | 63 | +        # Check that `\r` character replaces `\n` | 
|  | 64 | +        p = self.root / 'fileB' | 
|  | 65 | +        p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r') | 
|  | 66 | +        self.assertEqual(self.ground.readbytes(p), b'abcde\r\rfghlk\r\rmnopq') | 
|  | 67 | + | 
|  | 68 | +        # Check that `\r\n` character replaces `\n` | 
|  | 69 | +        p = self.root / 'fileC' | 
|  | 70 | +        p.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n') | 
|  | 71 | +        self.assertEqual(self.ground.readbytes(p), b'abcde\r\r\nfghlk\r\n\rmnopq') | 
|  | 72 | + | 
|  | 73 | +        # Check that no argument passed will change `\n` to `os.linesep` | 
|  | 74 | +        os_linesep_byte = bytes(os.linesep, encoding='ascii') | 
|  | 75 | +        p = self.root / 'fileD' | 
|  | 76 | +        p.write_text('abcde\nfghlk\n\rmnopq') | 
|  | 77 | +        self.assertEqual(self.ground.readbytes(p), | 
|  | 78 | +                         b'abcde' + os_linesep_byte + | 
|  | 79 | +                         b'fghlk' + os_linesep_byte + b'\rmnopq') | 
|  | 80 | + | 
|  | 81 | +    def test_mkdir(self): | 
|  | 82 | +        p = self.root / 'newdirA' | 
|  | 83 | +        self.assertFalse(self.ground.isdir(p)) | 
|  | 84 | +        p.mkdir() | 
|  | 85 | +        self.assertTrue(self.ground.isdir(p)) | 
|  | 86 | + | 
|  | 87 | +    def test_symlink_to(self): | 
|  | 88 | +        if not self.ground.can_symlink: | 
|  | 89 | +            self.skipTest('needs symlinks') | 
|  | 90 | +        link = self.root.joinpath('linkA') | 
|  | 91 | +        link.symlink_to('fileA') | 
|  | 92 | +        self.assertTrue(self.ground.islink(link)) | 
|  | 93 | +        self.assertEqual(self.ground.readlink(link), 'fileA') | 
|  | 94 | + | 
|  | 95 | + | 
|  | 96 | +class ZipPathWriteTest(WriteTestBase, unittest.TestCase): | 
|  | 97 | +    ground = ZipPathGround(WritableZipPath) | 
|  | 98 | + | 
|  | 99 | + | 
|  | 100 | +class LocalPathWriteTest(WriteTestBase, unittest.TestCase): | 
|  | 101 | +    ground = LocalPathGround(WritableLocalPath) | 
|  | 102 | + | 
|  | 103 | + | 
|  | 104 | +class PathWriteTest(WriteTestBase, unittest.TestCase): | 
|  | 105 | +    ground = LocalPathGround(Path) | 
|  | 106 | + | 
|  | 107 | + | 
|  | 108 | +if __name__ == "__main__": | 
|  | 109 | +    unittest.main() | 
0 commit comments