Skip to content

fix: allow to set all configs via tesseract build --config-override #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions tesseract_core/sdk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import click
import typer
import yaml
from jinja2 import Environment, PackageLoader, StrictUndefined
from pydantic import ValidationError as PydanticValidationError
from rich.console import Console as RichConsole
Expand Down Expand Up @@ -193,24 +194,35 @@ def main_callback(

def _parse_config_override(
options: list[str] | None,
) -> tuple[tuple[list[str], str], ...]:
) -> tuple[tuple[list[str], Any], ...]:
"""Parse `["path1.path2.path3=value"]` into `[(["path1", "path2", "path3"], "value")]`."""
if options is None:
return ()

def _parse_option(option: str):
bad_param = typer.BadParameter(
f"Invalid config override {option} (must be `keypath=value`)",
param_hint="config_override",
)
if option.count("=") != 1:
raise bad_param
def _parse_option(option: str) -> tuple[list[str], Any]:
if "=" not in option:
raise typer.BadParameter(
f'Invalid config override "{option}" (must be `keypath=value`)',
param_hint="config_override",
)

key, value = option.split("=")
if not key or not value:
raise bad_param
key, value = option.split("=", maxsplit=1)
if not re.match(r"\w[\w|\.]*", key):
raise typer.BadParameter(
f'Invalid keypath "{key}" in config override "{option}"',
param_hint="config_override",
)

path = key.split(".")

try:
value = yaml.safe_load(value)
except yaml.YAMLError as e:
raise typer.BadParameter(
f'Invalid value for config override "{option}", could not parse value as YAML: {e}',
param_hint="config_override",
) from e

return path, value

return tuple(_parse_option(option) for option in options)
Expand Down
73 changes: 73 additions & 0 deletions tests/sdk_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,76 @@ def test_bad_docker_executable_env_var():
)
assert result.returncode == 1
assert "Executable `not-a-docker` not found" in result.stderr.decode()


@pytest.mark.parametrize(
"arg_to_override",
[
"name",
"build_config.custom_build_steps",
"build_config.base_image",
"build_config.package_data",
],
)
def test_config_override(
arg_to_override, cli_runner, mocker, dummy_tesseract_location, mocked_docker
):
mocked_build = mocker.patch("tesseract_core.sdk.engine.build_tesseract")

def _run_with_override(key, value):
return cli_runner.invoke(
cli,
[
"build",
str(dummy_tesseract_location),
"--config-override",
f"{key}={value}",
"--generate-only",
],
catch_exceptions=False,
)

if arg_to_override == "name":
argpairs = (
(
"my-tesseract",
((["name"], "my-tesseract"),),
),
)
elif arg_to_override == "build_config.custom_build_steps":
argpairs = (
(
"[RUN foo='bar']",
((["build_config", "custom_build_steps"], ["RUN foo='bar'"]),),
),
(
'[RUN echo "hello world"]',
((["build_config", "custom_build_steps"], ['RUN echo "hello world"']),),
),
)
elif arg_to_override == "build_config.base_image":
argpairs = (
(
"ubuntu:latest",
((["build_config", "base_image"], "ubuntu:latest"),),
),
)
elif arg_to_override == "build_config.package_data":
argpairs = (
(
'["data/file.txt:/app/data/file.txt"]',
(
(
["build_config", "package_data"],
[("data/file.txt:/app/data/file.txt")],
),
),
),
)
else:
raise ValueError(f"Unknown arg_to_override: {arg_to_override}")

for value, expected in argpairs:
result = _run_with_override(arg_to_override, value)
assert result.exit_code == 0, result.stderr
assert mocked_build.call_args[1]["config_override"] == expected
Loading