Skip to content

Commit

Permalink
Allowed spaces in variable resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
tombaeyens committed Feb 24, 2025
1 parent 6a84972 commit 8dfb222
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion soda-core/src/soda_core/cli/soda.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def main():
print(dedent("""
__| _ \| \ \\
\__ \ ( | | _ \\
____/\___/___/_/ _\\ CLI 4.0.0b1
____/\___/___/_/ _\\ CLI 4.0.0.dev??
""").strip("\n"))

cli_parser = argparse.ArgumentParser(epilog="Run 'soda {command} -h' for help on a particular soda command")
Expand Down
10 changes: 5 additions & 5 deletions soda-core/src/soda_core/common/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,10 @@ def read_value(
required: bool = False,
default_value=None,
) -> object | None:
if key in self.yaml_dict:
value = self.yaml_dict.get(key)
elif env_var is not None and env_var in os.environ:
if env_var is not None and env_var in os.environ:
value = os.environ[env_var]
elif key in self.yaml_dict:
value = self.yaml_dict.get(key)
else:
env_var_text: str = f"or environment variable '{env_var}' " if isinstance(env_var, str) else ""
if required:
Expand Down Expand Up @@ -417,10 +417,10 @@ def to_list(self) -> list:
class VariableResolver:

@classmethod
def resolve(cls, source_text_with_variables: str, variables: dict[str, str] | None) -> str:
def resolve(cls, source_text_with_variables: str, variables: dict[str, str] | None = None) -> str:
if isinstance(source_text_with_variables, str):
return re.sub(
pattern=r"\$\{([a-zA-Z_][a-zA-Z_0-9]*)\}",
pattern=r"\$\{ *([a-zA-Z_][a-zA-Z_0-9]*)\ *}",
repl=lambda m: cls._resolve_variable_pattern(variables=variables, variable=m.group(1).strip()),
string=source_text_with_variables,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from textwrap import dedent

from soda_core.common.yaml import VariableResolver


def test_variable():
src=dedent("""
host: ${HOST}
""").strip()

result = VariableResolver.resolve(source_text_with_variables=src, variables={"HOST": "test.soda.io"})
assert "host: test.soda.io" == result


def test_variable_with_spaces():
src=dedent("""
host: ${ HOST }
""").strip()

result = VariableResolver.resolve(source_text_with_variables=src, variables={"HOST": "test.soda.io"})
assert "host: test.soda.io" == result


def test_variable_env_var(env_vars: dict):
src=dedent("""
host: ${HOST}
""").strip()

env_vars["HOST"] = "test.soda.io"

result = VariableResolver.resolve(source_text_with_variables=src)
assert "host: test.soda.io" == result


def test_variable_not_found():
src=dedent("""
host: ${HOST}
""").strip()

result = VariableResolver.resolve(source_text_with_variables=src)
assert "host: ${HOST}" == result

0 comments on commit 8dfb222

Please sign in to comment.