Skip to content

BDD prefixes with same beginning are not handled properly #5340

Closed

Description

@orenault

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

  1. added this to the v7.3 milestone on Feb 27, 2025
  2. added
    acknowledge
    on Feb 27, 2025
  3. added theissue type on Feb 27, 2025
  4. changed the title [-]Intermittent BDD Prefix Matching Failures Due to Unordered Regex Construction[/-] [+]BDD prefixes with same beginning are not handled properly[/+] on Mar 7, 2025
  5. pekkaklarck commented on Mar 7, 2025

    @pekkaklarck
    Member

    This example demonstrates the problem:

    >>> re.match('(foo|foobar)', 'foobar').group(1)
    'foo'

    As you can see, (foo|foobar) matches just foo, not foobar. 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.

  6. added a commit that references this issue on Mar 7, 2025
    341ae48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgeeffort: smallpriority: mediumrc 1

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions