diff options
-rw-r--r-- | tests/frontend/buildcheckout.py | 80 | ||||
-rw-r--r-- | tests/internals/storage_vdir_import.py | 12 | ||||
-rw-r--r-- | tests/testutils/filetypegenerator.py | 11 |
3 files changed, 58 insertions, 45 deletions
diff --git a/tests/frontend/buildcheckout.py b/tests/frontend/buildcheckout.py index a64faeb9d..1aa9bbd01 100644 --- a/tests/frontend/buildcheckout.py +++ b/tests/frontend/buildcheckout.py @@ -227,9 +227,9 @@ def test_build_checkout_tar_with_unconventional_name(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.open(name=checkout, mode="r") - assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() - assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() + with tarfile.open(name=checkout, mode="r") as tar: + assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() + assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) @@ -266,9 +266,9 @@ def test_build_checkout_tar_no_compression(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.open(name=checkout, mode="r:gz") - assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() - assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() + with tarfile.open(name=checkout, mode="r:gz") as tar: + assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() + assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) @@ -291,17 +291,16 @@ def test_build_checkout_tarball(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.TarFile(checkout) + with tarfile.TarFile(checkout) as tar: + tarinfo = tar.getmember(os.path.join(".", "usr", "bin", "hello")) + assert tarinfo.mode == 0o755 + assert tarinfo.uid == 0 and tarinfo.gid == 0 + assert tarinfo.uname == "" and tarinfo.gname == "" - tarinfo = tar.getmember(os.path.join(".", "usr", "bin", "hello")) - assert tarinfo.mode == 0o755 - assert tarinfo.uid == 0 and tarinfo.gid == 0 - assert tarinfo.uname == "" and tarinfo.gname == "" - - tarinfo = tar.getmember(os.path.join(".", "usr", "include", "pony.h")) - assert tarinfo.mode == 0o644 - assert tarinfo.uid == 0 and tarinfo.gid == 0 - assert tarinfo.uname == "" and tarinfo.gname == "" + tarinfo = tar.getmember(os.path.join(".", "usr", "include", "pony.h")) + assert tarinfo.mode == 0o644 + assert tarinfo.uid == 0 and tarinfo.gid == 0 + assert tarinfo.uname == "" and tarinfo.gname == "" @pytest.mark.datafiles(DATA_DIR) @@ -340,8 +339,8 @@ def test_build_checkout_tarball_using_ref(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.TarFile(checkout) - assert os.path.join(".", "etc", "buildstream", "config") in tar.getnames() + with tarfile.TarFile(checkout) as tar: + assert os.path.join(".", "etc", "buildstream", "config") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) @@ -436,11 +435,9 @@ def test_build_checkout_tarball_compression(datafiles, cli, compression): result = cli.run(project=project, args=checkout_args) result.assert_success() - # Docs say not to use TarFile class directly, using .open seems to work. - # https://docs.python.org/3/library/tarfile.html#tarfile.TarFile - tar = tarfile.open(name=checkout, mode="r:" + compression) - assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() - assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() + with tarfile.open(name=checkout, mode="r:" + compression) as tar: + assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() + assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) @@ -463,9 +460,9 @@ def test_build_checkout_tarball_stdout(datafiles, cli): with open(tarball, "wb") as f: f.write(result.output) - tar = tarfile.TarFile(tarball) - assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() - assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() + with tarfile.TarFile(tarball) as tar: + assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() + assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) @@ -480,12 +477,12 @@ def test_build_checkout_tarball_mtime_nonzero(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.TarFile(tarpath) - for tarinfo in tar.getmembers(): - # An mtime of zero can be confusing to other software, - # e.g. ninja build and template toolkit have both taken zero mtime to - # mean 'file does not exist'. - assert tarinfo.mtime > 0 + with tarfile.TarFile(tarpath) as tar: + for tarinfo in tar.getmembers(): + # An mtime of zero can be confusing to other software, + # e.g. ninja build and template toolkit have both taken zero mtime to + # mean 'file does not exist'. + assert tarinfo.mtime > 0 @pytest.mark.datafiles(DATA_DIR) @@ -548,9 +545,12 @@ def test_build_checkout_tarball_links(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.open(name=checkout, mode="r:") - tar.extractall(extract) - assert open(os.path.join(extract, "basicfolder", "basicsymlink")).read() == "file contents\n" + with tarfile.open(name=checkout, mode="r:") as tar: + tar.extractall(extract) + + with open(os.path.join(extract, "basicfolder", "basicsymlink")) as fp: + data = fp.read() + assert data == "file contents\n" @pytest.mark.datafiles(DATA_DIR) @@ -578,7 +578,9 @@ def test_build_checkout_links(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - assert open(os.path.join(checkout, "basicfolder", "basicsymlink")).read() == "file contents\n" + with open(os.path.join(checkout, "basicfolder", "basicsymlink")) as fp: + data = fp.read() + assert data == "file contents\n" @pytest.mark.datafiles(DATA_DIR) @@ -680,9 +682,9 @@ def test_build_checkout_force_tarball(datafiles, cli): result = cli.run(project=project, args=checkout_args) result.assert_success() - tar = tarfile.TarFile(tarball) - assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() - assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() + with tarfile.TarFile(tarball) as tar: + assert os.path.join(".", "usr", "bin", "hello") in tar.getnames() + assert os.path.join(".", "usr", "include", "pony.h") in tar.getnames() @pytest.mark.datafiles(DATA_DIR) diff --git a/tests/internals/storage_vdir_import.py b/tests/internals/storage_vdir_import.py index 459cb48dd..b302c630e 100644 --- a/tests/internals/storage_vdir_import.py +++ b/tests/internals/storage_vdir_import.py @@ -299,7 +299,9 @@ def test_descend(tmpdir): d.import_files(test_dir) digest = d.descend("a", "l").index["g"].get_digest() - assert Content_to_check == open(cas_cache.objpath(digest)).read() + with open(cas_cache.objpath(digest)) as fp: + content = fp.read() + assert Content_to_check == content finally: cas_cache.release_resources() @@ -354,7 +356,9 @@ def test_relative_symlink(tmpdir): d.import_files(test_dir) digest = d.descend("a", "l", follow_symlinks=True).index["file"].get_digest() - assert Content_to_check == open(cas_cache.objpath(digest)).read() + with open(cas_cache.objpath(digest)) as fp: + content = fp.read() + assert Content_to_check == content finally: cas_cache.release_resources() @@ -380,7 +384,9 @@ def test_abs_symlink(tmpdir): digest = d.descend("a", "l", follow_symlinks=True).index["file"].get_digest() - assert Content_to_check == open(cas_cache.objpath(digest)).read() + with open(cas_cache.objpath(digest)) as fp: + content = fp.read() + assert Content_to_check == content finally: cas_cache.release_resources() diff --git a/tests/testutils/filetypegenerator.py b/tests/testutils/filetypegenerator.py index 41c502be2..732608cb6 100644 --- a/tests/testutils/filetypegenerator.py +++ b/tests/testutils/filetypegenerator.py @@ -63,7 +63,12 @@ def generate_file_types(path): os.chdir(parent) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - s.bind(child) - os.chdir(old_dir) - yield + + try: + s.bind(child) + os.chdir(old_dir) + yield + finally: + s.close() + clean() |