Skip to content

Commit 1433ce6

Browse files
More reliable coverage (#3294)
- Fix write conflicts when adding the coverage to the `GITHUB_STEP_SUMMARY`. - Add the target framework to the coverage report summaries.
1 parent 84089d0 commit 1433ce6

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

Directory.Build.targets

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,49 @@
1616
<ReportGeneratorReportTypes>HTML</ReportGeneratorReportTypes>
1717
<ReportGeneratorReportTypes Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' ">$(ReportGeneratorReportTypes);MarkdownSummaryGitHub</ReportGeneratorReportTypes>
1818
<ReportGeneratorTargetDirectory>$([System.IO.Path]::Combine($(ArtifactsPath), 'coverage'))</ReportGeneratorTargetDirectory>
19+
<ReportGeneratorMarkdownSummary>$([System.IO.Path]::Combine($(ReportGeneratorTargetDirectory), 'SummaryGithub.md'))</ReportGeneratorMarkdownSummary>
1920
<MergeWith>$([System.IO.Path]::Combine($(ReportGeneratorTargetDirectory), 'coverage.json'))</MergeWith>
2021
</PropertyGroup>
22+
<UsingTask TaskName="WriteLinesToFileWithRetry" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
23+
<ParameterGroup>
24+
<File ParameterType="System.String" Required="true" />
25+
<Lines ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
26+
</ParameterGroup>
27+
<Task>
28+
<Code Type="Fragment" Language="cs"><![CDATA[
29+
var lines = new System.Collections.Generic.List<string>();
30+
foreach (var line in Lines)
31+
{
32+
lines.Add(line.ItemSpec);
33+
}
34+
int attempt = 0;
35+
while (attempt < 3)
36+
{
37+
try
38+
{
39+
System.IO.File.AppendAllLines(File, lines);
40+
break;
41+
}
42+
catch (System.IO.IOException)
43+
{
44+
attempt++;
45+
System.Threading.Thread.Sleep(1_000);
46+
}
47+
}
48+
]]></Code>
49+
</Task>
50+
</UsingTask>
2151
<Target Name="GenerateCoverageReports" AfterTargets="GenerateCoverageResultAfterTest" Condition=" '$(CollectCoverage)' == 'true' ">
2252
<ReportGenerator ReportFiles="@(CoverletReport)" ReportTypes="$(ReportGeneratorReportTypes)" Tag="$(Version)" TargetDirectory="$(ReportGeneratorTargetDirectory)" Title="$(AssemblyName)" VerbosityLevel="Warning" />
23-
<PropertyGroup Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' ">
24-
<_ReportSummaryContent>&lt;details&gt;&lt;summary&gt;:chart_with_upwards_trend: &lt;b&gt;$(AssemblyName) Code Coverage report&lt;/b&gt;&lt;/summary&gt;</_ReportSummaryContent>
53+
<PropertyGroup Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' AND Exists('$(ReportGeneratorMarkdownSummary)') ">
54+
<_ReportSummaryContent>&lt;details&gt;&lt;summary&gt;:chart_with_upwards_trend: &lt;b&gt;$(AssemblyName) Code Coverage report&lt;/b&gt; %28$(TargetFramework)%29&lt;/summary&gt;</_ReportSummaryContent>
2555
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.Environment]::NewLine)</_ReportSummaryContent>
2656
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.Environment]::NewLine)</_ReportSummaryContent>
27-
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.IO.File]::ReadAllText('$([System.IO.Path]::Combine($(ReportGeneratorTargetDirectory), 'SummaryGithub.md'))'))</_ReportSummaryContent>
57+
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.IO.File]::ReadAllText('$(ReportGeneratorMarkdownSummary)'))</_ReportSummaryContent>
2858
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.Environment]::NewLine)</_ReportSummaryContent>
2959
<_ReportSummaryContent>$(_ReportSummaryContent)$([System.Environment]::NewLine)</_ReportSummaryContent>
3060
<_ReportSummaryContent>$(_ReportSummaryContent)&lt;/details&gt;</_ReportSummaryContent>
3161
</PropertyGroup>
32-
<WriteLinesToFile Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' " ContinueOnError="WarnAndContinue" File="$(GITHUB_STEP_SUMMARY)" Lines="$(_ReportSummaryContent)" />
62+
<WriteLinesToFileWithRetry Condition=" '$(ReportGeneratorOutputMarkdown)' == 'true' AND Exists('$(ReportGeneratorMarkdownSummary)') " ContinueOnError="WarnAndContinue" File="$(GITHUB_STEP_SUMMARY)" Lines="$(_ReportSummaryContent)" />
3363
</Target>
3464
</Project>

0 commit comments

Comments
 (0)