Skip to content

The v5.x╱6.x AltCover.Fake package

Steve Gilham edited this page Apr 11, 2020 · 1 revision

The v5.x╱6.x 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.

Before v6.3 required Fake 5.9.3 or later; at v6.3 requires Fake 5.18 or later

module AltCover_Fake.DotNet.Testing.Primitive

This module is declared [<RequireQualifiedAccess>] Included data types CollectParams and PrepareParams are otherwise identical to module AltCover.Primitive of the in-process API. There is no Logging type.

module AltCover_Fake.DotNet.Testing.AltCover

This module is declared [<RequireQualifiedAccess>] Included data types are otherwise identical to module AltCover.FSApi of the in-process API, with the following exceptions

  • CollectParams has no Validate : bool -> string array method
  • PrepareParams has no Validate : unit -> string array method
  • There is no Logging type
  • There are the following extra types
[<NoComparison>]
type AltCover.ArgType =
  | Collect of CollectParams
  | Prepare of PrepareParams
  | ImportModule
  | GetVersion

defining the AltCover operation wanted

[<NoComparison; Obsolete("Use Fake.DotNet.ToolType instead">] // Obsolete as of v6.3
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; NoEquality>] // NoEquality as of v6.3
type Params =
  { /// Path to the Altcover executable.
    ToolPath : string
    /// Which version of the tool
    ToolType : ToolType
    /// Define the tool through FAKE 5.18 ToolType -- if set, overrides
    FakeToolType: Fake.DotNet.ToolType option // v6.3
    /// Working directory for relative file paths.  Default is the current working directory
    WorkingDirectory : string
    /// Command arguments
    Args : ArgType }

  static member Create a : ArgType -> Params
  member WithToolType tool : Fake.DotNet.ToolType -> Params // v6.3; sets the FakeToolType field
  member WithCreateProcess command : CreateProcess<_> -> Params // v6.3; sets Executable (if appropriate) and CommandLine

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.splitCommandLine : string -> string list (v6.3)

to safely break up the argument list from e.g. Fake.DotNet.Testing.NUnit3.buildArgs or ``Fake.DotNet.Testing.XUnit2.buildArgsfor constructing aCommandLine` (see example here)

AltCover.buildDotNetTestCommandLine: (DotNet.TestOptions -> DotNet.TestOptions) -> string -> (string * string list)

taking the arguments for DotNet.test and returning the dotnet path and the rest of the command line for again constructing a CommandLine; and Executable for CollectParams. See example setup and use.

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

AltCover.run  parameters : Params -> unit

which actually does the work, and

AltCover.runWithMono parameters : string option -> Params -> unit

which runs AltCover expressed as a Fake.DotNet.ToolType.FullFramework under the supplied mono path (defaulting to just mono) on Windows, just as the AltCover.ToolType.MonoTool did.

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

module AltCover_Fake.DotNet.Testing.DotNet

This module is declared [<RequireQualifiedAccess>] Contains type CLIArgs otherwise identical to the one in module AltCover.DotNet

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

  • member self.WithParameters (prepare:AltCover.PrepareParams) (collect:AltCover.CollectParams) (force : AltCover_Fake.DotNet.Testing.DotNet.CLIArgs) : Fake.DotNet.DotNet.TestOptions (obsolete at v6.5)
  • member self.WithAltCoverParameters (prepare:AltCover.PrepareParams) (collect:AltCover.CollectParams) (force : AltCover_Fake.DotNet.Testing.DotNet.CLIArgs) : Fake.DotNet.DotNet.TestOptions (from v6.5) 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
open AltCover_Fake.DotNet.Testing
open Fake.Core
open Fake.DotNet
...
  let ForceTrue = DotNet.CLIArgs.Force true
    let p0 = Primitive.PrepareParams.Create()
    let pp0 = AltCover.PrepareParams.Primitive p0
    let c0 = Primitive.CollectParams.Create()
    let p1 = { p0 with CallContext = [ "[Fact]"; "0" ]
                       AssemblyFilter = [| "xunit" |] }
    let pp1 = AltCover.PrepareParams.Primitive p1
    let cc0 = AltCover.CollectParams.Primitive c0
    DotNet.test
      (fun to' ->
      (to'.WithParameters pp1 cc0 ForceTrue |> withCLIArgs)
      "dotnettest.fsproj"


  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 ForceTrue) "dotnettest.fsproj"