From 353a682fafa36b4e3df31e35bcd10ec4893399cb Mon Sep 17 00:00:00 2001 From: Tristan Van Berkom Date: Fri, 11 Jan 2019 15:54:36 -0500 Subject: tests: Migrate utils tests into internals directory --- tests/internals/utils.py | 34 ++++++++++++++ tests/internals/utils_move_atomic.py | 88 ++++++++++++++++++++++++++++++++++++ tests/internals/utils_save_atomic.py | 62 +++++++++++++++++++++++++ tests/utils/__init__.py | 0 tests/utils/misc.py | 34 -------------- tests/utils/movedirectory.py | 88 ------------------------------------ tests/utils/savefile.py | 62 ------------------------- 7 files changed, 184 insertions(+), 184 deletions(-) create mode 100644 tests/internals/utils.py create mode 100644 tests/internals/utils_move_atomic.py create mode 100644 tests/internals/utils_save_atomic.py delete mode 100644 tests/utils/__init__.py delete mode 100644 tests/utils/misc.py delete mode 100755 tests/utils/movedirectory.py delete mode 100644 tests/utils/savefile.py diff --git a/tests/internals/utils.py b/tests/internals/utils.py new file mode 100644 index 000000000..a34d3cda5 --- /dev/null +++ b/tests/internals/utils.py @@ -0,0 +1,34 @@ +import os +from unittest import mock + +from buildstream import _yaml + +from ..testutils.runcli import cli + + +KiB = 1024 +MiB = (KiB * 1024) +GiB = (MiB * 1024) +TiB = (GiB * 1024) + + +def test_parse_size_over_1024T(cli, tmpdir): + cli.configure({ + 'cache': { + 'quota': 2048 * TiB + } + }) + project = tmpdir.join("main") + os.makedirs(str(project)) + _yaml.dump({'name': 'main'}, str(project.join("project.conf"))) + + volume_space_patch = mock.patch( + "buildstream._artifactcache.ArtifactCache._get_volume_space_info_for", + autospec=True, + return_value=(1025 * TiB, 1025 * TiB) + ) + + with volume_space_patch: + result = cli.run(project, args=["build", "file.bst"]) + failure_msg = 'Your system does not have enough available space to support the cache quota specified.' + assert failure_msg in result.stderr diff --git a/tests/internals/utils_move_atomic.py b/tests/internals/utils_move_atomic.py new file mode 100644 index 000000000..6edbec611 --- /dev/null +++ b/tests/internals/utils_move_atomic.py @@ -0,0 +1,88 @@ +import pytest + +from buildstream.utils import move_atomic, DirectoryExistsError + + +@pytest.fixture +def src(tmp_path): + src = tmp_path.joinpath("src") + src.mkdir() + + with src.joinpath("test").open("w") as fp: + fp.write("test") + + return src + + +def test_move_to_empty_dir(src, tmp_path): + dst = tmp_path.joinpath("dst") + + move_atomic(src, dst) + + assert dst.joinpath("test").exists() + + +def test_move_to_empty_dir_create_parents(src, tmp_path): + dst = tmp_path.joinpath("nested/dst") + + move_atomic(src, dst) + assert dst.joinpath("test").exists() + + +def test_move_to_empty_dir_no_create_parents(src, tmp_path): + dst = tmp_path.joinpath("nested/dst") + + with pytest.raises(FileNotFoundError): + move_atomic(src, dst, ensure_parents=False) + + +def test_move_non_existing_dir(tmp_path): + dst = tmp_path.joinpath("dst") + src = tmp_path.joinpath("src") + + with pytest.raises(FileNotFoundError): + move_atomic(src, dst) + + +def test_move_to_existing_empty_dir(src, tmp_path): + dst = tmp_path.joinpath("dst") + dst.mkdir() + + move_atomic(src, dst) + assert dst.joinpath("test").exists() + + +def test_move_to_existing_file(src, tmp_path): + dst = tmp_path.joinpath("dst") + + with dst.open("w") as fp: + fp.write("error") + + with pytest.raises(NotADirectoryError): + move_atomic(src, dst) + + +def test_move_file_to_existing_file(tmp_path): + dst = tmp_path.joinpath("dst") + src = tmp_path.joinpath("src") + + with src.open("w") as fp: + fp.write("src") + + with dst.open("w") as fp: + fp.write("dst") + + move_atomic(src, dst) + with dst.open() as fp: + assert fp.read() == "src" + + +def test_move_to_existing_non_empty_dir(src, tmp_path): + dst = tmp_path.joinpath("dst") + dst.mkdir() + + with dst.joinpath("existing").open("w") as fp: + fp.write("already there") + + with pytest.raises(DirectoryExistsError): + move_atomic(src, dst) diff --git a/tests/internals/utils_save_atomic.py b/tests/internals/utils_save_atomic.py new file mode 100644 index 000000000..0731f7bea --- /dev/null +++ b/tests/internals/utils_save_atomic.py @@ -0,0 +1,62 @@ +import os +import pytest + +from buildstream.utils import save_file_atomic + + +def test_save_new_file(tmpdir): + filename = os.path.join(str(tmpdir), 'savefile-success.test') + with save_file_atomic(filename, 'w') as f: + f.write('foo\n') + + assert os.listdir(str(tmpdir)) == ['savefile-success.test'] + with open(filename) as f: + assert f.read() == 'foo\n' + + +def test_save_over_existing_file(tmpdir): + filename = os.path.join(str(tmpdir), 'savefile-overwrite.test') + + with open(filename, 'w') as f: + f.write('existing contents\n') + + with save_file_atomic(filename, 'w') as f: + f.write('overwritten contents\n') + + assert os.listdir(str(tmpdir)) == ['savefile-overwrite.test'] + with open(filename) as f: + assert f.read() == 'overwritten contents\n' + + +def test_exception_new_file(tmpdir): + filename = os.path.join(str(tmpdir), 'savefile-exception.test') + + with pytest.raises(RuntimeError): + with save_file_atomic(filename, 'w') as f: + f.write('Some junk\n') + raise RuntimeError("Something goes wrong") + + assert os.listdir(str(tmpdir)) == [] + + +def test_exception_existing_file(tmpdir): + filename = os.path.join(str(tmpdir), 'savefile-existing.test') + + with open(filename, 'w') as f: + f.write('existing contents\n') + + with pytest.raises(RuntimeError): + with save_file_atomic(filename, 'w') as f: + f.write('Some junk\n') + raise RuntimeError("Something goes wrong") + + assert os.listdir(str(tmpdir)) == ['savefile-existing.test'] + with open(filename) as f: + assert f.read() == 'existing contents\n' + + +def test_attributes(tmpdir): + filename = os.path.join(str(tmpdir), 'savefile-attributes.test') + with save_file_atomic(filename, 'w') as f: + assert f.real_filename == filename + assert f.name != filename diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/utils/misc.py b/tests/utils/misc.py deleted file mode 100644 index a34d3cda5..000000000 --- a/tests/utils/misc.py +++ /dev/null @@ -1,34 +0,0 @@ -import os -from unittest import mock - -from buildstream import _yaml - -from ..testutils.runcli import cli - - -KiB = 1024 -MiB = (KiB * 1024) -GiB = (MiB * 1024) -TiB = (GiB * 1024) - - -def test_parse_size_over_1024T(cli, tmpdir): - cli.configure({ - 'cache': { - 'quota': 2048 * TiB - } - }) - project = tmpdir.join("main") - os.makedirs(str(project)) - _yaml.dump({'name': 'main'}, str(project.join("project.conf"))) - - volume_space_patch = mock.patch( - "buildstream._artifactcache.ArtifactCache._get_volume_space_info_for", - autospec=True, - return_value=(1025 * TiB, 1025 * TiB) - ) - - with volume_space_patch: - result = cli.run(project, args=["build", "file.bst"]) - failure_msg = 'Your system does not have enough available space to support the cache quota specified.' - assert failure_msg in result.stderr diff --git a/tests/utils/movedirectory.py b/tests/utils/movedirectory.py deleted file mode 100755 index 6edbec611..000000000 --- a/tests/utils/movedirectory.py +++ /dev/null @@ -1,88 +0,0 @@ -import pytest - -from buildstream.utils import move_atomic, DirectoryExistsError - - -@pytest.fixture -def src(tmp_path): - src = tmp_path.joinpath("src") - src.mkdir() - - with src.joinpath("test").open("w") as fp: - fp.write("test") - - return src - - -def test_move_to_empty_dir(src, tmp_path): - dst = tmp_path.joinpath("dst") - - move_atomic(src, dst) - - assert dst.joinpath("test").exists() - - -def test_move_to_empty_dir_create_parents(src, tmp_path): - dst = tmp_path.joinpath("nested/dst") - - move_atomic(src, dst) - assert dst.joinpath("test").exists() - - -def test_move_to_empty_dir_no_create_parents(src, tmp_path): - dst = tmp_path.joinpath("nested/dst") - - with pytest.raises(FileNotFoundError): - move_atomic(src, dst, ensure_parents=False) - - -def test_move_non_existing_dir(tmp_path): - dst = tmp_path.joinpath("dst") - src = tmp_path.joinpath("src") - - with pytest.raises(FileNotFoundError): - move_atomic(src, dst) - - -def test_move_to_existing_empty_dir(src, tmp_path): - dst = tmp_path.joinpath("dst") - dst.mkdir() - - move_atomic(src, dst) - assert dst.joinpath("test").exists() - - -def test_move_to_existing_file(src, tmp_path): - dst = tmp_path.joinpath("dst") - - with dst.open("w") as fp: - fp.write("error") - - with pytest.raises(NotADirectoryError): - move_atomic(src, dst) - - -def test_move_file_to_existing_file(tmp_path): - dst = tmp_path.joinpath("dst") - src = tmp_path.joinpath("src") - - with src.open("w") as fp: - fp.write("src") - - with dst.open("w") as fp: - fp.write("dst") - - move_atomic(src, dst) - with dst.open() as fp: - assert fp.read() == "src" - - -def test_move_to_existing_non_empty_dir(src, tmp_path): - dst = tmp_path.joinpath("dst") - dst.mkdir() - - with dst.joinpath("existing").open("w") as fp: - fp.write("already there") - - with pytest.raises(DirectoryExistsError): - move_atomic(src, dst) diff --git a/tests/utils/savefile.py b/tests/utils/savefile.py deleted file mode 100644 index 0731f7bea..000000000 --- a/tests/utils/savefile.py +++ /dev/null @@ -1,62 +0,0 @@ -import os -import pytest - -from buildstream.utils import save_file_atomic - - -def test_save_new_file(tmpdir): - filename = os.path.join(str(tmpdir), 'savefile-success.test') - with save_file_atomic(filename, 'w') as f: - f.write('foo\n') - - assert os.listdir(str(tmpdir)) == ['savefile-success.test'] - with open(filename) as f: - assert f.read() == 'foo\n' - - -def test_save_over_existing_file(tmpdir): - filename = os.path.join(str(tmpdir), 'savefile-overwrite.test') - - with open(filename, 'w') as f: - f.write('existing contents\n') - - with save_file_atomic(filename, 'w') as f: - f.write('overwritten contents\n') - - assert os.listdir(str(tmpdir)) == ['savefile-overwrite.test'] - with open(filename) as f: - assert f.read() == 'overwritten contents\n' - - -def test_exception_new_file(tmpdir): - filename = os.path.join(str(tmpdir), 'savefile-exception.test') - - with pytest.raises(RuntimeError): - with save_file_atomic(filename, 'w') as f: - f.write('Some junk\n') - raise RuntimeError("Something goes wrong") - - assert os.listdir(str(tmpdir)) == [] - - -def test_exception_existing_file(tmpdir): - filename = os.path.join(str(tmpdir), 'savefile-existing.test') - - with open(filename, 'w') as f: - f.write('existing contents\n') - - with pytest.raises(RuntimeError): - with save_file_atomic(filename, 'w') as f: - f.write('Some junk\n') - raise RuntimeError("Something goes wrong") - - assert os.listdir(str(tmpdir)) == ['savefile-existing.test'] - with open(filename) as f: - assert f.read() == 'existing contents\n' - - -def test_attributes(tmpdir): - filename = os.path.join(str(tmpdir), 'savefile-attributes.test') - with save_file_atomic(filename, 'w') as f: - assert f.real_filename == filename - assert f.name != filename -- cgit v1.2.1