From 9db26f280037a12ddf7f62230112b0d7228a63fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Hirsz?= Date: Mon, 6 May 2024 11:14:08 +0200 Subject: [PATCH] Fix default value in env variables handled like variable name (#684) --- docs/releasenotes/unreleased/fixes.1.rst | 14 ++++++++++++++ robotidy/transformers/RenameVariables.py | 10 +++++++++- .../expected/configure_ignore_vars.robot | 3 +++ .../RenameVariables/expected/excluded_vars.robot | 3 +++ .../RenameVariables/source/excluded_vars.robot | 3 +++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 docs/releasenotes/unreleased/fixes.1.rst diff --git a/docs/releasenotes/unreleased/fixes.1.rst b/docs/releasenotes/unreleased/fixes.1.rst new file mode 100644 index 00000000..5c3d4a09 --- /dev/null +++ b/docs/releasenotes/unreleased/fixes.1.rst @@ -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} diff --git a/robotidy/transformers/RenameVariables.py b/robotidy/transformers/RenameVariables.py index e46797f6..48e8b62b 100644 --- a/robotidy/transformers/RenameVariables.py +++ b/robotidy/transformers/RenameVariables.py @@ -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 diff --git a/tests/atest/transformers/RenameVariables/expected/configure_ignore_vars.robot b/tests/atest/transformers/RenameVariables/expected/configure_ignore_vars.robot index be3cd288..af676924 100644 --- a/tests/atest/transformers/RenameVariables/expected/configure_ignore_vars.robot +++ b/tests/atest/transformers/RenameVariables/expected/configure_ignore_vars.robot @@ -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 diff --git a/tests/atest/transformers/RenameVariables/expected/excluded_vars.robot b/tests/atest/transformers/RenameVariables/expected/excluded_vars.robot index cf95ba40..15caa203 100644 --- a/tests/atest/transformers/RenameVariables/expected/excluded_vars.robot +++ b/tests/atest/transformers/RenameVariables/expected/excluded_vars.robot @@ -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 diff --git a/tests/atest/transformers/RenameVariables/source/excluded_vars.robot b/tests/atest/transformers/RenameVariables/source/excluded_vars.robot index 0cddcc9e..f3107453 100644 --- a/tests/atest/transformers/RenameVariables/source/excluded_vars.robot +++ b/tests/atest/transformers/RenameVariables/source/excluded_vars.robot @@ -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