Re-enable xunit.console code coverage (#35894)
authorViktor Hofer <viktor.hofer@microsoft.com>
Wed, 6 May 2020 17:03:55 +0000 (19:03 +0200)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 17:03:55 +0000 (19:03 +0200)
Re-enable xunit console code coverage measurement until
https://github.com/coverlet-coverage/coverlet/issues/834 is fixed.

.config/dotnet-tools.json
docs/workflow/building/libraries/code-coverage.md
eng/testing/coverage.targets
src/libraries/tests.proj

index fd4f16b..49e4750 100644 (file)
@@ -2,8 +2,14 @@
   "version": 1,
   "isRoot": true,
   "tools": {
+    "coverlet.console": {
+      "version": "1.7.1",
+      "commands": [
+        "coverlet"
+      ]
+    },
     "dotnet-reportgenerator-globaltool": {
-      "version": "4.5.2",
+      "version": "4.5.8",
       "commands": [
         "reportgenerator"
       ]
index 7b77512..d9ab38a 100644 (file)
@@ -36,7 +36,7 @@ This runs the tests and generates the full code coverage report. The resulting i
 
 You can also build and test with code coverage for a particular test project rather than for the whole repo with the `/p:Coverage=true` property:
 
-    dotnet test -f net5.0 /p:Coverage=true
+    dotnet build /t:Test /p:Coverage=true
 
 The results for this one library will then be available in this index.htm file, where $(OutDir) is the directory where the binaries were generated.
 
@@ -44,7 +44,7 @@ The results for this one library will then be available in this index.htm file,
 
 For example, to build, test, and get code coverage results for the System.Diagnostics.Debug library, from the root of the repo one can do:
 
-    dotnet test src\libraries\System.Diagnostics.Debug -f net5.0 /p:Coverage=true
+    dotnet build src\System.Diagnostics.Debug\tests /t:Test /p:Coverage=true
 
 And then once the run completes:
 
index 62e77ca..fd53a86 100644 (file)
     </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('&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)" 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 '$(SkipCoverageReport)' != 'true'"
+          Condition="'$(Coverage)' == 'true' and '$(BuildAllProjects)' != 'true' and '$(SkipCoverageReport)' != 'true'"
           AfterTargets="VSTest">
     <ItemGroup Condition="'$(CoverageReportInputPath)' == ''">
       <CoverageOutputFile Include="$(OutDir)TestResults\*\coverage.opencover.xml" />
index 9863624..6e92a43 100644 (file)
@@ -5,7 +5,7 @@
     <!-- For tests we want to continue running if a test run failed. -->
     <TestContinueOnError>ErrorAndContinue</TestContinueOnError>
     <TraversalGlobalProperties>BuildAllProjects=true</TraversalGlobalProperties>
-    <CoverageReportInputPath>$(ArtifactsBinDir)*.Tests\TestResults\*\coverage.opencover.xml</CoverageReportInputPath>
+    <CoverageReportInputPath>$(ArtifactsBinDir)\*.Tests\**\coverage.opencover.xml</CoverageReportInputPath>
     <CoverageReportDir>$(ArtifactsDir)coverage</CoverageReportDir>
     <EnableCoverageSupport>true</EnableCoverageSupport>
   </PropertyGroup>