coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.97k stars 386 forks source link

Add possibility to give warning when coverage is below the threshold instead of just failing the build #701

Open roostaamir opened 4 years ago

roostaamir commented 4 years ago

Currently there is a possibility in the MSbuild tool to specify a threshold for the coverage result and if the coverage percentage is below the threshold, then the build fails

We have a requirement in our project to just give warnings when coverage is lower than the threshold (instead of failing it). Therefore, I think it could be a good idea to add an option to the threshold functionality to specify whether the build should fail or just give a warning.

MarcoRossignoli commented 4 years ago

Thank's for the advice, we'll wait to see thumb up and comment from community.

roostaamir commented 4 years ago

I'm working on a local version of the change myself. Can I do a pull request for this or should I wait for thumbs ups?

MarcoRossignoli commented 4 years ago

It depends, are you adding one more switch?

roostaamir commented 4 years ago

Yes.

MarcoRossignoli commented 4 years ago

Can you update top issue with the proposal idea?So we can talk about.

roostaamir commented 4 years ago

Sure. I will add a snippet tonight

roostaamir commented 4 years ago

Sorry for the delay. This weekend was not my best.

So here's how I wanted to tackle this problem. First I would introduce a new CommandOption for the console to accept. Something like this:

CommandOption thresholdAction = app.Option("--threshold-action", "The action to take when coverage is below the threshold value. Defaults to stop the build", CommandOptionType.SingleValue);

Then I would use an Enum to determine the actions possible, which in this case would be something like Warning and FailBuild.

I would later get the argument like this: var dThresholdAction = thresholdAction.HasValue() ? Enum.Parse<ThresholdAction>(thresholdAction.Value(), true) : Enum.Parse<ThresholdAction>("FailBuild", true);

In the end, if coverage was less than the threshold value (and of course the threshold value was specified) I would check the thresholdAction. If it was the default, I would let the process go as it is right now and throw the exception that is already throwing. However, if the action was to give warning them I would just do a logger.LogWarning() call instead

I tried to keep the naming convention and code style. But the naming and style of code could be different of course.

Let me know if this sounds plausible to you.

MarcoRossignoli commented 4 years ago

@roostaamir looks fine to me, if you work on this you should also add same new switch to msbuild version https://github.com/tonerdo/coverlet/blob/master/src/coverlet.msbuild.tasks/CoverageResultTask.cs#L214

roostaamir commented 4 years ago

@MarcoRossignoli I'm finished with my pull request about this: https://github.com/tonerdo/coverlet/pull/720

The only thing I did not know about was the versioning of the package. Please lift the version however you see fit :)

Danny-UKDM commented 2 years ago

did this make it in? Would love to see this capability

daveMueller commented 2 years ago

@Danny-UKDM No up to now this didn't make it in. But every contribution is welcome. So you or someone else can reopen the PR and continue.

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 3 months with no activity.

roostaamir commented 2 months ago

Hello after 4 years! :D

I saw that I am getting notifications about this thread and I remembered that I totally forgot about this feature I was working on. I think my last attempt was before Covid, which I think was the main reason I dropped and forgot about this feature.

I will pick the development of the feature up again and update the PR or create a whole new one, depending on how the code is changed during these years