💚 Fix tests

This commit is contained in:
Gina Häußge committed 2020-10-22 16:22:00 +02:00
1 parent d5e4cf3a1a
commit 55ddfdd314
2 files changed
+84 -101

No files matched your search

+6 -1
View File
@@ -1158,7 +1158,12 @@ def atomic_write(
dir = os.path.dirname(filename)
temp_config = tempfile.NamedTemporaryFile(
mode=mode, prefix=prefix, suffix=suffix, delete=False, encoding=encoding, dir=dir
mode=mode,
prefix=prefix,
suffix=suffix,
delete=False,
dir=dir,
encoding=encoding if "b" not in mode else None,
)
try:
try:
+78 -100
View File
@@ -119,26 +119,20 @@ class TestAtomicWrite(unittest.TestCase):
pass
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@mock.patch("os.close")
@mock.patch("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write(
self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move
):
def test_atomic_write(self, mock_exists, mock_chmod, mock_namedtempfile, mock_move):
"""Tests the regular basic "good" case."""
# setup
fd = 0
path = "tempfile.tmp"
umask = 0o026
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = False
# test
@@ -147,27 +141,24 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
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_namedtempfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="w+b", encoding=None, delete=False
)
mock_file.write.assert_called_once_with("test")
mock_file.close.assert_called_once_with()
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("tempfile.NamedTemporaryFile")
@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
self, mock_exists, mock_chmod, mock_namedtempfile, mock_move
):
"""Tests whether the tempoary file is to created in the same directory as the target file."""
"""Tests whether the temporary 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")
@@ -175,32 +166,38 @@ class TestAtomicWrite(unittest.TestCase):
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = False
umask = 0o026
# test
with octoprint.util.atomic_write(targetpath) as f:
f.write("test")
with mock.patch("octoprint.util.UMASK", umask):
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_namedtempfile.assert_called_once_with(
prefix="tmp",
suffix="",
dir=tmpdirpath,
mode="w+b",
encoding=None,
delete=False,
)
mock_chmod.assert_called_once_with(path, 0o664 & ~umask)
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("tempfile.NamedTemporaryFile")
@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
self, mock_exists, mock_chmod, mock_namedtempfile, mock_move
):
"""Tests whether the tempoary file is to created in the same directory as the target file. This time submitting a relative path. """
"""Tests whether the temporary 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")
@@ -208,40 +205,45 @@ class TestAtomicWrite(unittest.TestCase):
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = False
umask = 0o026
# test
with octoprint.util.atomic_write(targetpath) as f:
f.write("test")
with mock.patch("octoprint.util.UMASK", umask):
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_namedtempfile.assert_called_once_with(
prefix="tmp",
suffix="",
dir=tmpdirpath,
mode="w+b",
encoding=None,
delete=False,
)
mock_chmod.assert_called_once_with(path, 0o664 & ~umask)
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("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_error_on_write(
self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move
self, mock_exists, mock_chmod, mock_namedtempfile, mock_move
):
"""Tests the error case where something in the wrapped code fails."""
# setup
fd = 0
path = "tempfile.tmp"
mock_file = mock.MagicMock()
mock_file.name = path
mock_file.write.side_effect = RuntimeError()
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = False
# test
@@ -253,32 +255,28 @@ class TestAtomicWrite(unittest.TestCase):
pass
# assert
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_namedtempfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="w+b", encoding=None, delete=False
)
mock_file.close.assert_called_once_with()
self.assertFalse(mock_move.called)
self.assertFalse(mock_chmod.called)
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@mock.patch("os.close")
@mock.patch("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_error_on_move(
self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move
self, mock_exists, mock_chmod, mock_namedtempfile, mock_move
):
"""Tests the error case where the final move fails."""
# setup
fd = 0
path = "tempfile.tmp"
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_move.side_effect = RuntimeError()
mock_exists.return_value = False
@@ -291,34 +289,30 @@ class TestAtomicWrite(unittest.TestCase):
pass
# assert
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_namedtempfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="w+b", encoding=None, delete=False
)
mock_file.close.assert_called_once_with()
self.assertTrue(mock_move.called)
self.assertTrue(mock_chmod.called)
@mock.patch("shutil.move")
@mock.patch("tempfile.mkstemp")
@mock.patch("io.open")
@mock.patch("os.close")
@mock.patch("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_parameters(
self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move
self, mock_exists, mock_chmod, mock_namedtempfile, mock_move
):
"""Tests that the open parameters are propagated properly."""
# setup
fd = 0
path = "tempfile.tmp"
umask = 0o026
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = False
# test
@@ -329,33 +323,29 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
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_namedtempfile.assert_called_once_with(
prefix="foo", suffix="bar", dir="", mode="w", encoding="utf-8", delete=False
)
mock_file.close.assert_called_once_with()
mock_chmod.assert_called_once_with(path, 0o664 & ~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("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
def test_atomic_write_custom_permissions(
self, mock_exists, mock_chmod, mock_close, mock_open, mock_mkstemp, mock_move
self, mock_exists, mock_chmod, mock_namedtemporaryfile, mock_move
):
"""Tests that custom permissions may be set."""
# setup
fd = 0
path = "tempfile.tmp"
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtemporaryfile.return_value = mock_file
mock_exists.return_value = False
# test
@@ -365,17 +355,15 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
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_namedtemporaryfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="wt", encoding="utf-8", delete=False
)
mock_file.close.assert_called_once_with()
mock_chmod.assert_called_once_with(path, 0o755)
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("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
@mock.patch("os.stat")
@@ -384,22 +372,18 @@ class TestAtomicWrite(unittest.TestCase):
mock_stat,
mock_exists,
mock_chmod,
mock_close,
mock_open,
mock_mkstemp,
mock_namedtempfile,
mock_move,
):
"""Tests that the permissions of an existing file are combined with the requested permissions."""
# setup
fd = 0
path = "tempfile.tmp"
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = True
mock_stat_result = mock.MagicMock()
@@ -413,17 +397,15 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
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_namedtempfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="wt", encoding="utf-8", delete=False
)
mock_file.close.assert_called_once_with()
mock_chmod.assert_called_once_with(path, 0o777) # 0o755 | 0o666
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("tempfile.NamedTemporaryFile")
@mock.patch("os.chmod")
@mock.patch("os.path.exists")
@mock.patch("os.stat")
@@ -432,22 +414,18 @@ class TestAtomicWrite(unittest.TestCase):
mock_stat,
mock_exists,
mock_chmod,
mock_close,
mock_open,
mock_mkstemp,
mock_namedtempfile,
mock_move,
):
"""Tests that max_permissions limit the combined file permissions."""
# setup
fd = 0
path = "tempfile.tmp"
mock_file = mock.MagicMock()
mock_file.name = path
mock_mkstemp.return_value = fd, path
mock_open.return_value = mock_file
mock_namedtempfile.return_value = mock_file
mock_exists.return_value = True
mock_stat_result = mock.MagicMock()
@@ -461,9 +439,9 @@ class TestAtomicWrite(unittest.TestCase):
f.write("test")
# assert
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_namedtempfile.assert_called_once_with(
prefix="tmp", suffix="", dir="", mode="wt", encoding="utf-8", delete=False
)
mock_file.close.assert_called_once_with()
mock_chmod.assert_called_once_with(
path, 0o644