From 29db7f035b74d8e1a4e9044f3c26f2caacc833d1 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 14 Mar 2025 13:16:50 +0300 Subject: [PATCH 1/2] [3.12] gh-131219: Improve tests in `test_lzma.py` by adding more asserts (GH-131220) (cherry picked from commit f6c24a5c89a1f08d583ab7f4d5c6751223f8e280) Co-authored-by: sobolevn --- Lib/test/test_lzma.py | 45 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index db290e139327e0..0aa71d18cab0e2 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -538,13 +538,17 @@ class FileTestCase(unittest.TestCase): def test_init(self): with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: - pass + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "rb") with LZMAFile(BytesIO(), "w") as f: - pass + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") with LZMAFile(BytesIO(), "x") as f: - pass + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") with LZMAFile(BytesIO(), "a") as f: - pass + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") def test_init_with_PathLike_filename(self): filename = FakePath(TESTFN) @@ -567,25 +571,32 @@ def test_init_with_filename(self): def test_init_mode(self): with TempFile(TESTFN): - with LZMAFile(TESTFN, "r"): - pass - with LZMAFile(TESTFN, "rb"): - pass - with LZMAFile(TESTFN, "w"): - pass - with LZMAFile(TESTFN, "wb"): - pass - with LZMAFile(TESTFN, "a"): - pass - with LZMAFile(TESTFN, "ab"): - pass + with LZMAFile(TESTFN, "r") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "rb") + with LZMAFile(TESTFN, "rb") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "rb") + with LZMAFile(TESTFN, "w") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") + with LZMAFile(TESTFN, "wb") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") + with LZMAFile(TESTFN, "a") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") + with LZMAFile(TESTFN, "ab") as f: + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, "wb") def test_init_with_x_mode(self): self.addCleanup(unlink, TESTFN) for mode in ("x", "xb"): unlink(TESTFN) with LZMAFile(TESTFN, mode) as f: - pass + self.assertIsInstance(f, LZMAFile) + self.assertEqual(f.mode, 'wb') with self.assertRaises(FileExistsError): LZMAFile(TESTFN, mode) From 7036a6511f1334e38730f74ce444fec5d7c2fe58 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 14 Mar 2025 14:37:49 +0300 Subject: [PATCH 2/2] Update test_lzma.py --- Lib/test/test_lzma.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index 0aa71d18cab0e2..14029c17539b85 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -539,16 +539,12 @@ class FileTestCase(unittest.TestCase): def test_init(self): with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "rb") with LZMAFile(BytesIO(), "w") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") with LZMAFile(BytesIO(), "x") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") with LZMAFile(BytesIO(), "a") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") def test_init_with_PathLike_filename(self): filename = FakePath(TESTFN) @@ -573,22 +569,16 @@ def test_init_mode(self): with TempFile(TESTFN): with LZMAFile(TESTFN, "r") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "rb") with LZMAFile(TESTFN, "rb") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "rb") with LZMAFile(TESTFN, "w") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") with LZMAFile(TESTFN, "wb") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") with LZMAFile(TESTFN, "a") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") with LZMAFile(TESTFN, "ab") as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, "wb") def test_init_with_x_mode(self): self.addCleanup(unlink, TESTFN) @@ -596,7 +586,6 @@ def test_init_with_x_mode(self): unlink(TESTFN) with LZMAFile(TESTFN, mode) as f: self.assertIsInstance(f, LZMAFile) - self.assertEqual(f.mode, 'wb') with self.assertRaises(FileExistsError): LZMAFile(TESTFN, mode)