coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.99k stars 385 forks source link

Pre-commit githooks #1412

Open cobasajaxinpho opened 1 year ago

cobasajaxinpho commented 1 year ago

Hi i was trying to setup a coverage checker whereby if fall under 80 %, they won't able to commit it.

somehow result specified is under 80% but still consider success. Need some suggestion is it a correct way to do that?

dotnet version: 7.0.100 OS: Mac OS ventura m1 Chip coverlet version: 3.2.0

code specified in pre-commit:

dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line rc=$? if [[ $rc != 0 ]]; then echo "${RED}Unit tests failed, please fix and push again${NC}" echo "" exit $rc fi

Result:

+-----------------+-------+--------+--------+ | Module | Line | Branch | Method | +-----------------+-------+--------+--------+ | WebApplication2 | 15.9% | 16.66% | 20% | +-----------------+-------+--------+--------+

+---------+-------+--------+--------+ | | Line | Branch | Method | +---------+-------+--------+--------+ | Total | 15.9% | 16.66% | 20% | +---------+-------+--------+--------+ | Average | 15.9% | 16.66% | 20% | +---------+-------+--------+--------+

Successfully commited.

Thank you

daveMueller commented 1 year ago

At first sight it seems correct what you are doing. Could you provide a small repro for that?

cobasajaxinpho commented 1 year ago

At first sight it seems correct what you are doing. Could you provide a small repro for that?

Hi Dave.

https://github.com/cobasajaxinpho/Testor

Can try link above for your reference. Thank you. :)

daveMueller commented 1 year ago

Thanks @cobasajaxinpho 🙏

daveMueller commented 1 year ago

OK I continued investigating this a bit today. It seems like for the msbuild driver the parameters aren't passed to the CoverageResultTask anymore if there is a .NET 7 SDK installed. This is a known issue here https://github.com/coverlet-coverage/coverlet/issues/1391. Thus the Threshold currently is always 0.

In the link are also some workarounds if you need to use msbuild driver. As alternative you could also use the global tool where the Threshold feature is also working.

grafik

cobasajaxinpho commented 1 year ago

Hi Dave,

Thanks for the response, you mean we need to install the coverlet console global version is it?

daveMueller commented 1 year ago

You can also use the msbuild driver but then you currently need to do the workaround with the environment variables. I would most likely go with this here https://github.com/coverlet-coverage/coverlet/issues/1391#issuecomment-1333080663. But I don't have much experience with Mac OS and thus I'm not sure if e.g. a simple -e:CoverletOutputFormat='lcov' will work there.

daveMueller commented 1 year ago

Hi, the issue was fixed by the microsoft guys with sdk 7.0.101 that was released last week microsoft/vstest#4014. I just checked it with your repro and it works fine. Please give it a try again.

cobasajaxinpho commented 1 year ago

just tried on mac still having the same issue. haha.. you are using mac or windows ?

daveMueller commented 1 year ago

I tried it on windows and linux. Are you sure sdk 7.0.101 is installed? Unfortunately I don't have a Mac and can't test it there.

Windows: grafik

Linux (basic docker image [mcr.microsoft.com/dotnet/sdk:7.0]): grafik

daveMueller commented 1 year ago

@cobasajaxinpho Are you still facing this issue even though the sdk is now on 7.0.202?

Bertk commented 1 month ago

Please verify whether the problem still exists using the latest coverlet 6.0.2 release.