reqnroll / Reqnroll

Open-source Cucumber-style BDD test automation framework for .NET.
https://reqnroll.net
BSD 3-Clause "New" or "Revised" License
302 stars 33 forks source link

Add a reqnroll config option to not consider cucumber expressions #134

Closed jdb0123 closed 1 month ago

jdb0123 commented 1 month ago

Currently regex expressions incorrectly determined as cucumber expressions can be fixed by appending ^ and $ to the start and the end of the expression. This extra configuration aims to make it easier to only use regex expressions without having to append ^ and $.

Types of changes

Checklist:

gasparnagy commented 1 month ago

@jdb0123 Thx for considering this. I'm absolutely understand the frustration you might have with the incorrect detection of regexes, but I'm not sure we should introduce a config setting to globally disable this, for multiple reasons:

jdb0123 commented 1 month ago

Hi @gasparnagy thanks for the quick reply.

This is a one-time problem and only for existing projects and it can be easily fix by adding the ^ and $ markers. I have even provided a simple solution that can be used for changing all of them with a single search and replace in Visual Studio. (See docs.)

We run into this multiple times. We have quite a large repo of steps, all in regex structure. When steps are updated, they can become ambiguous between a cucumber or regex step. Which is only detected during runtime.

With such a global setting we would block users from iteratively moving on to Cucumber Expressions, that are now the "industry standard".

I think it would mainly enable people to make to keep using regex expressions. When they desire to (iteratively) move to cucumber expression, they can still use the ^ and $ markers to specifically mark their regex expressions (as described by you above). Which can then be slowly replaced by cucumber expressions. We would like to keep using regex expressions over cucumber expressions, due to their flexibility and since we have quite a large amount of "static analysis" test for our regex expressions.

This adds a certain complexity and maintenance overhead on the codebase of the IDE integrations. We would need to update this in the Visual Studio extension (making sure we found the right config, testing, etc.) but also for example the Visual Studio Code extension would not be compatible with that.

Could you explain which changes would be required in the extensions order to support this? Currently the only thing I could think off is the skeleton snippets. I would be more than happy to assist on making the required changes to the IDE extensions.

gasparnagy commented 1 month ago

I have made some tests with an alternative solution. If you update to Reqnroll v2.0.0, you can add a simple in-project plugin to your solution where you can reconfigure the cucumber expression detection. You can fine-tune the rules or even completely disable cucumber expressions. I have tested and it seemed to work. Could you please give it a try and let me know if that would be sufficient? If yes, I would add this to the docs.

So the only need to add this file to your Reqnroll project:

using Reqnroll.Bindings.CucumberExpressions;
using Reqnroll.Plugins;
using Reqnroll.UnitTestProvider;
using ReqnrollCalculator.Specs.Support;

[assembly:RuntimePlugin(typeof(ForceRegexPlugin))]

namespace ReqnrollCalculator.Specs.Support;

public class ForceRegexPlugin : IRuntimePlugin
{
    // ReSharper disable once ClassNeverInstantiated.Local
    private class ForceRegexDetector : ICucumberExpressionDetector
    {
        public bool IsCucumberExpression(string cucumberExpressionCandidate)
        {
            return false;
        }
    }

    public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters, UnitTestProviderConfiguration unitTestProviderConfiguration)
    {
        runtimePluginEvents.CustomizeGlobalDependencies += (_, args) =>
        {
            args.ObjectContainer.RegisterTypeAs<ForceRegexDetector, ICucumberExpressionDetector>();
        };
    }
}
jdb0123 commented 1 month ago

Hi @gasparnagy

That appears to be an elegant solution and effectively addresses our issues. I've tested it by integrating it directly into the bdd project. And also tested it by creating a separate plugin project, which is then added a reference to the bdd project. Both work as expected.

Thank you for providing an alternative solution. I would suggest to close this PR.

gasparnagy commented 1 month ago

@jdb0123 Super. Thx for the feedback. I keep this open for myself as a reminder to add this to the docs, but I will close it after.

gasparnagy commented 1 month ago

Workaround documented at https://docs.reqnroll.net/latest/guides/how-to-configure-cucumber-expression-behavior.html

jdb0123 commented 1 month ago

@gasparnagy Very minor:

In order to override the detection strategy, you need to implement a simle --> simple Reqnroll runtime plugin. In the simplest case this is just adding a C# file to your Reqnroll project.

Besides that it seems like clear documentation :+1:

gasparnagy commented 1 month ago

@jdb0123 Thx for checking. Fixed the typo.