From acc61b8c931aa588b77352a8150851e390cd1356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pekka=20Kl=C3=A4rck?= Date: Thu, 27 Oct 2022 20:54:14 +0300 Subject: [PATCH] Fix search order w/ two matches when one is from std lib. Search order needs to be used first on standard library keywords filtered only afterwards. Fixes #4516. --- atest/robot/keywords/keyword_namespaces.robot | 29 ++++++++++++++----- .../keywords/keyword_namespaces.robot | 12 ++++++++ src/robot/running/namespace.py | 4 +-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/atest/robot/keywords/keyword_namespaces.robot b/atest/robot/keywords/keyword_namespaces.robot index a19702c3601..f8b5c96cb44 100644 --- a/atest/robot/keywords/keyword_namespaces.robot +++ b/atest/robot/keywords/keyword_namespaces.robot @@ -46,12 +46,24 @@ Local keyword in resource file has precedence even if search order is set Keyword From Custom Library Overrides Keywords From Standard Library ${tc} = Check Test Case ${TEST NAME} - Verify Override Message ${ERRORS}[2] ${tc.kws[0].msgs[0]} Comment BuiltIn - Verify Override Message ${ERRORS}[3] ${tc.kws[1].msgs[0]} Copy Directory OperatingSystem + Verify Override Message ${ERRORS}[2] ${tc.kws[0]} Comment BuiltIn + Verify Override Message ${ERRORS}[3] ${tc.kws[1]} Copy Directory OperatingSystem + +Search order can give presedence to standard library keyword over custom keyword + ${tc} = Check Test Case ${TEST NAME} + Check Keyword Data ${tc.kws[1]} BuiltIn.Comment args=Used from BuiltIn + Verify Override Message ${ERRORS}[4] ${tc.kws[2]} Copy Directory OperatingSystem + +Search order can give presedence to custom keyword over standard library keyword + ${tc} = Check Test Case ${TEST NAME} + Check Keyword Data ${tc.kws[1]} MyLibrary1.Comment + Check Log Message ${tc.kws[1].msgs[0]} Overrides keyword from BuiltIn library + Check Keyword Data ${tc.kws[2]} MyLibrary1.Copy Directory + Check Log Message ${tc.kws[2].msgs[0]} Overrides keyword from OperatingSystem library Keyword From Custom Library Overrides Keywords From Standard Library Even When Std Lib Imported With Different Name ${tc} = Check Test Case ${TEST NAME} - Verify Override Message ${ERRORS}[4] ${tc.kws[0].msgs[0]} Replace String + Verify Override Message ${ERRORS}[5] ${tc.kws[0]} Replace String ... String MyLibrary2 Std With Name My With Name No Warning When Custom Library Keyword Is Registered As RunKeyword Variant And It Has Same Name As Std Keyword @@ -71,16 +83,17 @@ Keywords are first searched from test case file even if they contain dot *** Keywords *** Verify override message - [Arguments] ${error msg} ${kw msg} ${kw} ${standard} ${custom}=MyLibrary1 + [Arguments] ${error msg} ${kw} ${name} ${standard} ${custom}=MyLibrary1 ... ${std with name}= ${ctm with name}= ${std imported as} = Set Variable If "${std with name}" ${SPACE}imported as '${std with name}' ${EMPTY} ${ctm imported as} = Set Variable If "${ctm with name}" ${SPACE}imported as '${ctm with name}' ${EMPTY} ${std long} = Set Variable If "${std with name}" ${std with name} ${standard} ${ctm long} = Set Variable If "${ctm with name}" ${ctm with name} ${custom} ${expected} = Catenate - ... Keyword '${kw}' found both from a custom library '${custom}'${ctm imported as} + ... Keyword '${name}' found both from a custom library '${custom}'${ctm imported as} ... and a standard library '${standard}'${std imported as}. The custom keyword is used. - ... To select explicitly, and to get rid of this warning, use either '${ctm long}.${kw}' - ... or '${std long}.${kw}'. + ... To select explicitly, and to get rid of this warning, use either '${ctm long}.${name}' + ... or '${std long}.${name}'. Check Log Message ${error msg} ${expected} WARN - Check Log Message ${kw msg} ${expected} WARN + Check Log Message ${kw.msgs[0]} ${expected} WARN + Check Log Message ${kw.msgs[1]} Overrides keyword from ${standard} library diff --git a/atest/testdata/keywords/keyword_namespaces.robot b/atest/testdata/keywords/keyword_namespaces.robot index 2753540cf87..81a015abbbc 100644 --- a/atest/testdata/keywords/keyword_namespaces.robot +++ b/atest/testdata/keywords/keyword_namespaces.robot @@ -73,6 +73,18 @@ Keyword From Custom Library Overrides Keywords From Standard Library Comment Copy Directory +Search order can give presedence to standard library keyword over custom keyword + Set Library Search Order BuiltIn + Comment Used from BuiltIn + Copy Directory + [Teardown] Set Library Search Order + +Search order can give presedence to custom keyword over standard library keyword + Set Library Search Order MyLibrary1 + Comment + Copy Directory + [Teardown] Set Library Search Order + Keyword From Custom Library Overrides Keywords From Standard Library Even When Std Lib Imported With Different Name ${ret} = Replace String Should Be Equal ${ret} I replace nothing! diff --git a/src/robot/running/namespace.py b/src/robot/running/namespace.py index 1467ef3a350..c71823a8a94 100644 --- a/src/robot/running/namespace.py +++ b/src/robot/running/namespace.py @@ -390,9 +390,9 @@ def _get_runner_from_libraries(self, name): if len(handlers) > 1: handlers = self._select_best_matches(handlers) if len(handlers) > 1: - handlers, pre_run_message = self._filter_stdlib_handler(handlers) + handlers = self._filter_based_on_search_order(handlers) if len(handlers) > 1: - handlers = self._filter_based_on_search_order(handlers) + handlers, pre_run_message = self._filter_stdlib_handler(handlers) if len(handlers) != 1: self._raise_multiple_keywords_found(handlers, name) runner = handlers[0].create_runner(name, self.languages)