Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -76,3 +76,4 @@ def main(
app.command("bump-version")(_metadata.bump_version)
app.command("get-version")(_metadata.get_version)
app.command("run-creator")(_run_creator.run_creator)
app.command("legacy-escape")(_escaping.legacy_escape)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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:
"""
Replaces all '$${' sequences with '$$$${' unless they are part of an
existing '$$$${' sequence.
Args:
text: The input string.
Returns:
The modified string. This function will NOT return None.
"""
# The pattern finds '$${' that is not preceded by another '$'.
pattern = r"(?<!\$)\$\${"
replacement = "$$$${"

return re.sub(pattern, replacement, text)


def legacy_escape(
osparc_config_dirname: Annotated[
Path,
typer.Option(
"--osparc-config-dirname",
help="Path to where the .osparc configuration directory is located",
),
] = Path(OSPARC_CONFIG_DIRNAME),
):
"""Escapes the `$${` with `$$$${` in all .y*ml files in the osparc config directory."""

if not osparc_config_dirname.exists():
msg = "Invalid path to metadata file or folder"
raise typer.BadParameter(msg)

for file in osparc_config_dirname.rglob("*.y*ml"):
read_text = file.read_text()
replaced_text = escape_dollar_brace(read_text)
if read_text != replaced_text:
print(f"Escaped sequence in {file}")
file.write_text(replaced_text)
42 changes: 40 additions & 2 deletions packages/service-integration/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
# pylint:disable=redefined-outer-name

import os
import shutil
import traceback
from collections.abc import Callable
from pathlib import Path

import pytest
from click.testing import Result
from service_integration import __version__


def _format_cli_error(result: Result) -> 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()
)
20 changes: 20 additions & 0 deletions packages/service-integration/tests/test_cli__escaping.py
Original file line number Diff line number Diff line change
@@ -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
Loading