From e83e2a4467dea5457016c23f8cfdefb5234260e8 Mon Sep 17 00:00:00 2001 From: Tobias Frost Date: Sun, 6 Sep 2020 20:02:24 +0200 Subject: [PATCH] Hint mkstemp to create directory in the destination directory. --- src/octoprint/util/__init__.py | 4 +- tests/util/test_file_helpers.py | 77 ++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 5072b022a..f2951f46e 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -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: diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 80c009434..3d3d64741 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -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()