Skip to content

Commit 9fafb73

Browse files
committed
Improve test by switching to monkeypatch
1 parent 29128e1 commit 9fafb73

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

tests/test_misc.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"""Tests for `imcflibs.imagej.misc` utility functions."""
22

3+
import logging
4+
5+
import imcflibs.imagej.misc
6+
37
from imcflibs.imagej.misc import bytes_to_human_readable
48
from imcflibs.imagej.misc import save_script_parameters
59

610

11+
PASSWORD_ITEMS = ["OMERO_PASSWD"]
12+
13+
714
class ModuleItem:
815
"""Mock for the org.scijava.module.ModuleItem interface."""
916

@@ -94,17 +101,29 @@ def test_save_script_parameters_fail(caplog):
94101
assert "ScriptModule inspection failed" in caplog.messages[0]
95102

96103

97-
# FIXME: probably better use monkeypatch instead of mocker for more flexibility
98-
# in modifying the return value depending on the ScriptModule contents
99-
def test_save_script_parameters(tmpdir, mocker):
104+
def test_save_script_parameters(tmp_path, monkeypatch, caplog):
100105
"""Tests save_script_parameters."""
101-
base = tmpdir.mkdir("base")
102-
m_is_password_style = mocker.patch("imcflibs.imagej.misc._is_password_style")
103-
m_is_password_style.return_value = False
106+
caplog.set_level(logging.DEBUG)
107+
caplog.clear()
104108

105-
script_module = ScriptModule(["AAA", "BBB"], {"AAA": "aaa", "BBB": "bbb"})
109+
base = tmp_path / "saved_parameters"
110+
base.mkdir()
111+
112+
def _is_password_style(item):
113+
return item.getName() in PASSWORD_ITEMS
114+
115+
monkeypatch.setattr(imcflibs.imagej.misc, "_is_password_style", _is_password_style)
116+
117+
script_module = ScriptModule(
118+
["AAA", "BBB", "OMERO_PASSWD"],
119+
{"AAA": "aaa", "BBB": "bbb", "OMERO_PASSWD": "ultra-secret"},
120+
)
106121
script_globals = {"org.scijava.script.ScriptModule": script_module}
107122
save_script_parameters(script_globals, destination=base)
123+
assert "Skipping password-style parameter" in caplog.text
124+
assert "Saved 2 parameters (skipped 1 password" in caplog.text
125+
assert "Saved 2 script parameters to" in caplog.text
126+
108127
with open(str(base) + "/script_parameters.txt", "r") as f:
109128
contents = f.read()
110129
assert contents == "AAA: aaa\nBBB: bbb\n"

0 commit comments

Comments
 (0)