From 937172f0858c3881b6609316279fa630c8c5a4a3 Mon Sep 17 00:00:00 2001 From: Dorota Jarecka Date: Tue, 10 Nov 2020 21:39:13 -0500 Subject: [PATCH] fixing template formatting - didnt work well if the directory had a dot in the name --- pydra/engine/helpers_file.py | 8 +++-- pydra/engine/tests/test_shelltask.py | 52 ++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pydra/engine/helpers_file.py b/pydra/engine/helpers_file.py index 351e23610b..eff8e20033 100644 --- a/pydra/engine/helpers_file.py +++ b/pydra/engine/helpers_file.py @@ -600,8 +600,12 @@ def _template_formatting(template, inputs_dict, keep_extension=True): fld_value = inputs_dict[fld_name] if fld_value is attr.NOTHING: return attr.NOTHING - fld_value = str(fld_value) # in case it's a path - filename, *ext = fld_value.split(".", maxsplit=1) + fld_value_parent = Path(fld_value).parent + fld_value_name = Path(fld_value).name + + name, *ext = fld_value_name.split(".", maxsplit=1) + filename = str(fld_value_parent / name) + # if keep_extension is False, the extensions are removed if keep_extension is False: ext = [] diff --git a/pydra/engine/tests/test_shelltask.py b/pydra/engine/tests/test_shelltask.py index ba95c6af81..b21b4c7c5b 100644 --- a/pydra/engine/tests/test_shelltask.py +++ b/pydra/engine/tests/test_shelltask.py @@ -1162,8 +1162,56 @@ def test_shell_cmd_inputspec_9(tmpdir, plugin, results_function): assert shelly.output_dir == res.output.file_copy.parent -@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter]) +@pytest.mark.parametrize("results_function", [result_no_submitter]) def test_shell_cmd_inputspec_9a(tmpdir, plugin, results_function): + """ + providing output name using input_spec (output_file_template in metadata), + the template has a suffix, the extension of the file will be moved to the end + the change: input file has directory with a dot + """ + cmd = "cp" + file = tmpdir.mkdir("data.inp").join("file.txt") + file.write("content") + + my_input_spec = SpecInfo( + name="Input", + fields=[ + ( + "file_orig", + attr.ib( + type=File, + metadata={"position": 2, "help_string": "new file", "argstr": ""}, + ), + ), + ( + "file_copy", + attr.ib( + type=str, + metadata={ + "output_file_template": "{file_orig}_copy", + "help_string": "output file", + "argstr": "", + }, + ), + ), + ], + bases=(ShellSpec,), + ) + + shelly = ShellCommandTask( + name="shelly", executable=cmd, input_spec=my_input_spec, file_orig=file + ) + + res = results_function(shelly, plugin) + assert res.output.stdout == "" + assert res.output.file_copy.exists() + assert res.output.file_copy.name == "file_copy.txt" + # checking if it's created in a good place + assert shelly.output_dir == res.output.file_copy.parent + + +@pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter]) +def test_shell_cmd_inputspec_9b(tmpdir, plugin, results_function): """ providing output name using input_spec (output_file_template in metadata) and the keep_extension is set to False, so the extension is removed completely. @@ -1209,7 +1257,7 @@ def test_shell_cmd_inputspec_9a(tmpdir, plugin, results_function): @pytest.mark.parametrize("results_function", [result_no_submitter, result_submitter]) -def test_shell_cmd_inputspec_9b(tmpdir, plugin, results_function): +def test_shell_cmd_inputspec_9c(tmpdir, plugin, results_function): """ providing output name using input_spec (output_file_template in metadata) and the keep_extension is set to False, so the extension is removed completely,