Skip to content

Commit b977f61

Browse files
authored
Coverage report tweaks (#272)
* Tuned the GenerateCoverageReport target in the library template - Added dependency on Dotnetest - Closed #251 * Added ShowCoverageReport target into library template * Code coverage evaluation in the library template is disabled by default
1 parent 972265c commit b977f61

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

Content/Library/.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ jobs:
3131
./build.sh
3232
env:
3333
CI: true
34+
ENABLE_COVERAGE: true
3435
- name: Build via Windows
3536
if: runner.os == 'Windows'
3637
run: ./build.cmd
3738
env:
3839
CI: true
40+
ENABLE_COVERAGE: true

Content/Library/.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
NUGET_TOKEN: ${{ secrets.NUGET_TOKEN }}
2626
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2727
FAKE_DETAILED_ERRORS: true
28-
DISABLE_COVERAGE: true # AltCover doesn't work with Release builds, reports lower coverage than actual
28+
ENABLE_COVERAGE: false # AltCover doesn't work with Release builds, reports lower coverage than actual
2929
run: |
3030
chmod +x ./build.sh
3131
./build.sh Publish

Content/Library/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ or
3636

3737
- `CONFIGURATION` will set the [configuration](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x#options) of the dotnet commands. If not set, it will default to Release.
3838
- `CONFIGURATION=Debug ./build.sh` will result in `-c` additions to commands such as in `dotnet build -c Debug`
39-
- `DISABLE_COVERAGE` Will disable running code coverage metrics. AltCover can have [severe performance degradation](https://github.com/SteveGilham/altcover/issues/57) so it's worth disabling when looking to do a quicker feedback loop.
40-
- `DISABLE_COVERAGE=1 ./build.sh`
39+
- `ENABLE_COVERAGE` Will enable running code coverage metrics. AltCover can have [severe performance degradation](https://github.com/SteveGilham/altcover/issues/57) so code coverage evaluation are disabled by default to speed up the feedback loop.
40+
- `ENABLE_COVERAGE=1 ./build.sh` will enable code coverage evaluation
4141

4242

4343
---
@@ -74,6 +74,7 @@ src/MyLib.1/bin/
7474
- `FSharpAnalyzers` - Runs [BinaryDefense.FSharp.Analyzers](https://github.com/BinaryDefense/BinaryDefense.FSharp.Analyzers).
7575
- `DotnetTest` - Runs [dotnet test](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test?tabs=netcore21) on the [solution file](https://docs.microsoft.com/en-us/visualstudio/extensibility/internals/solution-dot-sln-file?view=vs-2019).
7676
- `GenerateCoverageReport` - Code coverage is run during `DotnetTest` and this generates a report via [ReportGenerator](https://github.com/danielpalme/ReportGenerator).
77+
- `ShowCoverageReport` - Shows the report generated in `GenerateCoverageReport`.
7778
- `WatchTests` - Runs [dotnet watch](https://docs.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch?view=aspnetcore-3.0) with the test projects. Useful for rapid feedback loops.
7879
- `GenerateAssemblyInfo` - Generates [AssemblyInfo](https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.applicationservices.assemblyinfo?view=netframework-4.8) for libraries.
7980
- `DotnetPack` - Runs [dotnet pack](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-pack). This includes running [Source Link](https://github.com/dotnet/sourcelink).

Content/Library/build/build.fs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let mutable changelogBackupFilename = ""
131131

132132
let publishUrl = "https://www.nuget.org"
133133

134-
let disableCodeCoverage = environVarAsBoolOrDefault "DISABLE_COVERAGE" false
134+
let enableCodeCoverage = environVarAsBoolOrDefault "ENABLE_COVERAGE" false
135135

136136
let githubToken = Environment.environVarOrNone "GITHUB_TOKEN"
137137

@@ -289,6 +289,10 @@ let failOnLocalBuild () =
289289
if not isCI.Value then
290290
failwith "Not on CI. If you want to publish, please use CI."
291291

292+
let failOnCIBuild () =
293+
if isCI.Value then
294+
failwith "On CI. If you want to run this target, please use a local build."
295+
292296
let allPublishChecks () =
293297
failOnLocalBuild ()
294298
Changelog.failOnEmptyChangelog latestEntry
@@ -403,12 +407,16 @@ let dotnetTest ctx =
403407
|> Seq.map IO.Path.GetFileNameWithoutExtension
404408
|> String.concat "|"
405409

410+
let isGenerateCoverageReport = ctx.Context.TryFindTarget("GenerateCoverageReport").IsSome
411+
406412
let args = [
407413
"--no-build"
408-
sprintf "/p:AltCover=%b" (not disableCodeCoverage)
409-
sprintf "/p:AltCoverThreshold=%d" coverageThresholdPercent
410-
sprintf "/p:AltCoverAssemblyExcludeFilter=%s" excludeCoverage
411-
"/p:AltCoverLocalSource=true"
414+
if enableCodeCoverage || isGenerateCoverageReport then
415+
sprintf "/p:AltCover=true"
416+
if not isGenerateCoverageReport then
417+
sprintf "/p:AltCoverThreshold=%d" coverageThresholdPercent
418+
sprintf "/p:AltCoverAssemblyExcludeFilter=%s" excludeCoverage
419+
"/p:AltCoverLocalSource=true"
412420
]
413421

414422
DotNet.test
@@ -449,6 +457,15 @@ let generateCoverageReport _ =
449457

450458
dotnet.reportgenerator id args
451459

460+
let showCoverageReport _ =
461+
failOnCIBuild ()
462+
coverageReportDir </> "index.html"
463+
|> Command.ShellCommand
464+
|> CreateProcess.fromCommand
465+
|> Proc.start
466+
|> ignore
467+
468+
452469
let watchTests _ =
453470
!!testsGlob
454471
|> Seq.map (fun proj ->
@@ -714,6 +731,7 @@ let initTargets () =
714731
Target.create "FSharpAnalyzers" fsharpAnalyzers
715732
Target.create "DotnetTest" dotnetTest
716733
Target.create "GenerateCoverageReport" generateCoverageReport
734+
Target.create "ShowCoverageReport" showCoverageReport
717735
Target.create "WatchTests" watchTests
718736
Target.create "GenerateAssemblyInfo" generateAssemblyInfo
719737
Target.create "DotnetPack" dotnetPack
@@ -770,6 +788,10 @@ let initTargets () =
770788
"DotnetBuild"
771789
==>! "WatchDocs"
772790

791+
"DotnetTest"
792+
==> "GenerateCoverageReport"
793+
==>! "ShowCoverageReport"
794+
773795
"UpdateChangelog"
774796
==> "GenerateAssemblyInfo"
775797
==> "GitRelease"

0 commit comments

Comments
 (0)