Skip to content

Instantly share code, notes, and snippets.

@jkone27
Last active December 15, 2023 18:50
Show Gist options
  • Save jkone27/6c1b5eea5fdc3dbbfdd5d2e16ee405f4 to your computer and use it in GitHub Desktop.
Save jkone27/6c1b5eea5fdc3dbbfdd5d2e16ee405f4 to your computer and use it in GitHub Desktop.
coverlet coverage fsc script using fli, creates a report and shows it in browser
#r "nuget: Fli"
open Fli
open System
open System
let isHelpRequested = fsi.CommandLineArgs |> Seq.contains "--h"
[<Literal>]
let usageStr =
"""usage:
dotnet fsi cover.fsx <extra options>
options:
--h: help/usage
--threshold : 80 [ code coverage % ]
--show: display coverage report in html output in browser
--onlyReport: do collect coverage, use existing file
--noReport: do not create a report
"""
if isHelpRequested then
printfn $"{usageStr}"
exit 0
else
()
// arguments skip name of the script (arg0)
let args =
fsi.CommandLineArgs
|> Array.toList
|> List.skip 1
[<Literal>]
let srcDir = __SOURCE_DIRECTORY__
let coverageFormat = "lcov"
// TODO: can just add this to csproj level - <AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
let coverletAssemblyFilters =
[
"[ACME.CORP.Client]*"
"[ACME.CORP.Cryptography]*"
"[ACME.CORP.Migrations]*"
"[ACME.CORP.Infrastructure]*"
"[ACME.CORP.Models]*"
"[ACME.CORP.*Tests?]*"
]
|> String.concat "?%2c"
|> fun s -> $"\"{s}\""
let threshold =
match args with
|"--threshold"::t::[] -> (int)t |> Some
|_ -> None
// DOC: https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
let onlyReport =
args |> Seq.contains "--onlyReport"
let noReport =
args |> Seq.contains "--noReport"
if onlyReport |> not then
let coverlet =
cli {
Exec "dotnet"
Arguments [
"test"
"--no-build"
"/p:CollectCoverage=true"
$"/p:CoverletOutputFormat={coverageFormat}"
$"/p:CoverletOutput={srcDir}/coverage/lcov.info"
$"/p:Exclude={coverletAssemblyFilters}"
match threshold with
|Some(t) ->
$"/p:Threshold={t}"
|None -> ()
]
}
coverlet |> Command.toString |> printfn "COVERAGE> %s"
coverlet
|> Command.execute
|> Output.printText
if noReport |> not then
let reportGenerator =
cli {
Exec "dotnet"
Arguments [
"reportgenerator"
"-reports:./coverage/lcov.info"
"-targetdir:coverage"
]
}
reportGenerator |> Command.toString |> printfn "REPORT> %s"
reportGenerator
|> Command.execute
|> Output.printText
let showReport = args |> Seq.contains "--show"
if showReport then
let isWindows =
Environment.OSVersion.ToString().Contains("Windows", StringComparison.OrdinalIgnoreCase)
let openBrowser =
if isWindows then
"explorer"
else
"open"
cli {
Exec openBrowser
Arguments [
"./coverage/index.html"
]
}
|> Command.execute
|> Output.printError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment