Skip to content

Commit 42db191

Browse files
authored
Report code coverage that includes P tests (#2478)
* 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
1 parent 6271126 commit 42db191

File tree

12 files changed

+490
-383
lines changed

12 files changed

+490
-383
lines changed

.github/workflows/code-analysis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Code analysis
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88
workflow_dispatch:
99

1010
jobs:
@@ -32,6 +32,7 @@ jobs:
3232
Import-Module ConvertToSARIF -Force
3333
3434
Get-ChildItem -Path ./src/ -Filter *.ps* -Recurse -File |
35+
Where-Object { $_.Name -ne 'Sync-WithProfiler.ps1' } |
3536
Invoke-ScriptAnalyzer -Settings ./.github/workflows/PSScriptAnalyzerSettings.psd1 |
3637
ConvertTo-SARIF -FilePath results.sarif
3738

azure-pipelines.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,13 @@ stages:
112112
targetType: inline
113113
pwsh: $(pwsh)
114114
script: |
115-
& ./test.ps1 -CI -PassThru -NoBuild
115+
& ./test.ps1 -CI -CC -PassThru -NoBuild
116116
workingDirectory: '$(Build.SourcesDirectory)'
117-
- task: PublishCodeCoverageResults@2
117+
- task: PublishCodeCoverageResults@1
118118
inputs:
119+
codeCoverageTool: 'JaCoCo'
119120
summaryFileLocation: 'coverage.xml'
120-
pathToSources: 'src/'
121+
pathToSources: '$(Build.SourcesDirectory)/bin/'
121122
failIfCoverageEmpty: false
122123
condition: succeededOrFailed()
123124
- task: PublishTestResults@2

src/csharp/Pester/Pester.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<DebugType>embedded</DebugType>
88
</PropertyGroup>
99

10+
<PropertyGroup>
11+
<DefineConstants>$(DefineConstants);PESTER</DefineConstants>
12+
</PropertyGroup>
13+
1014
<!-- PowerShell 7.2.x is the oldest supported PowerShell version. That version is built using net6.0.
1115
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:
1216
https://github.com/PowerShell/PowerShell/issues/16408

src/csharp/Pester/Tracing/CodeCoverageTracer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public CodeCoverageTracer(List<CodeCoveragePoint> points)
4848
// keyed as path -> line:column -> CodeCoveragePoint
4949
public Dictionary<string, Dictionary<string, List<CodeCoveragePoint>>> Hits { get; } = new Dictionary<string, Dictionary<string, List<CodeCoveragePoint>>>(StringComparer.OrdinalIgnoreCase);
5050

51-
public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __)
51+
public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __, string ___, string ____)
5252
{
5353
if (_debug && extent?.File != null && CultureInfo.InvariantCulture.CompareInfo.IndexOf(extent.File, _debugFile, CompareOptions.OrdinalIgnoreCase) >= 0)
5454
{
@@ -102,10 +102,10 @@ public void Trace(string message, IScriptExtent extent, ScriptBlock _, int __)
102102

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

107107
// Profiler v4 compatible overload
108-
public void Trace(IScriptExtent extent, ScriptBlock _, int __, string ___, string ____) => Trace(null, extent, _, __);
108+
public void Trace(IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName) => Trace(null, extent, scriptBlock, level, functionName, moduleName);
109109
#pragma warning restore IDE0060
110110
}
111111
}
Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
1-
using System;
1+
// Copied from Profiler module, branch: Fix-error-autodetection, commit: 150bbcf Fix error autodetection
2+
3+
using System;
24
using System.Management.Automation;
35
using System.Management.Automation.Language;
46
using System.Reflection;
57

6-
namespace Pester.Tracing
8+
9+
#if PESTER
10+
namespace Pester.Tracing;
11+
#else
12+
namespace Profiler;
13+
#endif
14+
15+
class ExternalTracerAdapter : ITracer
716
{
8-
class ExternalTracerAdapter : ITracer
9-
{
10-
private readonly object _tracer;
11-
private readonly MethodInfo _traceMethod;
12-
private readonly int _version = 0;
17+
private readonly object _tracer;
18+
private readonly MethodInfo _traceMethod;
19+
private readonly int _version = 0;
1320

14-
public object Tracer => _tracer;
21+
public object Tracer => _tracer;
1522

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

21-
_tracer = tracer ?? new NullReferenceException(nameof(tracer));
22-
_version = 2;
23-
var traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
28+
_tracer = tracer ?? new NullReferenceException(nameof(tracer));
29+
_version = 2;
30+
var traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
2431
typeof(string), // message
2532
typeof(IScriptExtent), // extent
2633
typeof(ScriptBlock), // scriptblock
2734
typeof(int) }); // level
2835

29-
if (traceMethod == null)
30-
{
31-
_version = 1;
32-
traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
36+
if (traceMethod == null)
37+
{
38+
_version = 1;
39+
traceMethod = tracer.GetType().GetMethod("Trace", new Type[] {
3340
typeof(string), // message
3441
typeof(IScriptExtent), // extent
3542
typeof(ScriptBlock), // scriptblock
3643
typeof(int) }); // level
37-
}
38-
39-
_traceMethod = traceMethod ??
40-
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)");
4144
}
4245

43-
public void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level)
44-
{
45-
var parameters = _version == 2
46-
? new object[] { message, extent, scriptBlock, level }
47-
: new object[] { extent, scriptBlock, level };
48-
_traceMethod.Invoke(_tracer, parameters);
49-
}
46+
_traceMethod = traceMethod ??
47+
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)");
48+
}
49+
50+
public void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName)
51+
{
52+
var parameters = _version == 2
53+
? new object[] { message, extent, scriptBlock, level }
54+
: new object[] { extent, scriptBlock, level };
55+
_traceMethod.Invoke(_tracer, parameters);
5056
}
5157
}
58+
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
using System.Management.Automation;
1+
// Copied from Profiler module, branch: Fix-error-autodetection, commit: 150bbcf Fix error autodetection
2+
3+
using System.Management.Automation;
24
using System.Management.Automation.Language;
35

4-
namespace Pester.Tracing
6+
# if PESTER
7+
namespace Pester.Tracing;
8+
#else
9+
namespace Profiler;
10+
#endif
11+
12+
public interface ITracer
513
{
6-
public interface ITracer
7-
{
8-
void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level);
9-
}
14+
void Trace(string message, IScriptExtent extent, ScriptBlock scriptBlock, int level, string functionName, string moduleName);
1015
}

0 commit comments

Comments
 (0)