From e49d0b0c223609f26a204b9661243e7fcb6216a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 3 Aug 2020 10:50:57 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Use=20fixed=20umask=20for=20test?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/util/test_file_helpers.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 196db58fb..eed953d48 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -108,6 +108,7 @@ class TestAtomicWrite(unittest.TestCase): # setup fd = 0 path = "tempfile.tmp" + umask = 0o026 mock_file = mock.MagicMock() mock_file.name = path @@ -117,8 +118,9 @@ class TestAtomicWrite(unittest.TestCase): mock_exists.return_value = False # test - with octoprint.util.atomic_write("somefile.yaml") as f: - f.write("test") + with mock.patch("octoprint.util.UMASK", umask): + with octoprint.util.atomic_write("somefile.yaml") as f: + f.write("test") # assert mock_mkstemp.assert_called_once_with(prefix="tmp", suffix="") @@ -126,7 +128,7 @@ class TestAtomicWrite(unittest.TestCase): mock_open.assert_called_once_with(path, mode="w+b") mock_file.write.assert_called_once_with("test") mock_file.close.assert_called_once_with() - mock_chmod.assert_called_once_with(path, 0o664) + mock_chmod.assert_called_once_with(path, 0o644 & ~umask) mock_move.assert_called_once_with(path, "somefile.yaml") @mock.patch("shutil.move") @@ -214,6 +216,7 @@ class TestAtomicWrite(unittest.TestCase): # setup fd = 0 path = "tempfile.tmp" + umask = 0o026 mock_file = mock.MagicMock() mock_file.name = path @@ -223,15 +226,16 @@ class TestAtomicWrite(unittest.TestCase): mock_exists.return_value = False # test - with octoprint.util.atomic_write("somefile.yaml", mode="w", prefix="foo", suffix="bar") as f: - f.write("test") + with mock.patch("octoprint.util.UMASK", umask): + with octoprint.util.atomic_write("somefile.yaml", mode="w", prefix="foo", suffix="bar") as f: + f.write("test") # assert mock_mkstemp.assert_called_once_with(prefix="foo", suffix="bar") 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() - mock_chmod.assert_called_once_with(path, 0o664) + mock_chmod.assert_called_once_with(path, 0o664 & ~umask) mock_move.assert_called_once_with(path, "somefile.yaml") @@ -241,7 +245,7 @@ class TestAtomicWrite(unittest.TestCase): @mock.patch("os.close") @mock.patch("os.chmod") @mock.patch("os.path.exists") - def test_atomic_custom_permissions(self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move): + def test_atomic_write_custom_permissions(self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move): """Tests that custom permissions may be set.""" # setup