robotframework-tidy icon indicating copy to clipboard operation
robotframework-tidy copied to clipboard

Enhancement: RenameVariables transformer

Open oboehmer opened this issue 2 years ago • 2 comments

We have a requirement to bulk-replace variables, and wondered if we can leverage Robotidy for this.. Simplified test case shown below, our goal would be to replace the variables OLD_SCALAR by NEW_SCALAR, OLD by NEW, OLD_DICT by NEW_DICT, and also OLD_DICT_2 by NEW_DICT_2, but also its keys old_key1 by new_key1.. Obviously this example could also be achieved through regexp substitution, but we would like to explore if Robotidy could do it "better" and "more robust" than a potentially complex regexp.

Really appreciate you looking into this!



*** Test Cases ***
Test
    Import Variables    ${CURDIR}/vars.yaml
    Log Variables
    FOR   ${var}   IN   @{OLD}
        Log   ${var}
    END

    Should Be True   ${OLD_SCALAR}
    Should Be True   ${OLD}
    Should be True   ${OLD}[1]
    Should be True   ${OLD_DICT.a}
    Should be True   ${OLD_DICT}[a]
    Should be True   ${OLD_DICT_2.old_key1}
    Should be True   ${OLD_DICT_2}[old_key1]

    Do something with ${OLD}
    Do something with ${OLD}[1]
    Do something with ${OLD_DICT.a}
    Do something with ${OLD_DICT}[a]
    Do something with ${OLD_DICT_2.old_key1}
    Do something with ${OLD_DICT_2}[old_key1]

*** Keywords ***
Do something with ${var}
    Log   ${var}

vars.yaml content:

OLD_SCALAR: 1

OLD:
- 1
- 2
- 3

OLD_DICT:
  a: 1
  b: 1
  c: 1

OLD_DICT_2:
  old_key1: 1
  old_key2: 2

oboehmer avatar Oct 29 '22 17:10 oboehmer

I apologize for the late reply. This feature wasn't possible before but now with #502 in open PRs we will have all tools available to implement it (new transformer is named RenameVariables but does not rename it like you want, just normalize the name and the scope of variable). After this time you probably don't need it anymore but it may be useful in the future (and also for the others) so I will work on that in the future releases.

It will work similarly to RenameKeywords/RenameTestCases. However I'm thinking about adding reading from the file to allow bulk renaming.

You mentioned vars.yaml file, do you also expect to have your variables renamed here? It could be more tricky than just renaming variables in the robot files.

bhirsz avatar Apr 03 '23 07:04 bhirsz

Hi @bhirsz ! Thanks for getting back to me.. I am still looking for this capability, so still looking forward to it ;-)

I only included the vars.yaml to illustrate what the variable values for my examples would be.. Of course I would not expect tidy to touch .yaml files ;-)

oboehmer avatar Apr 03 '23 11:04 oboehmer