You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatically select "best" match if there is conflict with keywords using embedded arguments #4454
Currently the following test (adapted from an example in a comment to #4069) fails:
*** Test Cases ***Example
Execute "x" on device "y"
*** Keywords ***Execute "${command}"
Log Executing ${command}.
Execute "${command}" on device "${device}"
Log Executing ${command} on ${device}.
The reason for the failure is that Execute "x" on device "y" matches both of the keyword implementations. A solution to this problem, recommended in the User Guide, has been using custom patterns that limit what keywords match. For example, changing the first keyword to Execute "${command:[^"]+}" would mitigate the issue.
Issue #4069 proposes making custom patterns more strict so that they would be validated also when the argument is given as a variable. As the comment referenced above explains, this unfortunately causes problems if patterns have been used for resolving conflicts as explained above.
A solution to the problem caused by conflicts, that would also allow us making custom pattern validation more strict, would be Robot somehow being able to detect what is the "best" match. For a human it is pretty clear that if you use Execute "x" on device "y", Execute "${command}" on device "${device}" matches better than Execute "${command}", but the problem is how Robot can detect that. It's also a question that can it always detect it right or could there be false positives. I have one pretty simple approach in mind that I explain in a separate comment. Other approaches can also be proposed!
The simplest solution I can think of, that also ought to work, is checking which of the matched keywords has longest "non argument" part. That part must match the used keyword literally, and if the part is longer then the match can also be considered better. For example, Execute "${command}" has 10 characters that aren't part of the arguments while Execute "${command}" on device "${device}" has 23. In this case it would thus be easy to tell that the latter keyword matches better. Do others see counter examples where this simple algorithm wouldn't work right?
A benefit of this solution would be that it is simple to implement. In fact I already have a prototype done and could easily finish it still in RF 5.1 timeline. I'm mostly worried if the solution good enough and how big risk there is for false positives.
Here's one example where the algorithm I proposed above doesn't work too well. I would say that If / else matches both implementations equally well and using it should cause an error. With the proposed algorithm it would match the first implementation because ${if} / else has 7 characters that aren't part of arguments while If / ${else} has only 5. I need to think could this situation be detected without making the algorithm overly complicated.
*** Test Cases ***Example
x / else
If / y
If / else
*** Keywords ***${if} / else
Log to console if: ${if}If / ${else}
Log to console else: ${else}
I got another idea how to select the best match: we could match candidates against each others directly. If we have candidates A and B, then A would be considered a better match than B if A's name matches B but B's name doesn't match A. If both match each others or neither match, then matches are equally good. If there's a single candidate that's better than all others, then we use that and otherwise we report an error.
As an example, Execute "${command}" on device "${device}" matches Execute "${command}", but not the other way around. Thus the example in the original description succeeds. As another example, ${if} / else and If / ${else} match each others and are considered equally good. Thus If / else fails in the above example fails as it should.
Based on my testing the above algorithm seems to work well. Need to still think are there examples where it wouldn't. To be useful, it wouldn't even need to be able to detect all cases where a match is better than others, but producing false positives wouldn't be good. Also, if not being able to find the best match would be relatively common, we probably could make custom regexp validation more strict.
Are you @oboehmer interested to test this in your environment? The algorithm explained is pretty simple. I'll paste the diff here, but I can also either create branch or, if we agree this looks promising, just commit it to master.
diff --git a/src/robot/running/handlerstore.py b/src/robot/running/handlerstore.py
index 8a4e7e35b..6c9c62c67 100644
--- a/src/robot/running/handlerstore.py+++ b/src/robot/running/handlerstore.py@@ -68,8 +68,21 @@ class HandlerStore:
embedded = [template for template in self._embedded if template.matches(name)]
if len(embedded) == 1:
return embedded[0]
+ for candidate in embedded:+ if self._is_best_embedded_match(candidate, embedded):+ return candidate
self._raise_no_single_match(name, embedded)
+ def _is_best_embedded_match(self, candidate, alternatives):+ # Match is considered better than another match if it doesn't match+ # the other but the other matches it.+ for other in alternatives:+ if candidate is other:+ continue+ if other.matches(candidate.name) or not candidate.matches(other.name):+ return False+ return True+
def _raise_no_single_match(self, name, found):
if self.source_type == self.TEST_CASE_FILE_TYPE:
source = self.source_type
thanks for looking into this, @pekkaklarck .. I performed some initial tests which look very promising, currently running our full test suite with this patch and with [^]+ regex removed from all of our keywords.. will report status later today..
@pekkaklarck , something doesn't look right.. I do not get any more duplicate keyword errors, but the logic doesn't select the expected keyword:
*** Variables ***
${device} nso_mock
*** Test Cases ***
test 1
execute "show software | display json" on device "${device}"
test 2
${c}= Set Variable show software | display json
execute "${c}" on device "${device}"
test 3
execute "foo" on device "bar"
*** Keywords ***
execute "${command}"
Fail this should not be called
execute "${command}" on device "${device}"
Log this should be called
all tests fail:
robot /tmp/test.robot
==============================================================================
Test
==============================================================================
test 1 | FAIL |
this should not be called
------------------------------------------------------------------------------
test 2 | FAIL |
this should not be called
------------------------------------------------------------------------------
test 3 | FAIL |
this should not be called
------------------------------------------------------------------------------
Test | FAIL |
3 tests, 0 passed, 3 failed
==============================================================================
makes the above tests succeed and hopefully works fine also otherwise.
This is what you get when you don't have proper automated tests. I just had some example tests and I validated their results manually. Apparently I didn't look at what they actually did after I changed the implementation. Need to create tests that validate that results are as expected.
@pekkaklarck , I wonder if we can also resolve this use case:
$ cat test3.robot
*** Settings ***
Resource res1.robot
Resource res2.robot
*** Variables ***
${device} foo
*** Test Cases ***
test
disconnect from device "${device}" with alias "test_connection"
$ cat res1.robot
# res1.robot
*** Keywords ***
disconnect from device "${name}"
Fail this should not be executed
$
$ cat res2.robot
# res2.robot
*** Keywords ***
disconnect from device "${device}" with alias "${alias}"
Log this should be executed
$
executing the test suite results in a Multiple keywords warning:
Multiple keywords with name 'disconnect from device "${device}" with alias "test_connection"' found. Give the full name of the keyword you want to use:
res1.disconnect from device "${device}" with alias "test_connection"
res2.disconnect from device "${device}" with alias "test_connection"
if I put in a search order to prefer res1 (which is actually part of our use case, so this setup is not artificial), the test picks the keyword in res1, which is not the right one:
$ cat test3.robot
*** Settings ***
Resource res1.robot
Resource res2.robot
Suite Setup Set Library Search Order res1
*** Variables ***
${device} foo
*** Test Cases ***
test
disconnect from device "${device}" with alias "test_connection"
$ robot test3.robot
==============================================================================
Test3
==============================================================================
test | FAIL |
this should not be executed
------------------------------------------------------------------------------
Test3 | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Note: Putting the keywords into the same resource file works, I only observe the problem if the "overlapping" keywords are spread across multiple resource files or libraries (in our environment I observed this for overlapping kws defined in different Libraries):
$ cat test4.robot
*** Settings ***
Resource res3.robot
*** Variables ***
${device} foo
*** Test Cases ***
test
disconnect from device "${device}" with alias "test_connection"
$ cat res3.robot
# res3.robot
*** Keywords ***
disconnect from device "${name}"
Fail this should not be executed
disconnect from device "${device}" with alias "${alias}"
Log this should be executed
$
$ robot test4.robot
==============================================================================
Test4
==============================================================================
test | PASS |
------------------------------------------------------------------------------
Test4 | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Conflicts with embedded args are at the moment only looked at in a single file. I agree it would be better to handle that also if kws are in different files. Need to check how easy/hard that would be.
The case with search order is a bit different. If you explicitly give precedence to s certain lib/resource and it contains a matching keyword, I think using it without looking others makes sense. How is that currently handled without patches?
Both of these cases can also be resolved by prefixing the keyword with the lib/resource name.
Well, I would expect the logic to work across libs/files, so the conflict which would require search order (or prefix) would not occur at all.. will check how this case is handled without the patch.. reckon it would be the same..
Probably doable but I don't have time to look at it right now. The search order is looked at there as well, so if this conflict is handled first then better match wins even if the search order is set. I'm still not 100% is that a good idea, though.
If you want to test this with #4069 reverted (or disabled to be more precise), you can apply this trivial diff:
diff --git a/src/robot/running/arguments/embedded.py b/src/robot/running/arguments/embedded.py
index 9c7b2f6cf..e37c9e509 100644
--- a/src/robot/running/arguments/embedded.py+++ b/src/robot/running/arguments/embedded.py@@ -39,6 +39,7 @@ class EmbeddedArguments:
return list(zip(self.args, values))
def validate(self, values):
+ return
if not self.custom_patterns:
return
for arg, value in zip(self.args, values):
I'll do it like that myself but I'll add a comment there explaining it as well. I have that already done, but I also need to update tests and docs before committing it. No time for that now.
Selecting the best match was implemented in 95881a2. The conflict resolution order is the one discussed above.
I'm pretty happy how this works now. Docs still need to be updated and I'd also like to hear feedback from @oboehmer about the implementation before closing this.
It looks the latest commit now allows us to drop all of the [^"]+ regexps from our keywords, which are scattered around multiple libraries, partially with keywords overlapping across libraries. This really allows us to prepare for the #4069 enforcement once, and I trust we can now safely introduce it in 5.2 or 6.0, whenever..It would be good to announce this new matching capability quite loudly in the release notes so others can also prepare their libs accordingly..
thanks so much @pekkaklarck for addressing this so quickly and adjusting the algorithm!
I set high priority to this issue exactly to get it mentioned in the release notes. This enhancement makes using keywords with embedded arguments quite a bit easier regardless would #4069 affect you or not. Still need to update examples in the User Guide accordingly!
The section about embedded arguments in the User Guide was pretty bad in general and I ended up rewriting it pretty much totally. Great to have this issue closed now!
Activity
pekkaklarck commentedon Sep 7, 2022
The simplest solution I can think of, that also ought to work, is checking which of the matched keywords has longest "non argument" part. That part must match the used keyword literally, and if the part is longer then the match can also be considered better. For example,
Execute "${command}"has 10 characters that aren't part of the arguments whileExecute "${command}" on device "${device}"has 23. In this case it would thus be easy to tell that the latter keyword matches better. Do others see counter examples where this simple algorithm wouldn't work right?A benefit of this solution would be that it is simple to implement. In fact I already have a prototype done and could easily finish it still in RF 5.1 timeline. I'm mostly worried if the solution good enough and how big risk there is for false positives.
pekkaklarck commentedon Sep 7, 2022
Here's one example where the algorithm I proposed above doesn't work too well. I would say that
If / elsematches both implementations equally well and using it should cause an error. With the proposed algorithm it would match the first implementation because${if} / elsehas 7 characters that aren't part of arguments whileIf / ${else}has only 5. I need to think could this situation be detected without making the algorithm overly complicated.pekkaklarck commentedon Sep 8, 2022
I got another idea how to select the best match: we could match candidates against each others directly. If we have candidates A and B, then A would be considered a better match than B if A's name matches B but B's name doesn't match A. If both match each others or neither match, then matches are equally good. If there's a single candidate that's better than all others, then we use that and otherwise we report an error.
As an example,
Execute "${command}" on device "${device}"matchesExecute "${command}", but not the other way around. Thus the example in the original description succeeds. As another example,${if} / elseandIf / ${else}match each others and are considered equally good. ThusIf / elsefails in the above example fails as it should.pekkaklarck commentedon Sep 8, 2022
Based on my testing the above algorithm seems to work well. Need to still think are there examples where it wouldn't. To be useful, it wouldn't even need to be able to detect all cases where a match is better than others, but producing false positives wouldn't be good. Also, if not being able to find the best match would be relatively common, we probably could make custom regexp validation more strict.
Are you @oboehmer interested to test this in your environment? The algorithm explained is pretty simple. I'll paste the diff here, but I can also either create branch or, if we agree this looks promising, just commit it to master.
oboehmer commentedon Sep 8, 2022
thanks for looking into this, @pekkaklarck .. I performed some initial tests which look very promising, currently running our full test suite with this patch and with
[^]+regex removed from all of our keywords.. will report status later today..oboehmer commentedon Sep 8, 2022
@pekkaklarck , something doesn't look right.. I do not get any more duplicate keyword errors, but the logic doesn't select the expected keyword:
all tests fail:
pekkaklarck commentedon Sep 8, 2022
Thanks a lot for testing @oboehmer and sorry for the stupid logic bug. Changing
to
makes the above tests succeed and hopefully works fine also otherwise.
This is what you get when you don't have proper automated tests. I just had some example tests and I validated their results manually. Apparently I didn't look at what they actually did after I changed the implementation. Need to create tests that validate that results are as expected.
oboehmer commentedon Sep 9, 2022
@pekkaklarck , I wonder if we can also resolve this use case:
executing the test suite results in a
Multiple keywordswarning:if I put in a search order to prefer res1 (which is actually part of our use case, so this setup is not artificial), the test picks the keyword in res1, which is not the right one:
Note: Putting the keywords into the same resource file works, I only observe the problem if the "overlapping" keywords are spread across multiple resource files or libraries (in our environment I observed this for overlapping kws defined in different Libraries):
pekkaklarck commentedon Sep 9, 2022
Conflicts with embedded args are at the moment only looked at in a single file. I agree it would be better to handle that also if kws are in different files. Need to check how easy/hard that would be.
The case with search order is a bit different. If you explicitly give precedence to s certain lib/resource and it contains a matching keyword, I think using it without looking others makes sense. How is that currently handled without patches?
Both of these cases can also be resolved by prefixing the keyword with the lib/resource name.
oboehmer commentedon Sep 9, 2022
Well, I would expect the logic to work across libs/files, so the conflict which would require search order (or prefix) would not occur at all.. will check how this case is handled without the patch.. reckon it would be the same..
pekkaklarck commentedon Sep 10, 2022
Conflicts across files would need to be handled here:
robotframework/src/robot/running/namespace.py
Line 341 in c87a715
Probably doable but I don't have time to look at it right now. The search order is looked at there as well, so if this conflict is handled first then better match wins even if the search order is set. I'm still not 100% is that a good idea, though.
pekkaklarck commentedon Sep 10, 2022
If you want to test this with #4069 reverted (or disabled to be more precise), you can apply this trivial diff:
I'll do it like that myself but I'll add a comment there explaining it as well. I have that already done, but I also need to update tests and docs before committing it. No time for that now.
8 remaining items
pekkaklarck commentedon Sep 15, 2022
Selecting the best match was implemented in 95881a2. The conflict resolution order is the one discussed above.
I'm pretty happy how this works now. Docs still need to be updated and I'd also like to hear feedback from @oboehmer about the implementation before closing this.
Use "normal" match over embedded matches in case of conflicts.
Resolve another conflict with embedded args (#4454).
pekkaklarck commentedon Sep 16, 2022
Huge thanks to @oboehmer for testing this and reporting a problem related to search order. That ought to be fixed by the commit above.
oboehmer commentedon Sep 16, 2022
It looks the latest commit now allows us to drop all of the
[^"]+regexps from our keywords, which are scattered around multiple libraries, partially with keywords overlapping across libraries. This really allows us to prepare for the #4069 enforcement once, and I trust we can now safely introduce it in 5.2 or 6.0, whenever..It would be good to announce this new matching capability quite loudly in the release notes so others can also prepare their libs accordingly..thanks so much @pekkaklarck for addressing this so quickly and adjusting the algorithm!
Test that exact match wins over embedded matches regardless search or…
pekkaklarck commentedon Sep 16, 2022
I set high priority to this issue exactly to get it mentioned in the release notes. This enhancement makes using keywords with embedded arguments quite a bit easier regardless would #4069 affect you or not. Still need to update examples in the User Guide accordingly!
pekkaklarck commentedon Sep 21, 2022
The section about embedded arguments in the User Guide was pretty bad in general and I ended up rewriting it pretty much totally. Great to have this issue closed now!
pekkaklarck commentedon Jun 7, 2023
As explained in #4609, It turned out that using the search order only as the last resort wasn't a good idea. In RF 6.1 it will be used first.