Skip to content

The v4.0 AltCover.Fake package

Steve Gilham edited this page Dec 23, 2018 · 1 revision

The v4.0 AltCover.Fake package

This provides helpers to drive command-line AltCover (or dotnet test with AltCover) from any of the other packages. In these scenarios, AltCover operates outside the Fake build process. The slightly awkward AltCover_Fake namespace was chosen to allow co-existence with the previous in-process API's AltCover.Fake names.

module AltCover_Fake.DotNet.Testing.AltCover

This module is declared [<RequireQualifiedAccess>] Included data types are

[<NoComparison>]
type AltCover.CollectParams =
  {RecorderDirectory: String;
   WorkingDirectory: String;
   Executable: String;
   LcovReport: String;
   Threshold: String;
   Cobertura: String;
   OutputFile: String;
   CommandLine: String;}
  with
    static member Create() : unit -> CollectParams
    member withCommandLine args : string list -> CollectParams
  end

with the same fields as the in-process version. As before, Create() returns an instance with all string values empty; withCommandLine uses FAKE's CreateProcess APIs to create a suitable CommandLine field (enquoting as required and concatenating the individual arguments).

[<NoComparison>]
type AltCover.PrepareParams =
  {InputDirectory: String;
   OutputDirectory: String;
   SymbolDirectories: string seq;
   Dependencies: string seq;
   Keys: string seq;
   StrongNameKey: String;
   XmlReport: String;
   FileFilter: string seq;
   AssemblyFilter: string seq;
   AssemblyExcludeFilter: string seq;
   TypeFilter: string seq;
   MethodFilter: string seq;
   AttributeFilter: string seq;
   PathFilter: string seq;
   CallContext: string seq;
   OpenCover: bool;
   InPlace: bool;
   Save: bool;
   Single: bool;
   LineCover: bool;
   BranchCover: bool;
   CommandLine: String;}
  with
    static member Create() : unit -> CollectParams
    member withCommandLine args : string list -> CollectParams
  end

with the same fields as the in-process version. As before, Create() returns an instance with all empty or false fields except OpenCover, InPlace and Save are true; withCommandLine uses FAKE's CreateProcess APIs to create a suitable CommandLine field (enquoting as required and concatenating the individual arguments).

[<NoComparison>]
type AltCover.ArgType =
  | Collect of CollectParams
  | Prepare of PrepareParams
  | ImportModule
  | GetVersion

defining the AltCover operation wanted

[<NoComparison>]
type AltCover.ToolType =
  | DotNet of string option
  | Mono of string option
  | Global
  | Framework

defining the AltCover executable type to be used (Framework defaults to running under Mono except on Windows, Mono means use Mono regardless). The string option values allow for overriding the default dotnet or mono executables respectively.

[<NoComparison>]
type Params =
  { /// Path to the Altcover executable.
    ToolPath : string
    /// Which version of the tool
    ToolType : ToolType
    /// Working directory for relative file paths.  Default is the current working directory
    WorkingDirectory : string
    /// Command arguments
    Args : ArgType }

  static member Create a : ArgType -> Params

where the Create member defaults to the altcover global tool, using the current working directory, to run the supplied arguments.

and finally, module-level functions

AltCover.composeCommandLine parameters : Params -> Fake.Core.CreateProcess<unit>

which takes an AltCover.Params and synthesises the command to execute, from which the full command line can be read and

AltCover.Run  parameters : Params -> unit

which actually does the work.

Example

open AltCover_Fake.DotNet.Testing
...
    let prep =
      { AltCover.PrepareParams.Create() with XmlReport = xaltReport
                                             OutputDirectory = "./__UnitTestWithAltCover"
                                             StrongNameKey = keyfile
                                             OpenCover = false
                                             InPlace = false
                                             Save = false }
      |> AltCover.Prepare
    { AltCover.Params.Create prep with ToolPath = altcover
                                       ToolType = AltCover.ToolType.Framework
                                       WorkingDirectory = xtestDirectory }
    |> AltCover.run

type Fake.DotNet.DotNet.TestOptions Extension methods (in module AltCover_Fake.DotNet.DotNet)

  • member self.WithParameters (prepare:AltCover.PrepareParams) (collect:AltCover.CollectParams) : Fake.DotNet.DotNet.TestOptions Adds the specified set of arguments to the CustomParams member of the Common member
  • member self.WithImportModule () : Fake.DotNet.DotNet.TestOptions Adds "/p:AltCoverIpmo=true" to the CustomParams member of the Common member
  • member self.WithGetVersion () : Fake.DotNet.DotNet.TestOptions Adds "/p:AltCoverGetVersion=true" to the CustomParams member of the Common member

Example

open AltCover_Fake.DotNet.DotNet
...
  let p2 = { AltCover.PrepareParams.Create () with CallContext = [| "[Fact]"; "0" |] }
  let c2 = AltCover.CollectParams.Create()

  let setBaseOptions (o:DotNet.Options) =
    { o with WorkingDirectory = Path.getFullName "./_DotnetTest"
             Verbosity = Some DotNet.Verbosity.Minimal }

  DotNet.test (fun to' -> to'.WithCommon(setBaseOptions).WithParameters p2 c2) "dotnettest.fsproj"