-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Profiler based code coverage (#1937)
Do code coverage using the new Profiler approach under UseBreakpoints = $false experimental flag.
- Loading branch information
Showing
15 changed files
with
946 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Pester.Tracing | ||
{ | ||
public struct CodeCoveragePoint | ||
{ | ||
public static CodeCoveragePoint Create(string path, int line, int column, int bpColumn, string astText) | ||
{ | ||
return new CodeCoveragePoint(path, line, column, bpColumn, astText); | ||
} | ||
|
||
public CodeCoveragePoint(string path, int line, int column, int bpColumn, string astText) | ||
{ | ||
Path = path; | ||
Line = line; | ||
Column = column; | ||
BpColumn = bpColumn; | ||
AstText = astText; | ||
|
||
// those are not for users to set, | ||
// we use them to make CC output easier to debug | ||
// because this will show in list of hits what we think | ||
// should or should not hit, for performance just bool | ||
// would be enough | ||
Text = default; | ||
Hit = false; | ||
} | ||
|
||
public int Line; | ||
public int Column; | ||
public int BpColumn; | ||
public string Path; | ||
public string AstText; | ||
|
||
// those are not for users to set, | ||
// we use them to make CC output easier to debug | ||
// because this will show in list of hits what we think | ||
// should or should not hit, for performance just bool | ||
// would be enough | ||
public string Text; | ||
public bool Hit; | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Hit}:'{AstText}':{Line}:{Column}:{Path}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System.Collections.Generic; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Language; | ||
|
||
namespace Pester.Tracing | ||
{ | ||
public class CodeCoverageTracer : ITracer | ||
{ | ||
public static CodeCoverageTracer Create(List<CodeCoveragePoint> points) | ||
{ | ||
return new CodeCoverageTracer(points); | ||
} | ||
|
||
public CodeCoverageTracer(List<CodeCoveragePoint> points) | ||
{ | ||
foreach (var point in points) | ||
{ | ||
var key = $"{point.Line}:{point.Column}"; | ||
if (!Hits.ContainsKey(point.Path)) | ||
{ | ||
var lineColumn = new Dictionary<string, CodeCoveragePoint> { [key] = point }; | ||
Hits.Add(point.Path, lineColumn); | ||
continue; | ||
} | ||
|
||
var hits = Hits[point.Path]; | ||
if (!hits.ContainsKey(key)) | ||
{ | ||
hits.Add(key, point); | ||
continue; | ||
} | ||
|
||
// if the key is there do nothing, we already set it to false | ||
} | ||
} | ||
|
||
// list of what Pester figures out from the AST that we care about for CC | ||
// keyed as path -> line:column -> CodeCoveragePoint | ||
public Dictionary<string, Dictionary<string, CodeCoveragePoint>> Hits { get; } = new Dictionary<string, Dictionary<string, CodeCoveragePoint>>(); | ||
|
||
public void Trace(IScriptExtent extent, ScriptBlock _, int __) | ||
{ | ||
// ignore unbound scriptblocks | ||
if (extent?.File == null) | ||
return; | ||
|
||
// Console.WriteLine($"{extent.File}:{extent.StartLineNumber}:{extent.StartColumnNumber}:{extent.Text}"); | ||
if (!Hits.TryGetValue(extent.File, out var lineColumn)) | ||
return; | ||
|
||
var key2 = $"{extent.StartLineNumber}:{extent.StartColumnNumber}"; | ||
if (!lineColumn.ContainsKey(key2)) | ||
return; | ||
|
||
|
||
var point = lineColumn[key2]; | ||
if (point.Hit == true) | ||
return; | ||
|
||
point.Hit = true; | ||
point.Text = extent.Text; | ||
|
||
lineColumn[key2] = point; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Language; | ||
using System.Reflection; | ||
|
||
namespace Pester.Tracing | ||
{ | ||
class ExternalTracerAdapter : ITracer | ||
{ | ||
private object _tracer; | ||
private MethodInfo _traceMethod; | ||
|
||
public ExternalTracerAdapter(object tracer) | ||
{ | ||
_tracer = tracer ?? new NullReferenceException(nameof(tracer)); | ||
var traceMethod = tracer.GetType().GetMethod("Trace", new Type[] { typeof(IScriptExtent), typeof(ScriptBlock), typeof(int) }); | ||
_traceMethod = traceMethod ?? throw new InvalidOperationException("The provided tracer does not have Trace method with this signature: Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level)"); | ||
} | ||
|
||
public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level) | ||
{ | ||
_traceMethod.Invoke(_tracer, new object[] { extent, scriptBlock, level }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Management.Automation; | ||
using System.Management.Automation.Language; | ||
|
||
namespace Pester.Tracing | ||
{ | ||
public interface ITracer | ||
{ | ||
void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level); | ||
} | ||
} |
Oops, something went wrong.