Skip to content

Commit

Permalink
Merge branch 'develop/simplest-update' into release/Byakko
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGilham committed Mar 24, 2018
2 parents 0aef5ac + 2c81891 commit cf94a72
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 54 deletions.
8 changes: 4 additions & 4 deletions AltCover/AltCover.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@
<HintPath>..\packages\FSharp.Core.4.3.4\lib\net45\FSharp.Core.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="Mono.Options">
<HintPath>..\packages\Mono.Options.Signed.0.2.3\lib\net45\Mono.Options.dll</HintPath>
Expand Down
17 changes: 7 additions & 10 deletions AltCover/OpenCover.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,30 @@ module OpenCover =
MethodCC = Some cc :: s.MethodCC}
else s

let MethodPointElement (codeSegment:Cil.SequencePoint) end' ref i =
let MethodPointElement (codeSegment:SeqPnt) ref i =
XElement(X "SequencePoint",
XAttribute(X "vc", 0),
XAttribute(X "uspid", i),
XAttribute(X "ordinal", 0),
XAttribute(X "offset", codeSegment.Offset),
XAttribute(X "sl", codeSegment.StartLine),
XAttribute(X "sc", codeSegment.StartColumn),
XAttribute(X "el", fst end'),
XAttribute(X "ec", snd end'),
XAttribute(X "el", codeSegment.EndLine),
XAttribute(X "ec", codeSegment.EndColumn),
XAttribute(X "bec", 0),
XAttribute(X "bev", 0),
XAttribute(X "fileid", ref))

let VisitCodeSegment (s : Context) (codeSegment:Cil.SequencePoint) i =
let VisitCodeSegment (s : Context) (codeSegment:SeqPnt) i =
if s.Excluded = Nothing then
// quick fix for .mdb lack of end line/column information
let end' = match (codeSegment.EndLine, codeSegment.EndColumn) with
| (-1, _) -> (codeSegment.StartLine, codeSegment.StartColumn + 1)
| endPair -> endPair
let file = codeSegment.Document.Url
let file = codeSegment.Document
let fileset, ref = if s.Files.ContainsKey file then
s.Files, s.Files.Item file
else
let index = s.Files.Count + 1
s.Files.Add (file, index), index
let element = MethodPointElement codeSegment end' ref i
let element = MethodPointElement codeSegment ref i
let head = s.Stack |> Seq.head
if head.IsEmpty then head.Add(element)
else head.FirstNode.AddBeforeSelf(element)
Expand All @@ -205,7 +202,7 @@ module OpenCover =
MethodSeq = s.MethodSeq + 1}
else s

let VisitMethodPoint (s : Context) (codeSegment':Cil.SequencePoint option) i =
let VisitMethodPoint (s : Context) (codeSegment':SeqPnt option) i =
match codeSegment' with
| Some codeSegment -> VisitCodeSegment s codeSegment i
| None -> s
Expand Down
12 changes: 4 additions & 8 deletions AltCover/Report.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,17 @@ module Report =
head.Add(element)
element :: s

let VisitMethodPoint (s : list<XElement>) (head:XElement) (codeSegment':Cil.SequencePoint option) included =
let VisitMethodPoint (s : list<XElement>) (head:XElement) (codeSegment':SeqPnt option) included =
match codeSegment' with
| Some codeSegment ->
// quick fix for .mdb lack of end line/column information
let end' = match (codeSegment.EndLine, codeSegment.EndColumn) with
| (-1, _) -> (codeSegment.StartLine, codeSegment.StartColumn + 1)
| endPair -> endPair
let element = XElement(X "seqpnt",
XAttribute(X "visitcount", 0),
XAttribute(X "line", codeSegment.StartLine),
XAttribute(X "column", codeSegment.StartColumn),
XAttribute(X "endline", fst end'),
XAttribute(X "endcolumn", snd end'),
XAttribute(X "endline", codeSegment.EndLine),
XAttribute(X "endcolumn", codeSegment.EndColumn),
XAttribute(X "excluded", ToExcluded included),
XAttribute(X "document", codeSegment.Document.Url))
XAttribute(X "document", codeSegment.Document))
if head.IsEmpty then head.Add(element)
else head.FirstNode.AddBeforeSelf(element)
| None -> ()
Expand Down
31 changes: 27 additions & 4 deletions AltCover/Visitor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,36 @@ open Mono.Cecil.Rocks
[<Flags>]
type Inspect = Ignore = 0 | Instrument = 1 | Track = 2 | TrackOnly = 4

[<ExcludeFromCodeCoverage>]
type SeqPnt = {
StartLine : int
StartColumn : int
EndLine : int
EndColumn : int
Document : string
Offset : int
}
with static member Build(codeSegment:Cil.SequencePoint) = {
StartLine = codeSegment.StartLine
StartColumn = codeSegment.StartColumn
EndLine = if codeSegment.EndLine < 0
then codeSegment.StartLine
else codeSegment.EndLine
EndColumn = if codeSegment.EndLine < 0
then codeSegment.StartColumn + 1
else codeSegment.EndColumn
Document = codeSegment.Document.Url
Offset = codeSegment.Offset
}

[<ExcludeFromCodeCoverage>]
type internal Node =
| Start of seq<string>
| Assembly of AssemblyDefinition * Inspect
| Module of ModuleDefinition * Inspect
| Type of TypeDefinition * Inspect
| Method of MethodDefinition * Inspect * (int * string) option
| MethodPoint of Instruction * SequencePoint option * int * bool
| MethodPoint of Instruction * SeqPnt option * int * bool
| AfterMethod of MethodDefinition * Inspect * (int * string) option
| AfterType
| AfterModule
Expand Down Expand Up @@ -244,9 +266,10 @@ module Visitor =
else
instructions.OrderByDescending(fun (x:Instruction) -> x.Offset)
|> Seq.mapi (fun i x -> let s = dbg.GetSequencePoint(x)
MethodPoint (x, Some s, i+point, interesting && (s.Document.Url |>
IsIncluded |>
IsInstrumented)))
MethodPoint (x, s |> SeqPnt.Build |> Some,
i+point, interesting && (s.Document.Url |>
IsIncluded |>
IsInstrumented)))

let rec internal Deeper node =
// The pattern here is map x |> map y |> map x |> concat => collect (x >> y >> z)
Expand Down
2 changes: 1 addition & 1 deletion AltCover/altcover.core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.10.0-beta7" />
<PackageReference Include="Mono.Cecil" Version="0.10.0" />
<PackageReference Include="Mono.Options" Version="5.3.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="System.Security.Permissions" Version="4.4.1" />
Expand Down
2 changes: 1 addition & 1 deletion AltCover/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FSharpLint.MSBuild" version="0.9.1-beta" targetFramework="net47" />
<package id="Mono.Cecil" version="0.10.0-beta7" targetFramework="net47" />
<package id="Mono.Cecil" version="0.10.0" targetFramework="net47" />
<package id="Mono.Options.Signed" version="0.2.3" targetFramework="net47" />
<package id="Newtonsoft.Json" version="11.0.1" targetFramework="net47" />
</packages>
2 changes: 1 addition & 1 deletion Build/targets.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Target "BuildMonoSamples" (fun _ ->

[
("./_Mono/Sample1", "-debug -out:./_Mono/Sample1/Sample1.exe ./Sample1/Program.cs")
("./_Mono/Sample3", "-target:library -debug -out:./_Mono/Sample3/Sample3.dll -lib:./packages/Mono.Cecil.0.10.0-beta7/lib/net40 -r:Mono.Cecil.dll ./Sample3/Class1.cs")
("./_Mono/Sample3", "-target:library -debug -out:./_Mono/Sample3/Sample3.dll -lib:./packages/Mono.Cecil.0.10.0/lib/net40 -r:Mono.Cecil.dll ./Sample3/Class1.cs")
]
|> Seq.iter (fun (dir, cmd) ->
Directory.ensure dir
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# altcover
Instrumenting coverage tool for .net and Mono, reimplemented and extended from [dot-net-coverage](https://github.com/SteveGilham/dot-net-coverage)
Instrumenting coverage tool for .net (framework and core) and Mono, reimplemented and extended from [dot-net-coverage](https://github.com/SteveGilham/dot-net-coverage)

The latest releases can be downloaded from [releases](https://github.com/SteveGilham/altcover/releases), but the easiest (and most automated) way is through the [nuget package](https://www.nuget.org/packages/AltCover). Alternatively, for .net core, which is distributed by source anyway for flexibility, you can just link from this repo as a Git module, and be as conservative or as bleeding-edge daring as you wish.

## Why altcover?
As the name suggests, it's an alternative coverage approach. Rather than working by hooking the .net profiling API at run-time, it works by weaving the same sort of extra IL into the assemblies of interest ahead of execution. This means that it should work pretty much everywhere, so long as the executing process has write access to the results file.
As the name suggests, it's an alternative coverage approach. Rather than working by hooking the .net profiling API at run-time, it works by weaving the same sort of extra IL into the assemblies of interest ahead of execution. This means that it should work pretty much everywhere, whatever your platform, so long as the executing process has write access to the results file. You can even mix-and-match between platforms used to instrument and those under test.

In particular, this approach supports Mono, as long as suitable `.mdb` (or `.pdb`, in recent versions) symbols are available. One major limitation here is that the `.mdb` format only stores the start location in the source of any code sequence point, and not the end; consequently any nicely coloured reports that take that information into account may show a bit strangely.

**Note** that under Mono on non-Windows platforms the default values of `--debug:full` or `--debug:pdbonly` generate no symbols from F# projects -- and without symbols, such assemblies cannot be instrumented. Unlike with C# projects, where the substitution appears to be automatic, to use the necessary `--debug:portable` option involves explicitly hand editing the old-school `.fsproj` file to have `<DebugType>portable</DebugType>`.
In particular, while instrumenting .net core assemblies "just works" with this approach, it also supports Mono, as long as suitable `.mdb` (or `.pdb`, in recent versions) symbols are available. One major limitation here is that the `.mdb` format only stores the start location in the source of any code sequence point, and not the end; consequently any nicely coloured reports that take that information into account may show a bit strangely.

### Why altcover? -- the back-story of why it was ever a thing

Back in 2010, the new .net version finally removed the deprecated profiling APIs that the free NCover 1.5.x series relied upon. The first version of AltCover was written to both fill a gap in functionality, and to give me an excuse for a ground-up F# project to work on. As such, it saw real production use for about a year and a half, until OpenCover reached a point where it could be used for .net4/x64 work (and I could find time to adapt everything downstream that consumed NCover format input).

Fast forwards to autumn 2017, and I get the chance to dust the project off, with the intention of saying that it worked on Mono, too -- and realise that it's _déja vu_ all over again, because .net core didn't yet have profiler based coverage tools either, and the same approach would work there as well.

### Other notes

1. On old-fashioned .net framework, the `ProcessExit` event handling window of ~2s is sufficient for processing significant bodies of code under test (several 10s of kloc, as observed in production back in the '10-'11 timeframe); under `dotnet test` the window seems to be rather tighter (about 100ms, experimentally, about enough for 1kloc). Therefore, the preferred way to perform coverage gathering for .net core, except for the smallest programs, is to run with AltCover in the "runner" mode. By their nature, unit tests invoking significant frameworks are not small programs, even if the system under test is itself small.

2. Under Mono on non-Windows platforms the default values of `--debug:full` or `--debug:pdbonly` generate no symbols from F# projects -- and without symbols, such assemblies cannot be instrumented. Unlike with C# projects, where the substitution appears to be automatic, to use the necessary `--debug:portable` option involves explicitly hand editing the old-school `.fsproj` file to have `<DebugType>portable</DebugType>`.


## Continuous Integration

| | |
Expand Down Expand Up @@ -55,10 +60,6 @@ Use `dotnet fake run ./Build/build.fsx --target <targetname>` to run to a specif

The tests in the `Tests.fs` file are ordered in the same dependency order as the code within the AltCover project (the later `Runner` tests aside). While working on any given layer, it would make sense to comment out all the tests for later files so as to show what is and isn't being covered by explicit testing, rather than merely being cascaded through.

## Other

The main executable still links the Mono.Cecil 0.10-beta7 version, as the 0.10 final version causes issues with some build tools that haven't caught up with events. The binary injected into the instrumented code does not link Cecil at all. Until the wrinkles are ironed out or worked around, I make this disclaimer instead.

## Thanks to

* [AppVeyor](https://ci.appveyor.com/project/SteveGilham/altcover) for allowing free build CI services for Open Source projects
Expand Down
7 changes: 5 additions & 2 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
**NOTE:** The main executable still links the Mono.Cecil 0.10-beta7 version, as the 0.10 final version causes issues with some build tools that haven't caught up with events. The binary injected into the instrumented code does not link Cecil at all. Until the wrinkles are ironed out or worked around, I make this disclaimer instead.
# 2.0.354 (Byakko series release 6)
No functional changes this release, just updates of consumed components and tooling
* Take the Cecil 0.10.0 final release, having worked around the issues with unit testing in .net core with NUnit3TestAdapter 3.10, which links the beta6 version, which has a subset of the internal API present in the full release.
* Take the latest VS2017 and FAKE updates for building

# 2.0.350
# 2.0.350 (Byakko series release 5)
* `-c|callContext` option to track (when collecting the coverage data in "runner" mode) what led to a particular line of code being visited -- either by being called from a method of a specified name or with a specified attribute (unit testing, in the main), or by time of visit (which would be more appropriate for system testing). Methods (typically, unit tests) may be tracked even if they are in assemblies that are excluded from instrumentation via the `-e`command line parameter.

# 2.0.330 (Byakko series release 4)
Expand Down
8 changes: 4 additions & 4 deletions Sample3/Sample3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Mono.Cecil, Version=0.10.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Mdb, Version=0.10.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Mdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Pdb, Version=0.10.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Pdb.dll</HintPath>
</Reference>
<Reference Include="Mono.Cecil.Rocks, Version=0.10.0.0, Culture=neutral, PublicKeyToken=50cebf1cceb9d05e, processorArchitecture=MSIL">
<HintPath>..\packages\Mono.Cecil.0.10.0-beta7\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
<HintPath>..\packages\Mono.Cecil.0.10.0\lib\net40\Mono.Cecil.Rocks.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion Sample3/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Cecil" version="0.10.0-beta7" targetFramework="net461" />
<package id="Mono.Cecil" version="0.10.0" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion Sample3/sample3.core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.10.0-beta7" />
<PackageReference Include="Mono.Cecil" Version="0.10.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit cf94a72

Please sign in to comment.