From e327b2e1d77b5640aff20f084713945b95022bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20Zar=C4=99bski?= Date: Mon, 3 Jun 2024 08:36:29 +0100 Subject: [PATCH] Add test and ensure '_' -> '-' conversion in command assembly --- simvue/executor.py | 3 +++ tests/refactor/test_executor.py | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/simvue/executor.py b/simvue/executor.py index d156c2a1..532a5ce4 100644 --- a/simvue/executor.py +++ b/simvue/executor.py @@ -189,6 +189,9 @@ def callback_function(status_code: int, std_out: str, std_err: str) -> None: for arg, value in kwargs.items(): if arg.startswith("__"): continue + + arg = arg.replace("_", "-") + if len(arg) == 1: if isinstance(value, bool) and value: _command += [f"-{arg}"] diff --git a/tests/refactor/test_executor.py b/tests/refactor/test_executor.py index 45b8bbbf..f928525f 100644 --- a/tests/refactor/test_executor.py +++ b/tests/refactor/test_executor.py @@ -2,6 +2,8 @@ import simvue import time import sys +import tempfile +import pathlib import multiprocessing @@ -42,3 +44,47 @@ def test_executor_add_process( message_contains="successfully" if successful else "non-zero exit", ) assert len(_events) == 1 + + +@pytest.mark.executor +def test_add_process_command_assembly(request: pytest.FixtureRequest) -> None: + with tempfile.TemporaryDirectory() as tempd: + _python_script=""" +import argparse +import os.path + +parser = argparse.ArgumentParser() +parser.add_argument('input_file') +parser.add_argument('--output-file') + +args=parser.parse_args() + +in_text = open(args.input_file).read() + +with open(args.output_file, 'w') as out_f: + out_f.write(in_text) + out_f.write('End of Line.') +""" + with (in_file := pathlib.Path(tempd).joinpath("input.txt")).open("w") as out_f: + out_f.write("Flynn has entered the grid.\n") + + with (code_file := pathlib.Path(tempd).joinpath("demo.py")).open("w") as out_f: + out_f.write(_python_script) + + out_file = pathlib.Path(tempd).joinpath("output.txt") + expected_cmd = f"python {code_file} {in_file} --output-file {out_file}" + + with simvue.Run() as run: + run.init( + "test_advanced_executor", + folder="/simvue_unit_testing", + tags=["simvue_client_tests", request.node.name] + ) + run.add_process( + identifier=(exe_id := "advanced_run"), + executable="python", + script=f"{code_file}", + input_file=f"{in_file}", + output_file=out_file + ) + assert run._executor._command_str[exe_id] == expected_cmd