11"""Tests for `imcflibs.imagej.misc` utility functions."""
22
33import logging
4+ import os
45
56from org .scijava .script import ScriptInfo , ScriptModule
67
910from imcflibs .imagej .misc import bytes_to_human_readable
1011from imcflibs .imagej .misc import save_script_parameters
1112
12-
1313PASSWORD_ITEMS = ["OMERO_PASSWD" ]
1414
1515
16- def test_save_script_parameters_fail (caplog ):
16+ def test_save_script_parameters_no_destination (caplog ):
17+ """Tests save_script_parameters skips saving without a destination."""
18+ caplog .clear ()
19+
20+ save_script_parameters (script_globals = None )
21+ assert "No destination provided" in caplog .messages [0 ]
22+
23+
24+ def test_save_script_parameters_fail (tmp_path , caplog ):
1725 """Tests save_script_parameters with an invalid script_globals object."""
1826 caplog .clear ()
1927
20- save_script_parameters (script_globals = None , destination = "" )
28+ save_script_parameters (script_globals = None , destination = tmp_path )
2129 assert "ScriptModule inspection failed" in caplog .messages [0 ]
2230
2331
@@ -38,19 +46,63 @@ def _is_password_style(item):
3846 input_names = ["AAA" , "BBB" , "OMERO_PASSWD" , "SJLOG" , "NOT_THERE" ],
3947 inputs = {"AAA" : "aaa" , "BBB" : "bbb" , "OMERO_PASSWD" : "ultra-secret" },
4048 )
49+ script_module .getInfo ().location = "file:/some/path/example1.py"
4150 script_globals = {"org.scijava.script.ScriptModule" : script_module }
42- save_script_parameters (script_globals , destination = base )
51+ save_script_parameters (script_globals , destination = base , overwrite = True )
4352 assert "Skipping parameter from skip-list" in caplog .text
4453 assert "Skipping password-style parameter" in caplog .text
4554 assert "Unable to fetch value for parameter: NOT_THERE" in caplog .text
4655 assert "Saved 2 parameters (skipped 1 password-style and 1 others)." in caplog .text
4756 assert "Saved 2 script parameters to" in caplog .text
4857
49- with open (str (base ) + "/script_parameters .txt" , "r" ) as f :
58+ with open (str (base ) + "/example1_params .txt" , "r" ) as f :
5059 contents = f .read ()
5160 assert contents == "AAA: aaa\n BBB: bbb\n "
5261
5362
63+ def test_save_script_parameters_timestamped (tmp_path , monkeypatch ):
64+ """Tests save_script_parameters default naming without a script location."""
65+
66+ def _is_password_style (item ):
67+ return False
68+
69+ monkeypatch .setattr (imcflibs .imagej .misc , "_is_password_style" , _is_password_style )
70+
71+ script_module = ScriptModule (input_names = ["AAA" ], inputs = {"AAA" : "aaa" })
72+ script_globals = {"org.scijava.script.ScriptModule" : script_module }
73+ save_script_parameters (script_globals , destination = tmp_path )
74+
75+ # the folder should now contain exactly one file
76+ files = os .listdir (str (tmp_path ))
77+ assert len (files ) == 1
78+
79+ # its name is "script_run-<timestamp>_params.txt"
80+ file_name = files [0 ]
81+ assert file_name .startswith ("script_run-" )
82+ assert file_name .endswith ("_params.txt" )
83+
84+ with open (os .path .join (str (tmp_path ), file_name ), "r" ) as f :
85+ assert f .read () == "AAA: aaa\n "
86+
87+
88+ def test_save_script_parameters_skip_whole_names (tmp_path , monkeypatch , caplog ):
89+ """Only whole names are skipped, case insensitive, not substrings."""
90+ caplog .set_level (logging .DEBUG )
91+ caplog .clear ()
92+ monkeypatch .setattr (imcflibs .imagej .misc , "_is_password_style" , lambda item : False )
93+
94+ script_module = ScriptModule (
95+ input_names = ["rm" , "fusion_format" , "commander" ],
96+ inputs = {"rm" : "roimanager" , "fusion_format" : "TIFF" , "commander" : "x" },
97+ )
98+ script_globals = {"org.scijava.script.ScriptModule" : script_module }
99+ save_script_parameters (script_globals , destination = tmp_path , overwrite = True )
100+
101+ with open (str (tmp_path ) + "/script_params.txt" , "r" ) as f :
102+ contents = f .read ()
103+ assert contents == "fusion_format: TIFF\n commander: x\n "
104+
105+
54106def test_bytes_to_human_readable_simple ():
55107 """Ensure common sizes are formatted into human-readable strings."""
56108 assert bytes_to_human_readable (500 ) == "500.0 bytes"
0 commit comments