Skip to content
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

Do specific replacement instead of a general replace for escaped dollar signs #32

Merged
merged 4 commits into from
Dec 31, 2021
Merged
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
8 changes: 7 additions & 1 deletion envyaml/envyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ def __read_yaml_file(file_path, cfg, strict, separator="|"):
# changes dictionary
replaces = dict()

shifting = 0

# iterate over findings
for entry in RE_PATTERN.finditer(content):
groups = entry.groupdict() # type: dict
Expand All @@ -235,7 +237,11 @@ def __read_yaml_file(file_path, cfg, strict, separator="|"):
default = groups["braced_default"]

elif groups["escaped"] and "$" in groups["escaped"]:
content = content.replace("$" + groups["escaped"], groups["escaped"])
span = entry.span()
content = content[:span[0] + shifting] + groups["escaped"] + content[span[1] + shifting:]
# Added shifting since every time we update content we are
# changing the original groups spans
shifting += len(groups["escaped"]) - (span[1] - span[0])

if variable is not None:
if variable in cfg:
Expand Down
5 changes: 5 additions & 0 deletions tests/env.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ test_escape:
one: $$.foo
two: $$meet
three: $${bracket}
four: SomePa$$$$$$word
five: SomePa$$$$$${word}
six: $$$PASSWORD
seven: $$${PASSWORD}
eight: $$${NON_EXISTING|DEFAULT}

key_with_slash: $ENV_WITH_SLASH
my_source_directory: "${MY_SOURCE_DIRECTORY}"
5 changes: 5 additions & 0 deletions tests/test_envyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ def test_it_should_pass_escaped_variable():
assert env["test_escape.one"] == "$.foo"
assert env["test_escape.two"] == "$meet"
assert env["test_escape.three"] == "${bracket}"
assert env["test_escape.four"] == "SomePa$$$word"
assert env["test_escape.five"] == "SomePa$$${word}"
assert env["test_escape.six"] == "$env-password-with-escape"
assert env["test_escape.seven"] == "$env-password-with-escape"
assert env["test_escape.eight"] == "$DEFAULT"


def test_it_should_properly_resolve_extra_fields():
Expand Down