Files
dotnet-runtime/eng/testing/coverage.targets
Viktor Hofer 68c5658b93 Stop reading TargetFramework prop in props files (#52897)
* Stop reading TargetFramework prop in props files

The TargetFramework property isn't expected to be set in props files
before a project's body is evaluated.

Don't let BuildTargetFramework property fallback to TargetFramework as
BTF's sole intent is to convey the TargetFramework to filter to and not
the current selected TargetFramework. Reduce usage of BTF so that it is
only used in places where code is actually conditioned on filtering.

In addition to that, specify BuildTargetFramework as a global property for
all traversal builds so when invoking one of the traversal projects directly
(src.proj, ref.proj, etc.), BuildTargetFramework doesn't need to specified
manually to get the default behavior (filter on compatible to net6.0).
Remove the inferred BuildingNetCoreAppVertical because of that and
inline its meaning.

Make sendtohelix a normal NoTargets proj so that it has access to the
properties which were moved into the Directory.Build.targets file as
otherwise it wouldn't import that file.
2021-05-19 20:04:01 +02:00

90 lines
5.5 KiB
XML

<Project>
<Target Name="SetupCoverageFilter">
<!--
We need to filter the data to only the assembly being tested. Otherwise we will gather tons of data about other assemblies.
If the code being tested is part of the runtime itself, it requires special treatment.
-->
<PropertyGroup Condition="'$(AssemblyBeingTested)' == '' and '$(CreateIntermediateRunSettingsFile)' != 'true'">
<_ProjectDirectoryUnderSourceDir>$(MSBuildProjectDirectory.SubString($(LibrariesProjectRoot.Length)))</_ProjectDirectoryUnderSourceDir>
<AssemblyBeingTested>$(_ProjectDirectoryUnderSourceDir.SubString(0, $(_ProjectDirectoryUnderSourceDir.IndexOfAny("\\/"))))</AssemblyBeingTested>
</PropertyGroup>
<!--
By default, code coverage data is only gathered for the assembly being tested.
CoverageAssemblies can be passed in to the build to gather coverage on additional assemblies.
-->
<ItemGroup>
<CoverageInclude Include="$(AssemblyBeingTested)" />
<CoverageInclude Include="System.Private.CoreLib" Condition="'$(TestRuntime)' == 'true'" />
<CoverageInclude Include="@(AssembliesBeingTested)" />
<CoverageInclude Include="$(CoverageAssemblies)" Condition="'$(CoverageAssemblies)' != ''" />
</ItemGroup>
<PropertyGroup>
<CoverageIncludeFilter>@(CoverageInclude -> '[%(Identity)]*', ',')</CoverageIncludeFilter>
</PropertyGroup>
<PropertyGroup Condition="'@(CoverageExcludeByFile)' != ''">
<CoverageExcludeByFileFilter>@(CoverageExcludeByFile -> '%(Identity)', ',')</CoverageExcludeByFileFilter>
</PropertyGroup>
<PropertyGroup Condition="'@(CoverageIncludeDirectory)' != ''">
<CoverageIncludeDirectoryFilter>@(CoverageIncludeDirectory -> '$(NetCoreAppCurrentTestHostPath)%(Identity)', ',')</CoverageIncludeDirectoryFilter>
</PropertyGroup>
</Target>
<!-- TODO remove when https://github.com/coverlet-coverage/coverlet/issues/834 is fixed. -->
<Target Name="AddCoverageCommand"
BeforeTargets="GenerateRunScript"
DependsOnTargets="SetupCoverageFilter"
Condition="'$(Coverage)' == 'true' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<PropertyGroup>
<CoverageOutputPath Condition="'$(CoverageOutputPath)' == ''">coverage.opencover.xml</CoverageOutputPath>
<CoverageReportInputPath Condition="'$(CoverageReportInputPath)' == ''">$(CoverageOutputPath)</CoverageReportInputPath>
<CoverageReportDir Condition="'$(CoverageReportDir)' == ''">$([MSBuild]::NormalizeDirectory('$(OutDir)', 'report'))</CoverageReportDir>
<RunScriptCommand>"$(DotNetTool)" coverlet "$(TargetFileName)" --target "$(RunScriptHost)" --targetargs "$(RunScriptCommand.Replace('&quot;$(RunScriptHost)&quot;', ''))" --format "opencover" --output "$(CoverageOutputPath)" --verbosity "normal" --use-source-link</RunScriptCommand>
<RunScriptCommand Condition="'@(CoverageExcludeByFile)' != ''">$(RunScriptCommand) --exclude-by-file @(CoverageExcludeByFile -> '"%(Identity)"', ' --exclude-by-file ')</RunScriptCommand>
<RunScriptCommand Condition="'@(CoverageIncludeDirectory)' != ''">$(RunScriptCommand) --include-directory @(CoverageIncludeDirectory -> '"$(RunScriptHostDir)%(Identity)"', ' --include-directory ')</RunScriptCommand>
<RunScriptCommand>$(RunScriptCommand) --include @(CoverageInclude -> '"[%(Identity)]*"', ' --include ')</RunScriptCommand>
<CoverageReportCommandLine>"$(DotNetTool)" reportgenerator "-reports:$(CoverageReportInputPath)" "-targetdir:$(CoverageReportDir.TrimEnd('\/'))" "-reporttypes:Html" "-verbosity:Info"</CoverageReportCommandLine>
</PropertyGroup>
<!-- Skip generating individual reports if a full report is generated. -->
<ItemGroup Condition="'$(BuildAllProjects)' != 'true' and '$(SkipCoverageReport)' != 'true'">
<PostRunScriptCommands Include="$(CoverageReportCommandLine)" />
</ItemGroup>
</Target>
<!-- Build a coverage report if building an individual library with Coverage true. -->
<Target Name="GenerateCoverageReport"
Condition="'$(Coverage)' == 'true' and '$(BuildAllProjects)' != 'true' and '$(SkipCoverageReport)' != 'true'"
AfterTargets="VSTest">
<ItemGroup Condition="'$(CoverageReportInputPath)' == ''">
<CoverageOutputFile Include="$(OutDir)TestResults\*\coverage.opencover.xml" />
</ItemGroup>
<PropertyGroup>
<CoverageReportInputPath Condition="'$(CoverageReportInputPath)' == ''">%(CoverageOutputFile.Identity)</CoverageReportInputPath>
<CoverageReportTypes Condition="'$(CoverageReportTypes)' == ''">Html</CoverageReportTypes>
<CoverageReportVerbosity Condition="'$(CoverageReportVerbosity)' == ''">Info</CoverageReportVerbosity>
<CoverageReportDir Condition="'$(CoverageReportDir)' == ''">$([MSBuild]::NormalizeDirectory('$(OutDir)', 'TestResults', 'report'))</CoverageReportDir>
<CoverageReportCommand>"$(DotNetTool)" reportgenerator "-reports:$(CoverageReportInputPath)" "-targetdir:$(CoverageReportDir.TrimEnd('\/'))" "-reporttypes:$(CoverageReportTypes)" "-verbosity:$(CoverageReportVerbosity)"</CoverageReportCommand>
</PropertyGroup>
<Exec Command="$(CoverageReportCommand)" />
</Target>
<!--
Clean the test results directory to guarantee that a report is generated from the
newest coverage results file.
Tracking issue https://github.com/microsoft/vstest/issues/2378.
-->
<Target Name="ClearTestResults"
BeforeTargets="VSTest"
Condition="'$(Coverage)' == 'true'">
<RemoveDir Directories="$(OutDir)TestResults" />
</Target>
</Project>