summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenjamin Schubert <ben.c.schubert@gmail.com>2018-11-09 10:41:26 +0000
committerrichardmaw-codethink <richard.maw@codethink.co.uk>2018-11-19 11:39:51 +0000
commit88089d2d3451573b109bd396aa4df033192375ce (patch)
tree20c65d15587a1ae31b939faad693741dce8910f8 /tests
parenta6defc0b47423180c3f17a723692019458603bb6 (diff)
downloadbuildstream-88089d2d3451573b109bd396aa4df033192375ce.tar.gz
Extract atomic move function to utils.py
Moving atomically a file/directory can be tricky since different errors might be raised for the same underlying problem. Having a utility function to reduce this discrepancies will help in ensuring we have correct behavior
Diffstat (limited to 'tests')
-rw-r--r--tests/sources/git.py63
-rwxr-xr-xtests/utils/movedirectory.py88
2 files changed, 89 insertions, 62 deletions
diff --git a/tests/sources/git.py b/tests/sources/git.py
index e5bce44a8..7ab32a6b5 100644
--- a/tests/sources/git.py
+++ b/tests/sources/git.py
@@ -21,14 +21,11 @@
#
import os
-from unittest import mock
import pytest
-import py.path
from buildstream._exceptions import ErrorDomain
-from buildstream import _yaml, SourceError
+from buildstream import _yaml
from buildstream.plugin import CoreWarnings
-from buildstream.plugins.sources.git import GitMirror
from tests.testutils import cli, create_repo
from tests.testutils.site import HAVE_GIT
@@ -39,64 +36,6 @@ DATA_DIR = os.path.join(
)
-@pytest.mark.parametrize(
- ["type_", "setup"],
- [
- ("inexistent-dir", lambda path: None),
- ("empty-dir", lambda path: path.mkdir()),
- ("non-empty-dir", lambda path: path.join("bad").write("hi", ensure=True)),
- ("file", lambda path: path.write("no")),
- ],
-)
-def test_git_mirror_atomic_move(tmpdir, type_, setup):
- # Create required elements
- class DummySource:
- def get_mirror_directory(self):
- return str(tmpdir.join("source").mkdir())
-
- status = mock.MagicMock()
-
- url = "file://{}/url".format(tmpdir)
-
- # Create fake download directory
- download_dir = tmpdir.join("download_dir")
- download_dir.join("oracle").write("hello", ensure=True)
-
- # Initiate mirror
- mirror = GitMirror(DummySource(), None, url, None)
-
- # Setup destination directory
- setup(py.path.local(mirror.mirror))
-
- # Copy directory content
- if type_ == "file":
- with pytest.raises(SourceError):
- mirror._atomic_move_mirror(str(download_dir), mirror.url)
- else:
- mirror._atomic_move_mirror(str(download_dir), mirror.url)
-
- # Validate
- if type_ == "non-empty-dir":
- # With a non empty directory, we should not have overriden
- # and notified a status
- assert DummySource.status.called
-
- expected_oracle = os.path.join(mirror.mirror, "bad")
- expected_content = "hi"
- elif type_ == "file":
- expected_oracle = mirror.mirror
- expected_content = "no"
- else:
- # Otherwise we should have a new directory and the data
- expected_oracle = os.path.join(mirror.mirror, "oracle")
- expected_content = "hello"
-
- assert os.path.exists(expected_oracle)
-
- with open(expected_oracle) as fp:
- assert fp.read() == expected_content
-
-
@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available")
@pytest.mark.datafiles(os.path.join(DATA_DIR, 'template'))
def test_fetch_bad_ref(cli, tmpdir, datafiles):
diff --git a/tests/utils/movedirectory.py b/tests/utils/movedirectory.py
new file mode 100755
index 000000000..6edbec611
--- /dev/null
+++ b/tests/utils/movedirectory.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)