FortuneN / FineCodeCoverage

Visualize unit test code coverage easily for free in Visual Studio Community Edition (and other editions too)
https://marketplace.visualstudio.com/items?itemName=FortuneNgwenya.FineCodeCoverage
Other
523 stars 40 forks source link

Respect msbuild import statements when overriding options #249

Closed sadedil closed 2 years ago

sadedil commented 2 years ago

Installed product versions

Description

I've four different Test projects in my solution folder and I want to configure options with <PropertyGroup Label="FineCodeCoverage"> approach. It's working when I add this property to each *.Tests.csproj file separately. But if I use MSBuild's <Import> statement in my *.Tests.csproj files, it's not working.

I've created a FineCodeCoverage.props file in root of the /test folder and I imported this file from my *.Tests.csproj files like <Import Project="../FineCodeCoverage.props" />.

Steps to recreate

  1. Create /test/FineCodeCoverage.props file with this content:
<Project>
  <PropertyGroup Label="FineCodeCoverage">
    <Enabled>True</Enabled>
    <Exclude>
      [SomeAssembly]*
    </Exclude>
    <IncludeTestAssembly>False</IncludeTestAssembly>
  </PropertyGroup>
</Project>
  1. Create /test/MyApp.Test1/MyApp.Test1.csproj (XUnit) and add this line <Import Project="../FineCodeCoverage.props" />
  2. Create /test/MyApp.Test1/MyApp.Test2.csproj (XUnit) and add this line <Import Project="../FineCodeCoverage.props" />
  3. Build the solution
  4. See this config is not working

Current behavior

Fine Code Coverage is not considered these options

Expected behavior

Fine Code Coverage should consider these options

tonyhallett commented 2 years ago

If you can find a way to achieve this I will be happy to accept a pull request.

It is possible to get a property value with IVsBuildPropertyStorage.GetPropertyValue but there is no way of knowing if the property is from the FineCodeCoverage PropertyGroup and it is not possible to prefix each property with fcc.

As far as I remember there are issues with using Microsoft.Build.Evaluation too. I certainly do not fancy reinventing that wheel.

The alternative I propose is

FCC searches up the directory structure for fccOptions.xml. FCC to support merging from fccOptions at different levels. Have a root attribute topLevel to stop searching further up. Options that can be be merged - arrays - are merged dependent upon an attribute. Global visual studio options are also considered as part of this merge. Then project level options ( not from imported props ) override or merge with those from global / fccOptions.

Your thoughts ?

tonyhallett commented 2 years ago

This code ( within CoverageProject Settings property ) does work but I am still hesitant to use it.

                       Microsoft.Build.Evaluation.Project evaluationProject = new Microsoft.Build.Evaluation.Project(this.ProjectFile);
                        var fccProperties = evaluationProject.AllEvaluatedProperties.Where(p => {
                            var isMatch = false;
                            var projectPropertyElement = p.Xml;
                            if (projectPropertyElement != null)
                            {
                                var parent = projectPropertyElement.Parent;
                                if (parent != null)
                                {
                                    isMatch = parent.Label == "FineCodeCoverage";
                                }
                            }
                            return isMatch;
                        }).ToList();
tonyhallett commented 2 years ago

See override globals. Requires different format for props Import

github-actions[bot] commented 2 years ago

released v1.1.172 and available on marketplace