robotframework-robocop
robotframework-robocop copied to clipboard
Create a rule checking whether the name of a variable is always written the same way
This could be a sort of extension of rule possible-variable-overwriting.
It need not be obvious that variables e.g. ${abc}
and ${A_B C}
are the same. Imagine the situation: someone sees variable ${MY VAR}
in a foreign code. They want to check occurences of the variable to discover where it is used and for what purpose etc. but they are not able to find all its occurences because the same variable is named e.g. ${MY_VAR}
somewhere. In this case it is almost impossible to find all occurences of the variable by standard means: AFAIK, Robot language server cannot do it; and standard search for a string can usualy ignore the case of letters, but cannot ignore mismatch in spaces and underscores. The only way I consider to be usable for search for all the possible forms of the variable is regular expression [mM][_ ]?[yY][_ ]?[vV][_ ]?[aA][_ ]?[rR]
. Ugh.
My suggestion: allow the only form of the name of a variable. It would be probably the best to check whether the variable is named everywhere like at its first occurence (since this will probably be its definition or first setting of its value).
Hey @MoreFamed,
I think that this is how the rule works now - it looks for any variable that looks visually differently but is exactly the same variable (as read by Robot Framework) and it reports a rule violation on each NEXT occurrence of this variable. Which means that if you have ${my var}
and then ${MY_VAR}
it will report only on ${MY_VAR}
. Also, it only checks for an overwriting inside test case or a keyword. Unfortunately, we would not implement a more complex handling of this situation with variables coming from imported files or from higher scope, because we would need to implement context reading and remembering all variables with a reference where they appeared. Is that what you asked for? If not, please elaborate more.
Unluckily, this is how RF works and it's not perfect that it allows so much freedom when comes to the variables naming, but thanks to that, we can write tests, that are closer to the natural language than any other programming language.
Thanks. I understand difficulties of implementation of context awareness. I did not know what could be done in Robocop easily... It's a pity.
However, what about extending the rule so that it would report all usages of a variable under a different name, not only assignement to it? E.g. now, this situation is reported:
${ABC} Set Variable xyz
${abc} Set Variable 123
and I consider reporting even this situation:
${ABC} Set Variable xyz
Log ${abc}
But there is a "conceptual" difference: in the first case the author probably doesn't know that those variables are the same; in the second one they write one variable in a sloppy way. I don't know whether the second case occurs often, so whether extending the rule is worth the work...
If you do not like my idea, please, close this issue.
That type of check (within scope of given keyword/test case, at most reading from suite variables) can be done. But it would be then different rule - or we would need to rename this rule. For example to "inconsistent-variable-name" or similar.
Yeah, this can be done. I think we can just create a new rule so that we will have a more precise description of it.