Skip to content

Commit

Permalink
Fix default value in env variables handled like variable name (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz authored May 6, 2024
1 parent 3ccea2d commit 9db26f2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/releasenotes/unreleased/fixes.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Handle default default of environment variable in RenameVariables (#677)
------------------------------------------------------------------------

``RenameVariables`` did not handle default value of environment variable correctly. Now following code::

Set Test Variable ${local variable} %{env variable=string message}
Log %{MY_ENV=${global}}
Log %{my env=${global} with extra}

should be transformed to::

Set Test Variable ${local_variable} %{ENV_VARIABLE=string message}
Log %{MY_ENV=${GLOBAL}}
Log %{MY_ENV=${DEFAULT} with extra string}
10 changes: 9 additions & 1 deletion robotidy/transformers/RenameVariables.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,15 @@ def rename_value(self, value: str, variable_case: str, is_var: bool = False):
name += match.before
# inline eval will start and end with {}
if not (match.base.startswith("{") and match.base.endswith("}")):
base = self.rename_value(match.base, variable_case=variable_case, is_var=True)
# handle environment variable with default %{ENV=default}
if match.identifier == "%" and "=" in match.base:
base, default = match.base.split("=", maxsplit=1)
default = self.rename_value(default, variable_case=variable_case, is_var=False)
else:
base, default = match.base, ""
base = self.rename_value(base, variable_case=variable_case, is_var=True)
if default:
base = f"{base}={default}"
base = f"{match.name[:2]}{base}}}"
else:
base = match.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Path and line separators
Environment variable
Log %{APPLICATION_PORT=8080}
Log %{env_var}
Set Test Variable ${LOCAL_VARIABLE} %{ENV_VARIABLE=string message}
Log %{MY_ENV=${DEFAULT}}
Log %{MY_ENV=${DEFAULT} with extra}

True and False
IF $True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Path and line separators
Environment variable
Log %{APPLICATION_PORT=8080}
Log %{ENV_VAR}
Set Test Variable ${LOCAL_VARIABLE} %{ENV_VARIABLE=string message}
Log %{MY_ENV=${DEFAULT}}
Log %{MY_ENV=${DEFAULT} with extra}

True and False
IF $True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Path and line separators
Environment variable
Log %{application_port=8080}
Log %{env_var}
Set Test Variable ${local variable} %{env variable=string message}
Log %{MY_ENV=${default}}
Log %{my env=${DEFAULT} with extra}

True and False
IF $True
Expand Down

0 comments on commit 9db26f2

Please sign in to comment.