Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report code coverage that includes P tests #2478

Merged
merged 12 commits into from
Jun 5, 2024
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ stages:
targetType: inline
pwsh: $(pwsh)
script: |
& ./test.ps1 -CI -PassThru -NoBuild
& ./test.ps1 -CI -CC -PassThru -NoBuild
workingDirectory: '$(Build.SourcesDirectory)'
- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: 'coverage.xml'
pathToSources: 'src/'
pathToSources: '$(Build.SourcesDirectory)/bin/'
failIfCoverageEmpty: false
condition: succeededOrFailed()
- task: PublishTestResults@2
Expand Down
4 changes: 4 additions & 0 deletions src/csharp/Pester/Pester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<DebugType>embedded</DebugType>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);PESTER</DefineConstants>
</PropertyGroup>

<!-- PowerShell 7.2.x is the oldest supported PowerShell version. That version is built using net6.0.
But there is a bug in 7.2.0 reference assemblies, up to 7.2.10, where the IExtens.File is missing from the reference assembly:
https://github.com/PowerShell/PowerShell/issues/16408
Expand Down
10 changes: 7 additions & 3 deletions src/csharp/Pester/Tracing/CodeCoverageTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public CodeCoverageTracer(List<CodeCoveragePoint> points)
// keyed as path -> line:column -> CodeCoveragePoint
public Dictionary<string, Dictionary<string, List<CodeCoveragePoint>>> Hits { get; } = new Dictionary<string, Dictionary<string, List<CodeCoveragePoint>>>(StringComparer.OrdinalIgnoreCase);

public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __)
public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __, string ___, string ____)
{
if (_debug && extent?.File != null && CultureInfo.InvariantCulture.CompareInfo.IndexOf(extent.File, _debugFile, CompareOptions.OrdinalIgnoreCase) >= 0)
{
Expand All @@ -73,6 +73,10 @@ public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __)
}
}

if (_debug) {
Console.WriteLine($">>> CC");
}

// ignore unbound scriptblocks
if (extent?.File == null)
return;
Expand Down Expand Up @@ -102,10 +106,10 @@ public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __)

#pragma warning disable IDE0060
// Profiler v3.1 compatible overload
public void Trace(IScriptExtent extent, ScriptBlock _, int __) => Trace(null, extent, _, __);
public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level) => Trace(null, extent, scriptBlock, level, null, null);

// Profiler v4 compatible overload
public void Trace(IScriptExtent extent, ScriptBlock _, int __, string ___, string ____) => Trace(null, extent, _, __);
public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName) => Trace(null, extent, scriptBlock, level, functionName, moduleName);
#pragma warning restore IDE0060
}
}
67 changes: 37 additions & 30 deletions src/csharp/Pester/Tracing/ExternalTracerAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
using System;
// Copied from Profiler module, branch: Fix-error-autodetection, commit: 150bbcf Fix error autodetection

using System;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Reflection;

namespace Pester.Tracing

#if PESTER
namespace Pester.Tracing;
#else
namespace Profiler;
#endif

class ExternalTracerAdapter : ITracer
{
class ExternalTracerAdapter : ITracer
{
private readonly object _tracer;
private readonly MethodInfo _traceMethod;
private readonly int _version = 0;
private readonly object _tracer;
private readonly MethodInfo _traceMethod;
private readonly int _version = 0;

public object Tracer => _tracer;
public object Tracer => _tracer;

public ExternalTracerAdapter(object tracer)
{
// We got tracer that is not using the same types that we use here. Find a method based on the signature
// and use that. This enables tracers to register even when they don't take dependency on our types e.g. Pester CC tracer.
public ExternalTracerAdapter(object tracer)
{
// We got tracer that is not using the same types that we use here. Find a method based on the signature
// and use that. This enables tracers to register even when they don't take dependency on our types e.g. Pester CC tracer.

_tracer = tracer ?? new NullReferenceException(nameof(tracer));
_version = 2;
var traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
_tracer = tracer ?? new NullReferenceException(nameof(tracer));
_version = 2;
var traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
typeof(string), // message
typeof(IScriptExtent), // extent
typeof(ScriptBlock), // scriptblock
typeof(int) }); // level

if (traceMethod == null)
{
_version = 1;
traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
if (traceMethod == null)
{
_version = 1;
traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
typeof(string), // message
typeof(IScriptExtent), // extent
typeof(ScriptBlock), // scriptblock
typeof(int) }); // level
}

_traceMethod = traceMethod ??
throw new InvalidOperationException("The provided tracer does not have Trace method with this signature: Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level) or Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level)");
}

public void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level)
{
var parameters = _version == 2
? new object[] { message, extent, scriptBlock, level }
: new object[] { extent, scriptBlock, level };
_traceMethod.Invoke(_tracer, parameters);
}
_traceMethod = traceMethod ??
throw new InvalidOperationException("The provided tracer does not have Trace method with this signature: Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level) or Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level)");
}

public void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName)
{
var parameters = _version == 2
? new object[] { message, extent, scriptBlock, level }
: new object[] { extent, scriptBlock, level };
_traceMethod.Invoke(_tracer, parameters);
}
}

17 changes: 11 additions & 6 deletions src/csharp/Pester/Tracing/ITracer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using System.Management.Automation;
// Copied from Profiler module, branch: Fix-error-autodetection, commit: 150bbcf Fix error autodetection

using System.Management.Automation;
using System.Management.Automation.Language;

namespace Pester.Tracing
# if PESTER
namespace Pester.Tracing;
#else
namespace Profiler;
#endif

public interface ITracer
{
public interface ITracer
{
void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level);
}
void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName);
}
Loading
Loading