diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index a615ea0a3..963a5a294 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -1067,9 +1067,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 eed953d48..c9b4f2114 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -123,7 +123,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") @@ -131,6 +131,69 @@ class TestAtomicWrite(unittest.TestCase): mock_chmod.assert_called_once_with(path, 0o644 & ~umask) 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") @@ -161,7 +224,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() @@ -197,7 +260,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() @@ -231,7 +294,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() @@ -264,7 +327,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() @@ -301,7 +364,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() @@ -338,7 +401,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()