Hint mkstemp to create directory in the destination directory.

This commit is contained in:
Tobias Frost committed 2020-09-07 15:10:50 +02:00
1 parent bdb68ae28b
commit e83e2a4467
2 files changed
+73 -8

No files matched your search

+3 -1
View File
@@ -1056,9 +1056,11 @@ def atomic_write(filename, mode="w+b", encoding="utf-8", prefix="tmp", suffix=""
if os.path.exists(filename):
permissions |= os.stat(filename).st_mode
permissions &= max_permissions
targetdirectory = os.path.dirname(filename)
# NamedTemporaryFile doesn't yet have an encoding parameter in py2, so we go the long way
fd, path = tempfile.mkstemp(suffix=suffix, prefix=prefix)
fd, path = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=targetdirectory)
os.close(fd)
if "b" in mode:
+70 -7
View File
@@ -121,7 +121,7 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="w+b")
mock_file.write.assert_called_once_with("test")
@@ -129,6 +129,69 @@ class TestAtomicWrite(unittest.TestCase):
mock_chmod.assert_called_once_with(path, 0o644)
mock_move.assert_called_once_with(path, "somefile.yaml")
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@mock.patch("os.close")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_path_aware(self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move):
"""Tests whether the tempoary file is to created in the same directory as the target file."""
# setup
fd = 0
tmpdirpath = "/testpath/with/subdirectories"
path = os.path.join(tmpdirpath, "tempfile.tmp")
targetpath = os.path.join(tmpdirpath, "somefile.yaml")
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_exists.return_value = False
# test
with octoprint.util.atomic_write(targetpath) as f:
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir=tmpdirpath)
mock_open.assert_called_once_with(path, mode="w+b")
mock_move.assert_called_once_with(path, targetpath)
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@mock.patch("os.close")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_rel_path_aware(self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move):
"""Tests whether the tempoary file is to created in the same directory as the target file. This time submitting a relative path. """
# setup
fd = 0
tmpdirpath = "../test"
path = os.path.join(tmpdirpath, "tempfile.tmp")
targetpath = os.path.join(tmpdirpath, "somefile.yaml")
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_exists.return_value = False
# test
with octoprint.util.atomic_write(targetpath) as f:
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir=tmpdirpath)
mock_open.assert_called_once_with(path, mode="w+b")
mock_move.assert_called_once_with(path, targetpath)
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@@ -159,7 +222,7 @@ class TestAtomicWrite(unittest.TestCase):
pass
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="w+b")
mock_file.close.assert_called_once_with()
@@ -195,7 +258,7 @@ class TestAtomicWrite(unittest.TestCase):
pass
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="w+b")
mock_file.close.assert_called_once_with()
@@ -227,7 +290,7 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="foo", suffix="bar")
mock_mkstemp.assert_called_once_with(prefix="foo", suffix="bar", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="w", encoding="utf-8")
mock_file.close.assert_called_once_with()
@@ -260,7 +323,7 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="wt", encoding="utf-8")
mock_file.close.assert_called_once_with()
@@ -297,7 +360,7 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="wt", encoding="utf-8")
mock_file.close.assert_called_once_with()
@@ -334,7 +397,7 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="")
mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="", dir="")
mock_close.assert_called_once_with(fd)
mock_open.assert_called_once_with(path, mode="wt", encoding="utf-8")
mock_file.close.assert_called_once_with()