Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Filenames in JaCoCo report seem to be wrong

Open JohnnyDoesStuff opened this issue 1 month ago • 4 comments

Checklist

What is the issue?

We create code coverage files and want to visualize the results in Jenkins (https://www.jenkins.io/) by using the coverage plugin (https://plugins.jenkins.io/coverage/). After an update of that plugin results could no longer be parsed by it and it seems that the xml file produced by Pester is wrong (Discussed here: https://issues.jenkins.io/browse/JENKINS-76044)

Results look e.g. like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd"[]>
<report name="Pester (08/28/2025 05:08:10)">
  <sessioninfo id="this" start="1756357668799" dump="1756357690892" />
  <package name="myRepo/someDirectory/anotherDirectory">
    <class name="myRepo/someDirectory/anotherDirectory/foo" sourcefilename="someDirectory/anotherDirectory/foo.ps1">
      <method name="<script>" desc="()" line="16">
        <counter type="INSTRUCTION" missed="0" covered="31" />
        <counter type="LINE" missed="0" covered="22" />
        <counter type="METHOD" missed="0" covered="1" />
      </method>
      <counter type="INSTRUCTION" missed="0" covered="31" />
      <counter type="LINE" missed="0" covered="22" />
      <counter type="METHOD" missed="0" covered="1" />
      <counter type="CLASS" missed="0" covered="1" />
    </class>
    <sourcefile name="someDirectory/anotherDirectory/foo.ps1">
      <line nr="16" mi="0" ci="1" mb="0" cb="0" />
      <line nr="18" mi="0" ci="1" mb="0" cb="0" />
      <line nr="20" mi="0" ci="2" mb="0" cb="0" />
  // more lines
      <counter type="INSTRUCTION" missed="0" covered="31" />
      <counter type="LINE" missed="0" covered="22" />
      <counter type="METHOD" missed="0" covered="1" />
      <counter type="CLASS" missed="0" covered="1" />
    </sourcefile>
    <counter type="INSTRUCTION" missed="0" covered="31" />
    <counter type="LINE" missed="0" covered="22" />
    <counter type="METHOD" missed="0" covered="1" />
    <counter type="CLASS" missed="0" covered="1" />
  </package>
  // more packages
  

Expected Behavior

I'm not an expert for JaCoCo but i assume that this line <class name="myRepo/someDirectory/anotherDirectory/foo" sourcefilename="someDirectory/anotherDirectory/foo.ps1">

Should instead look like this <class name="myRepo/someDirectory/anotherDirectory/foo" sourcefilename="foo.ps1">

Probably other occurences of sourcefile also need to be adapted

Steps To Reproduce

  1. Create 2 modules (or directories)
  2. In each of those we need some code that does stuff
  3. Then each needs a file to run tests on those
  4. The tests are executed with a configuration like that:
Import-Module Pester
$config = [PesterConfiguration]@{
	CodeCoverage = @{
	    Enabled = $true
		Path = @(
			".\module1\public\Start-Foo.ps1"
			".\module2\public\Start-Bar.ps1"
		)
		OutputPath = ".\Temp\coverage.xml"
	}
	Run = @{
		Path = @(
			".\module1\Module1.Unit.Tests.ps1"
			".\module2\Module2.Unit.Tests.ps1"
		)
		PassThru = $true
	}
	Output = @{
		Verbosity = "Detailed"
	}
}
Invoke-Pester -Configuration $config

Describe your environment

Pester version : 5.7.1 C:\Users\z003sk5z\Documents\PowerShell\Modules\Pester\5.7.1\Pester.psm1 PowerShell version : 7.5.4 OS version : Microsoft Windows NT 10.0.22631.0

Possible Solution?

Probably something like this is needed to get the file names: Split-Path $fileName -Leaf

Probably more than this naive approach is needed.

JohnnyDoesStuff avatar Nov 18 '25 09:11 JohnnyDoesStuff

Yes exactly! The problem is, that when parsing the XML there is no obvious way for me to determine which of the parent folders of sourcefilename are required. So I am currently ignoring all parent folders and using the raw file name.

uhafner avatar Nov 18 '25 10:11 uhafner

Please try if OutputFormat = "CoverageGutters" will work for you, I think that outputs paths similar to what you describe.

https://github.com/pester/Pester/blob/main/src/csharp/Pester/CodeCoverageConfiguration.cs#L44C111-L44C126

nohwnd avatar Nov 18 '25 10:11 nohwnd

Please try if OutputFormat = "CoverageGutters" will work for you, I think that outputs paths similar to what you describe.

https://github.com/pester/Pester/blob/main/src/csharp/Pester/CodeCoverageConfiguration.cs#L44C111-L44C126

That seems to work. I ran a pipeline with that option set and coverage plugin version 2.3036.v90d485b_810c6. That way i got the same functionality as before, so that's a solution for me.

I don't know if this issue then needs to stay open / is useful, so i'm not closing it now.

@uhafner sorry for bothering you then. At least it looks like updating the plugin on our Jenkins should work now.

JohnnyDoesStuff avatar Nov 18 '25 14:11 JohnnyDoesStuff

I think the "coverage gutters" format is more useful as default. Keeping this open so we can test it and use as default for v6.

nohwnd avatar Nov 18 '25 14:11 nohwnd