summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorМаксим <maksvenberv@yandex.ru>2020-10-21 02:08:19 (GMT)
committerGitHub <noreply@github.com>2020-10-21 02:08:19 (GMT)
commit5f227413400c4dfdba210cc0f8c9305421638bc1 (patch)
tree38e93bae801217c8797740e01c68ed3811665eb9 /Lib/test
parent25492a5b59c5b74328278f195540e318ab87674f (diff)
downloadcpython-5f227413400c4dfdba210cc0f8c9305421638bc1.zip
cpython-5f227413400c4dfdba210cc0f8c9305421638bc1.tar.gz
cpython-5f227413400c4dfdba210cc0f8c9305421638bc1.tar.bz2
bpo-23706: Add newline parameter to pathlib.Path.write_text (GH-22420) (GH-22420)
* Add _newline_ parameter to `pathlib.Path.write_text()` * Update documentation of `pathlib.Path.write_text()` * Add test case for `pathlib.Path.write_text()` calls with _newline_ parameter passed Automerge-Triggered-By: GH:methane
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pathlib.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 2cb6738..17292dc 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1510,6 +1510,26 @@ class _BasePathTest(object):
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(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_iterdir(self):
P = self.cls
p = P(BASE)