Skip to content

Commit

Permalink
Merge changes from Profiler so we can share the code easily
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed May 23, 2024
1 parent bb0dfae commit 7cb9c08
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 354 deletions.
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
6 changes: 3 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 Down Expand Up @@ -106,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
}
}
63 changes: 34 additions & 29 deletions src/csharp/Pester/Tracing/ExternalTracerAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,54 @@
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);
}
}

13 changes: 8 additions & 5 deletions src/csharp/Pester/Tracing/ITracer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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

0 comments on commit 7cb9c08

Please sign in to comment.