|
1 | 1 | """Tests for `imcflibs.imagej.misc` utility functions.""" |
2 | 2 |
|
| 3 | +import logging |
| 4 | + |
| 5 | +import imcflibs.imagej.misc |
| 6 | + |
3 | 7 | from imcflibs.imagej.misc import bytes_to_human_readable |
4 | 8 | from imcflibs.imagej.misc import save_script_parameters |
5 | 9 |
|
6 | 10 |
|
| 11 | +PASSWORD_ITEMS = ["OMERO_PASSWD"] |
| 12 | + |
| 13 | + |
7 | 14 | class ModuleItem: |
8 | 15 | """Mock for the org.scijava.module.ModuleItem interface.""" |
9 | 16 |
|
@@ -94,17 +101,29 @@ def test_save_script_parameters_fail(caplog): |
94 | 101 | assert "ScriptModule inspection failed" in caplog.messages[0] |
95 | 102 |
|
96 | 103 |
|
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): |
100 | 105 | """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() |
104 | 108 |
|
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 | + ) |
106 | 121 | script_globals = {"org.scijava.script.ScriptModule": script_module} |
107 | 122 | 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 | + |
108 | 127 | with open(str(base) + "/script_parameters.txt", "r") as f: |
109 | 128 | contents = f.read() |
110 | 129 | assert contents == "AAA: aaa\nBBB: bbb\n" |
|
0 commit comments