Repository navigation
Description
Description:
When defining custom language prefixes for BDD steps in the Robot Framework (for example, in a French language module), I observed that adding multiple prefixes with overlapping substrings (e.g., "Sachant que" and "Sachant") leads to intermittent failures in matching the intended BDD step.
Steps to Reproduce:
Create a custom language (e.g., a French language class) inheriting from Language.
Define given_prefixes with overlapping entries such as:
given_prefixes = ['Étant donné', 'Soit', 'Sachant que', 'Sachant']
Run tests that rely on detecting BDD step prefixes.
Occasionally, tests fail with errors indicating that the keyword "Sachant que" cannot be found, even though it is defined.
Expected Behavior:
The regex built from the BDD prefixes should consistently match the longer, more specific prefix ("Sachant que") instead of prematurely matching the shorter prefix ("Sachant").
Observed Behavior:
Due to the unordered nature of Python sets, the bdd_prefixes property (which is constructed using a set) produces a regex with the alternatives in an arbitrary order. This sometimes results in the shorter prefix ("Sachant") matching before the longer one ("Sachant que"), causing intermittent failures.
Potential Impact:
Unpredictable test failures when multiple BDD prefixes with overlapping substrings are used.
Difficulty in debugging issues due to the non-deterministic nature of the problem.
Suggested Fix:
Sort the BDD prefixes by descending length before constructing the regular expression. For example:
@property
def bdd_prefix_regexp(self):
if not self._bdd_prefix_regexp:
# Sort prefixes by descending length so that the longest ones are matched first
prefixes = sorted(self.bdd_prefixes, key=len, reverse=True)
pattern = '|'.join(prefix.replace(' ', r'\s') for prefix in prefixes).lower()
self._bdd_prefix_regexp = re.compile(rf'({pattern})\s', re.IGNORECASE)
return self._bdd_prefix_regexp
This change would ensure that longer, more specific prefixes are matched before their shorter substrings, eliminating the intermittent failure.
Environment:
Robot Framework version: 7.1.1
Python version: 3.11.5
Additional Notes:
This bug appears to be caused by the unordered nature of sets in Python, which is used in the bdd_prefixes property to combine all prefixes. The issue might not manifest when there are only three prefixes or when there is no overlapping substring scenario.
I hope this detailed report helps in reproducing and resolving the issue.
Activity
[-]Intermittent BDD Prefix Matching Failures Due to Unordered Regex Construction[/-][+]BDD prefixes with same beginning are not handled properly[/+]pekkaklarck commentedon Mar 7, 2025
This example demonstrates the problem:
As you can see,
(foo|foobar)matches justfoo, notfoobar. The latter match would be "better", but I guess the regexp engine is happy when it finds a match and doesn't look for other candidates.Test that BDD prefixes are sorted by length
Update French BDD prefixes