forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoverage.targets
89 lines (77 loc) · 5.46 KB
/
coverage.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<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 -> '$(TestHostRootPath)%(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'">
<PropertyGroup>
<CoverageOutputPath Condition="'$(CoverageOutputPath)' == ''">coverage.opencover.xml</CoverageOutputPath>
<CoverageReportInputPath Condition="'$(CoverageReportInputPath)' == ''">$(CoverageOutputPath)</CoverageReportInputPath>
<CoverageReportDir Condition="'$(CoverageReportDir)' == ''">$([MSBuild]::NormalizeDirectory('$(OutDir)', 'report'))</CoverageReportDir>
<RunScriptCommand>"$(DotNetTool)" tool run coverlet "$(TargetFileName)" --target "$(RunScriptHost)" --targetargs "$(RunScriptCommand.Replace('"$(RunScriptHost)"', ''))" --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)" tool run 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)" tool run 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>