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

Add Coverage to result object #1860

Merged
merged 4 commits into from
Mar 6, 2021
Merged

Add Coverage to result object #1860

merged 4 commits into from
Mar 6, 2021

Conversation

nohwnd
Copy link
Member

@nohwnd nohwnd commented Mar 6, 2021

Added Coverage to the Result object, and printing of the report to screen. The same report is reported to the CoverageObject.

Fix #1625

@nohwnd
Copy link
Member Author

nohwnd commented Mar 6, 2021

Example of the output:


CoveragePercent       : 25
CoverageReport        : Code coverage report:
                         Covered 25.00 % of 8 analyzed Commands in 2 Files.
                         Missed commands:

                        File    Class Function Line Command
                        ----    ----- -------- ---- -------
                        f1.ps1        f           5 "nope"
                        run.ps1                   1 /p/pester/build.ps1
                        run.ps1                   3 get-module pester
                        run.ps1                   3 Remove-Module
                        run.ps1                   4 import-module /p/pester/bin/pester.psd1
                        run.ps1                   6 $r = Invoke-Pester -ci $PSScriptRoot -PassThru



CommandsAnalyzedCount : 8
CommandsExecutedCount : 2
CommandsMissedCount   : 6
FilesAnalyzedCount    : 2
CommandsMissed        : {@{File=C:\Users\jajares\Desktop\coverage\f1.ps1; Line=5; StartLine=5; EndLine=5; StartColumn=9; EndColumn=15; Class=; Function=f; Command="nope"; HitCount=0}, @{File=C:\Users\jajares\Desktop\coverage\run.ps1; Line=1; StartLine=1; EndLine=1; StartColumn=1; EndColumn=20; Class=; Function=; Command=/p/pester/build.ps1; HitCount=0},
                        @{File=C:\Users\jajares\Desktop\coverage\run.ps1; Line=3; StartLine=3; EndLine=3; StartColumn=1; EndColumn=18; Class=; Function=; Command=get-module pester; HitCount=0}, @{File=C:\Users\jajares\Desktop\coverage\run.ps1; Line=3; StartLine=3; EndLine=3; StartColumn=21; EndColumn=34; Class=; Function=; Command=Remove-Module; HitCount=0}…}
CommandsExecuted      : {@{File=C:\Users\jajares\Desktop\coverage\f1.ps1; Line=2; StartLine=2; EndLine=2; StartColumn=5; EndColumn=10; Class=; Function=f; Command="yes"; HitCount=1}, @{File=C:\Users\jajares\Desktop\coverage\f1.ps1; Line=4; StartLine=4; EndLine=4; StartColumn=9; EndColumn=15; Class=; Function=f; Command=if ($false) {
                                "nope"
                            }; HitCount=1}}
FilesAnalyzed         : {C:\Users\jajares\Desktop\coverage\f1.ps1, C:\Users\jajares\Desktop\coverage\run.ps1

@@ -294,13 +294,14 @@ function Write-PesterReport {
function Write-CoverageReport {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add comment-based help here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's internal function, and it's a bit ugly that it writes screen and writes the report as well, but at least that makes they are the same. I would document only if the function is public.

@@ -614,6 +614,7 @@ function Get-CoverageReport {
MissedCommands = $missedCommands
HitCommands = $hitCommands
AnalyzedFiles = $analyzedFiles
CoveragePercent = if ($null -eq $CommandCoverage -or $CommandCoverage.Count -eq 0) { 0 } else { ($hitCommands.Count / $CommandCoverage.Count) * 100 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could string formatting be used here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid that because I don't want to force people to parse the value out if they need the raw value. They can add "%" easily, if they want.

@@ -1122,6 +1122,21 @@ function Invoke-Pester {
$totalMilliseconds = $run.Duration.TotalMilliseconds
$jaCoCoReport = Get-JaCoCoReportXml -CommandCoverage $breakpoints -TotalMilliseconds $totalMilliseconds -CoverageReport $coverageReport
$jaCoCoReport | & $SafeCommands['Out-File'] $PesterPreference.CodeCoverage.OutputPath.Value -Encoding $PesterPreference.CodeCoverage.OutputEncoding.Value
$reportText = Write-CoverageReport $coverageReport
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this work be done outside of Pester.ps1, perhaps in Coverage? Invoke-Pester has a complexity metric of 348 according to Invoke-PsCodeHealth and could be split up? Might be another PR...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not happy about writing the screen and report at the same time. But for the moment it is imho okay.


public override string ToString()
{
return CoveragePercent.ToString("N2 %");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would "P" work here instead of N2 %?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 1.000m in unit test with this and got N2 % back. Consider adding unit test to code with some cases?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -45,5 +47,10 @@ public static ErrorRecord CreateErrorRecord(string errorId, string message, stri
var errorRecord = new ErrorRecord(exception, errorId, errorCategory, targetObject);
return errorRecord;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but I was considering using it. Will remove it later if I don't need it.


public decimal CoveragePercent { get; set; }

public string CoverageReport { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be declared as nullable?

public string? CoverageReport { get; set; }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project does not use nullability annotations.

@nohwnd nohwnd merged commit 33a643f into v5.0 Mar 6, 2021
@nohwnd nohwnd deleted the print-cc-report branch March 6, 2021 21:34
@nohwnd
Copy link
Member Author

nohwnd commented Mar 6, 2021

Thanks for the comments. I merged the changes because I need them for my other fixes. How about the functionality? Looks to me like it works okay, apart from the percentage in the ToString?

@johlju
Copy link
Contributor

johlju commented Mar 8, 2021

@nohwnd Late to the party, but have just one thought. Looks good, but just wonder what was you thought of having pre-formatted text in the property CoverageReport? It looks like we have the information needed in the properties to create a command Out-PesterCoverage that can format the result object? 🤔

@nohwnd
Copy link
Member Author

nohwnd commented Mar 8, 2021

@johlju Not sure what you are asking. The report that is printed on the screen is echoed in the CoverageReport property. Plus, it always includes missing commands report, which I don't print to screen when verbosity is < Detailed. All the info that is used to construct that report is on the object as well. So your question is why I did it?

@nohwnd
Copy link
Member Author

nohwnd commented Mar 8, 2021

Renamed the property on the result object from Coverage to CodeCoverage, in case someone is looking at this and have 5.2.0-alpha2 or newer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CodeCoverage summary report missing
3 participants