summaryrefslogtreecommitdiff
path: root/tests/internals/utils_move_atomic.py
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-11 15:54:36 -0500
committerTristan Van Berkom <tristan.vanberkom@codethink.co.uk>2019-01-16 18:35:21 -0500
commit353a682fafa36b4e3df31e35bcd10ec4893399cb (patch)
tree9a2f6e2109ad98ba344818973d1982050a12d450 /tests/internals/utils_move_atomic.py
parent7e2300ce0431c5eddf12fa51f87f60a89e5c1450 (diff)
downloadbuildstream-353a682fafa36b4e3df31e35bcd10ec4893399cb.tar.gz
tests: Migrate utils tests into internals directory
Diffstat (limited to 'tests/internals/utils_move_atomic.py')
-rw-r--r--tests/internals/utils_move_atomic.py88
1 files changed, 88 insertions, 0 deletions
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)