Skip to content

Commit

Permalink
Report code coverage that includes P tests (#2478)
Browse files Browse the repository at this point in the history
* Enable code coverage before P tests and stop it after Pester tests

* this gets me to the point where I can run the whole coverage

* Merge changes from Profiler so we can share the code easily

* Measure cc

* Fix inline

* report to xml

* Absolute path

* Add sync script and sync

* Exclude profiler script from scanning

* Write xml

* Coveragepublish@1

* remove cc debug message
  • Loading branch information
nohwnd authored Jun 5, 2024
1 parent 6271126 commit 42db191
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 383 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Code analysis

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -32,6 +32,7 @@ jobs:
Import-Module ConvertToSARIF -Force
Get-ChildItem -Path ./src/ -Filter *.ps* -Recurse -File |
Where-Object { $_.Name -ne 'Sync-WithProfiler.ps1' } |
Invoke-ScriptAnalyzer -Settings ./.github/workflows/PSScriptAnalyzerSettings.psd1 |
ConvertTo-SARIF -FilePath results.sarif
Expand Down
7 changes: 4 additions & 3 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ stages:
targetType: inline
pwsh: $(pwsh)
script: |
& ./test.ps1 -CI -PassThru -NoBuild
& ./test.ps1 -CI -CC -PassThru -NoBuild
workingDirectory: '$(Build.SourcesDirectory)'
- task: PublishCodeCoverageResults@2
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: 'JaCoCo'
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
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 @@ -102,10 +102,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

0 comments on commit 42db191

Please sign in to comment.