diff options
author | Aleš Matěj <amatej@redhat.com> | 2021-02-24 10:43:28 +0300 |
---|---|---|
committer | Neal Gompa (ニール・ゴンパ) <ngompa13@gmail.com> | 2021-03-30 16:11:27 +0300 |
commit | 3110e7b577c8a9db7bcdf53bc674fa1fc1efbfeb (patch) | |
tree | 94fd706f5cc5d37196027334ae599916bc17b43b | |
parent | 2553174b14416b273219fe01239722bf5985770f (diff) |
Improve resource management for python tests (cleans up tests output)
-rw-r--r-- | tests/python/tests/test_crfile.py | 25 | ||||
-rw-r--r-- | tests/python/tests/test_misc.py | 3 | ||||
-rw-r--r-- | tests/python/tests/test_repomdrecord.py | 3 | ||||
-rw-r--r-- | tests/python/tests/test_xml_file.py | 50 | ||||
-rw-r--r-- | tests/python/tests/test_xml_parser.py | 24 |
5 files changed, 58 insertions, 47 deletions
diff --git a/tests/python/tests/test_crfile.py b/tests/python/tests/test_crfile.py index 09db365..ee92516 100644 --- a/tests/python/tests/test_crfile.py +++ b/tests/python/tests/test_crfile.py @@ -65,8 +65,8 @@ class TestCaseCrFile(unittest.TestCase): f.write("foobar") f.close() - content = open(path).read() - self.assertEqual(content, "foobar") + with open(path) as foo: + self.assertEqual(foo.read(), "foobar") def test_crfile_gz_compression(self): path = os.path.join(self.tmpdir, "foo.gz") @@ -77,8 +77,8 @@ class TestCaseCrFile(unittest.TestCase): f.close() import gzip - content = gzip.open(path).read().decode('utf-8') - self.assertEqual(content, "foobar") + with gzip.open(path) as foo_gz: + self.assertEqual(foo_gz.read().decode('utf-8'), "foobar") def test_crfile_bz2_compression(self): path = os.path.join(self.tmpdir, "foo.bz2") @@ -89,8 +89,9 @@ class TestCaseCrFile(unittest.TestCase): f.close() import bz2 - content = bz2.decompress(open(path, 'rb').read()).decode('utf-8') - self.assertEqual(content, "foobar") + with open(path, 'rb') as foo_bz2: + content = bz2.decompress(foo_bz2.read()).decode('utf-8') + self.assertEqual(content, "foobar") def test_crfile_xz_compression(self): path = os.path.join(self.tmpdir, "foo.xz") @@ -101,9 +102,9 @@ class TestCaseCrFile(unittest.TestCase): f.close() import subprocess - p = subprocess.Popen(["unxz", "--stdout", path], stdout=subprocess.PIPE) - content = p.stdout.read().decode('utf-8') - self.assertEqual(content, "foobar") + with subprocess.Popen(["unxz", "--stdout", path], stdout=subprocess.PIPE) as p: + content = p.stdout.read().decode('utf-8') + self.assertEqual(content, "foobar") def test_crfile_zck_compression(self): if cr.HAS_ZCK == 0: @@ -117,6 +118,6 @@ class TestCaseCrFile(unittest.TestCase): f.close() import subprocess - p = subprocess.Popen(["unzck", "--stdout", path], stdout=subprocess.PIPE) - content = p.stdout.read().decode('utf-8') - self.assertEqual(content, "foobar") + with subprocess.Popen(["unzck", "--stdout", path], stdout=subprocess.PIPE) as p: + content = p.stdout.read().decode('utf-8') + self.assertEqual(content, "foobar") diff --git a/tests/python/tests/test_misc.py b/tests/python/tests/test_misc.py index 22388aa..3dd753a 100644 --- a/tests/python/tests/test_misc.py +++ b/tests/python/tests/test_misc.py @@ -13,7 +13,8 @@ class TestCaseMisc(unittest.TestCase): self.nofile = os.path.join(self.tmpdir, "this_file_should_not_exists") self.tmpfile = os.path.join(self.tmpdir, "file") self.content = "some\nfoo\ncontent\n" - open(self.tmpfile, "w").write(self.content) + with open(self.tmpfile, "w") as tmp_file: + tmp_file.write(self.content) def tearDown(self): shutil.rmtree(self.tmpdir) diff --git a/tests/python/tests/test_repomdrecord.py b/tests/python/tests/test_repomdrecord.py index f92c2e2..58f21cc 100644 --- a/tests/python/tests/test_repomdrecord.py +++ b/tests/python/tests/test_repomdrecord.py @@ -162,7 +162,8 @@ class TestCaseRepomdRecord(unittest.TestCase): self.assertEqual(rec.db_ver, 11) def test_repomdrecord_compress_and_fill(self): - open(self.path01, "w").write("foobar\ncontent\nhh\n") + with open(self.path01, "w") as path01_file: + path01_file.write("foobar\ncontent\nhh\n") self.assertTrue(os.path.exists(self.path01)) rec = cr.RepomdRecord("primary", self.path01) diff --git a/tests/python/tests/test_xml_file.py b/tests/python/tests/test_xml_file.py index 8aa6d5c..fad8764 100644 --- a/tests/python/tests/test_xml_file.py +++ b/tests/python/tests/test_xml_file.py @@ -77,7 +77,8 @@ class TestCaseXmlFile(unittest.TestCase): "foobar/foo/xxx/cvydmaticxuiowe") # Already existing file - open(path, "w").write("foobar") + with open(path, "w") as foofile: + foofile.write("foobar") self.assertRaises(IOError, cr.PrimaryXmlFile, path) def test_xmlfile_no_compression(self): @@ -87,8 +88,8 @@ class TestCaseXmlFile(unittest.TestCase): self.assertTrue(os.path.isfile(path)) f.close() - content = open(path).read() - self.assertEqual(content, + with open(path) as primary: + self.assertEqual(primary.read(), """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> </metadata>""") @@ -101,8 +102,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() import gzip - content = gzip.open(path).read().decode('utf-8') - self.assertEqual(content, + with gzip.open(path) as primary: + self.assertEqual(primary.read().decode('utf-8'), """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> </metadata>""") @@ -115,8 +116,9 @@ class TestCaseXmlFile(unittest.TestCase): f.close() import bz2 - content = bz2.decompress(open(path, 'rb').read()).decode('utf-8') - self.assertEqual(content, + with open(path, 'rb') as primary_bz2: + content = bz2.decompress(primary_bz2.read()).decode('utf-8') + self.assertEqual(content, """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> </metadata>""") @@ -129,9 +131,9 @@ class TestCaseXmlFile(unittest.TestCase): f.close() import subprocess - p = subprocess.Popen(["unxz", "--stdout", path], stdout=subprocess.PIPE) - content = p.stdout.read().decode('utf-8') - self.assertEqual(content, + with subprocess.Popen(["unxz", "--stdout", path], stdout=subprocess.PIPE) as p: + content = p.stdout.read().decode('utf-8') + self.assertEqual(content, """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> </metadata>""") @@ -147,9 +149,9 @@ class TestCaseXmlFile(unittest.TestCase): f.close() import subprocess - p = subprocess.Popen(["unzck", "--stdout", path], stdout=subprocess.PIPE) - content = p.stdout.read().decode('utf-8') - self.assertEqual(content, + with subprocess.Popen(["unzck", "--stdout", path], stdout=subprocess.PIPE) as p: + content = p.stdout.read().decode('utf-8') + self.assertEqual(content, """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> </metadata>""") @@ -162,8 +164,8 @@ class TestCaseXmlFile(unittest.TestCase): f.set_num_of_pkgs(22) f.close() - content = open(path).read() - self.assertEqual(content, + with open(path) as primary: + self.assertEqual(primary.read(), """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="22"> </metadata>""") @@ -186,7 +188,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as primary: + self.assertEqual(primary.read(), """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> <package type="rpm"> @@ -257,7 +260,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as filelists: + self.assertEqual(filelists.read(), """<?xml version="1.0" encoding="UTF-8"?> <filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="0"> <package pkgid="4e0b775220c67f0f2c1fd2177e626b9c863a098130224ff09778ede25cea9a9e" name="Archer" arch="x86_64"> @@ -281,7 +285,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as other: + self.assertEqual(other.read(), """<?xml version="1.0" encoding="UTF-8"?> <otherdata xmlns="http://linux.duke.edu/metadata/other" packages="0"> <package pkgid="4e0b775220c67f0f2c1fd2177e626b9c863a098130224ff09778ede25cea9a9e" name="Archer" arch="x86_64"> @@ -308,7 +313,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as primary: + self.assertEqual(primary.read(), """<?xml version="1.0" encoding="UTF-8"?> <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="0"> <chunk>Some XML chunk</chunk> @@ -327,7 +333,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as filelists: + self.assertEqual(filelists.read(), """<?xml version="1.0" encoding="UTF-8"?> <filelists xmlns="http://linux.duke.edu/metadata/filelists" packages="0"> <chunk>Some XML chunk</chunk> @@ -346,7 +353,8 @@ class TestCaseXmlFile(unittest.TestCase): f.close() self.assertTrue(os.path.isfile(path)) - self.assertEqual(open(path).read(), + with open(path) as other: + self.assertEqual(other.read(), """<?xml version="1.0" encoding="UTF-8"?> <otherdata xmlns="http://linux.duke.edu/metadata/other" packages="0"> <chunk>Some XML chunk</chunk> diff --git a/tests/python/tests/test_xml_parser.py b/tests/python/tests/test_xml_parser.py index 8856096..ddc089f 100644 --- a/tests/python/tests/test_xml_parser.py +++ b/tests/python/tests/test_xml_parser.py @@ -101,8 +101,8 @@ class TestCaseXmlParserPrimary(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(PRIMARY_SNIPPET_01).read() - cr.xml_parse_primary_snippet(content, newpkgcb, pkgcb, warningcb, 1) + with open(PRIMARY_SNIPPET_01) as primary_snip: + cr.xml_parse_primary_snippet(primary_snip.read(), newpkgcb, pkgcb, warningcb, 1) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['super_kernel']) @@ -175,8 +175,8 @@ class TestCaseXmlParserPrimary(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(PRIMARY_SNIPPET_02).read() - cr.xml_parse_primary_snippet(content, newpkgcb, pkgcb, warningcb, 1) + with open(PRIMARY_SNIPPET_02) as primary_snip: + cr.xml_parse_primary_snippet(primary_snip.read(), newpkgcb, pkgcb, warningcb, 1) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['fake_bash', 'super_kernel']) @@ -380,8 +380,8 @@ class TestCaseXmlParserFilelists(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(FILELISTS_SNIPPET_01).read() - cr.xml_parse_filelists_snippet(content, newpkgcb, pkgcb, warningcb) + with open(FILELISTS_SNIPPET_01) as filelists_snip: + cr.xml_parse_filelists_snippet(filelists_snip.read(), newpkgcb, pkgcb, warningcb) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['super_kernel']) @@ -468,8 +468,8 @@ class TestCaseXmlParserFilelists(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(FILELISTS_SNIPPET_02).read() - cr.xml_parse_filelists_snippet(content, newpkgcb, pkgcb, warningcb) + with open(FILELISTS_SNIPPET_02) as filelists_snip: + cr.xml_parse_filelists_snippet(filelists_snip.read(), newpkgcb, pkgcb, warningcb) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['fake_bash', 'super_kernel']) @@ -684,8 +684,8 @@ class TestCaseXmlParserOther(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(OTHER_SNIPPET_01).read() - cr.xml_parse_other_snippet(content, newpkgcb, pkgcb, warningcb) + with open(OTHER_SNIPPET_01) as other_snip: + cr.xml_parse_other_snippet(other_snip.read(), newpkgcb, pkgcb, warningcb) self.assertEqual(userdata["warnings"], []) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], @@ -750,8 +750,8 @@ class TestCaseXmlParserOther(unittest.TestCase): def warningcb(warn_type, msg): userdata["warnings"].append((warn_type, msg)) - content = open(OTHER_SNIPPET_02).read() - cr.xml_parse_other_snippet(content, newpkgcb, pkgcb, warningcb) + with open(OTHER_SNIPPET_02) as other_snip: + cr.xml_parse_other_snippet(other_snip.read(), newpkgcb, pkgcb, warningcb) self.assertEqual([pkg.name for pkg in userdata["pkgs"]], ['fake_bash', 'super_kernel']) |