diff --git a/packages/service-integration/src/service_integration/cli/__init__.py b/packages/service-integration/src/service_integration/cli/__init__.py index a146de5735dd..6e8b5c6343e5 100644 --- a/packages/service-integration/src/service_integration/cli/__init__.py +++ b/packages/service-integration/src/service_integration/cli/__init__.py @@ -7,7 +7,7 @@ from .._meta import __version__ from ..settings import AppSettings -from . import _compose_spec, _metadata, _run_creator, _test +from . import _compose_spec, _escaping, _metadata, _run_creator, _test from ._config import config_app app = typer.Typer() @@ -72,6 +72,7 @@ def main( app.command("compose")(_compose_spec.create_compose) app.add_typer(config_app, name="config", help="Manage osparc config files") app.command("test")(_test.run_tests) +app.command("legacy-escape")(_escaping.legacy_escape) # legacy app.command("bump-version")(_metadata.bump_version) app.command("get-version")(_metadata.get_version) diff --git a/packages/service-integration/src/service_integration/cli/_escaping.py b/packages/service-integration/src/service_integration/cli/_escaping.py new file mode 100644 index 000000000000..2953911554cc --- /dev/null +++ b/packages/service-integration/src/service_integration/cli/_escaping.py @@ -0,0 +1,44 @@ +import re +from pathlib import Path +from typing import Annotated + +import typer + +from ..osparc_config import OSPARC_CONFIG_DIRNAME + + +def escape_dollar_brace(text: str) -> str: + # the pattern finds '$${' that is not preceded by another '$'. + pattern = r"(? str: + assert result.exception + tb_message = "\n".join(traceback.format_tb(result.exception.__traceback__)) + return f"Below exception was raised by the cli:\n{tb_message}" + + def test_cli_help(run_program_with_args: Callable): result = run_program_with_args( "--help", ) - assert result.exit_code == 0 + assert result.exit_code == os.EX_OK, _format_cli_error(result) def test_cli_version(run_program_with_args: Callable): result = run_program_with_args( "--version", ) - assert result.exit_code == 0 + assert result.exit_code == os.EX_OK, _format_cli_error(result) assert __version__ == result.output.strip() + + +@pytest.fixture +def copy_tests_data_dir(tests_data_dir: Path, tmp_path: Path) -> Path: + new_dir_path = tmp_path / "copy_tests_data_dir" + new_dir_path.mkdir(exist_ok=True, parents=True) + + for item in tests_data_dir.glob("*"): + print(f"Copying {item} to {new_dir_path / item.name}") + shutil.copy2(item, new_dir_path / item.name) + + return new_dir_path + + +def test_cli_legacy_escape(copy_tests_data_dir: Path, run_program_with_args: Callable): + result = run_program_with_args( + "legacy-escape", "--osparc-config-dirname", copy_tests_data_dir + ) + assert result.exit_code == os.EX_OK, _format_cli_error(result) + # NOTE only 1 file will have a sequence that will be escaped + assert ( + f"Escaped sequence in {copy_tests_data_dir}/docker-compose-meta.yml" + in result.output.strip() + ) diff --git a/packages/service-integration/tests/test_cli__escaping.py b/packages/service-integration/tests/test_cli__escaping.py new file mode 100644 index 000000000000..e33463b4dcd0 --- /dev/null +++ b/packages/service-integration/tests/test_cli__escaping.py @@ -0,0 +1,20 @@ +import pytest +from service_integration.cli._escaping import escape_dollar_brace + + +@pytest.mark.parametrize( + "to_escape, escaped", + [ + ("some text", "some text"), + ("$${escapes}", "$$$${escapes}"), + ("$$${preserves}", "$$${preserves}"), + ("$$$${preserves}", "$$$${preserves}"), + ("$$$$${preserves}", "$$$$${preserves}"), + ( + "$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}", + "$$$${escapes} & $$${preserves},$$$${preserves}, $$$$${preserves}", + ), + ], +) +def test_escape_dollar_brace(to_escape: str, escaped: str): + assert escape_dollar_brace(to_escape) == escaped