Skip to content

Commit

Permalink
Add function New-PSCodeHealthTableData to generate table rows for the…
Browse files Browse the repository at this point in the history
… HTML report
  • Loading branch information
MathieuBuisson committed Jul 2, 2017
1 parent eb641bb commit a502db1
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 18 deletions.
11 changes: 5 additions & 6 deletions PSCodeHealth/Assets/HealthReport.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
<div class="container-fluid">
<header class="container-fluid">
Expand Down Expand Up @@ -202,7 +201,7 @@ <h2 class="panel-title"> Per Function Information</h2>
</tr>
</thead>
<tbody>
{BEST_PRACTICES_TABLE_ROWS}
{BEST_PRACTICES_TABLE_ROWS}
</tbody>
</table>
</div>
Expand Down Expand Up @@ -290,7 +289,7 @@ <h2 class="panel-title"> Per Function Information</h2>
</tr>
</thead>
<tbody>
{MAINTAINABILITY_TABLE_ROWS}
{MAINTAINABILITY_TABLE_ROWS}
</tbody>
</table>
</div>
Expand Down Expand Up @@ -379,7 +378,7 @@ <h2 class="panel-title"> Failed Tests Details</h2>
</tr>
</thead>
<tbody>
{FAILED_TESTS_TABLE_ROWS}
{FAILED_TESTS_TABLE_ROWS}
</tbody>
</table>
</div>
Expand All @@ -400,7 +399,7 @@ <h2 class="panel-title"> Per Function Information</h2>
</tr>
</thead>
<tbody>
{COVERAGE_TABLE_ROWS}
{COVERAGE_TABLE_ROWS}
</tbody>
</table>
</div>
Expand All @@ -416,4 +415,4 @@ <h2 class="panel-title"> Per Function Information</h2>
{JS_CONTENT}
</script>
</body>
</html>
</html>
2 changes: 1 addition & 1 deletion PSCodeHealth/Assets/HealthReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ Chart.pluginService.register({
$(this).text($(this).text() == ' Expand' ? ' Collapse' : ' Expand');
});
}
});
});
134 changes: 134 additions & 0 deletions PSCodeHealth/Private/HtmlReport/New-PSCodeHealthTableData.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
Function New-PSCodeHealthTableData {
<#
.SYNOPSIS
Generate table rows for the HTML report, based on the data contained in a PSCodeHealth.Overall.HealthReport object.
.DESCRIPTION
Generate table rows for the HTML report, based on the data contained in a PSCodeHealth.Overall.HealthReport object.
This provides the rows for the following tables :
- Best Practices (per function)
- Maintainability (per function)
- Failed Tests Details
- Test Coverage (per function)
.PARAMETER HealthReport
To specify the input PSCodeHealth.Overall.HealthReport object containing the data.
.EXAMPLE
New-PSCodeHealthTableData -HealthReport $HealthReport
This generates the rows for the tables Best Practices, Maintainability, Failed Tests Details and Test Coverage tables, based on the data in $HealthReport.
.OUTPUTS
PSCustomObject
.NOTES
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
Param (
[Parameter(Mandatory, Position=0)]
[PSTypeName('PSCodeHealth.Overall.HealthReport')]
[PSCustomObject]$HealthReport
)

[System.Collections.ArrayList]$BestPracticesRows = @()
Foreach ( $Function in $HealthReport.FunctionHealthRecords ) {

If ( $Function.ScriptAnalyzerFindings -gt 0 ) {
[System.Collections.ArrayList]$FindingsDetails = @()
$Null = $FindingsDetails.Add(@"
`n <button type="button" class="btn btn-sm cell-expand-collapse"> Expand</button>
<table>`n
"@)
Foreach ( $Finding in $Function.ScriptAnalyzerResultDetails ) {

$ScriptName = Split-Path -Path $Function.FilePath -Leaf
$FindingDetail = @"
<tr>
<td class="cell-largeContent">ScriptName : $ScriptName<br>
Line (in the function) : $($Finding.Line)<br>
Severity : $($Finding.Severity)<br>
RuleName : $($Finding.RuleName)<br>
Message : $($Finding.Message)<br>
</td>
</tr>`n
"@
$Null = $FindingsDetails.Add($FindingDetail)
}
$CloseTable = @"
</table>`n
"@
$Null = $FindingsDetails.Add($CloseTable)
}
Else {
[string]$FindingsDetails = ''
}
$Row = @"
<tr>
<td>$($Function.FunctionName)</td>
<td>$($Function.ScriptAnalyzerFindings)</td>
<td>$($FindingsDetails)</td>
<td>$($Function.ContainsHelp)</td>
</tr>
"@
$Null = $BestPracticesRows.Add($Row)
}

[System.Collections.ArrayList]$MaintainabilityRows = @()
Foreach ( $Function in $HealthReport.FunctionHealthRecords ) {

$Row = @"
<tr>
<td>$($Function.FunctionName)</td>
<td>$($Function.LinesOfCode)</td>
<td>$($Function.Complexity)</td>
<td>$($Function.MaximumNestingDepth)</td>
</tr>
"@
$Null = $MaintainabilityRows.Add($Row)
}

If ( $HealthReport.NumberOfFailedTests -gt 0 ) {
[System.Collections.ArrayList]$FailedTestsRows = @()
Foreach ( $FailedTest in $HealthReport.FailedTestsDetails ) {

$Row = @"
<tr>
<td>$($FailedTest.File)</td>
<td>$($FailedTest.Line)</td>
<td>$($FailedTest.Describe)</td>
<td>$($FailedTest.TestName)</td>
<td>$($FailedTest.ErrorMessage)</td>
</tr>
"@
$Null = $FailedTestsRows.Add($Row)
}
}
Else {
[string]$FailedTestsRows = ''
}

[System.Collections.ArrayList]$CoverageRows = @()
Foreach ( $Function in $HealthReport.FunctionHealthRecords ) {

$Row = @"
<tr>
<td>$($Function.FunctionName)</td>
<td>$($Function.TestCoverage)</td>
<td>$($Function.CommandsMissed)</td>
</tr>
"@
$Null = $CoverageRows.Add($Row)
}

$ObjectProperties = [ordered]@{
'BestPracticesRows' = $BestPracticesRows
'MaintainabilityRows' = $MaintainabilityRows
'FailedTestsRows' = $FailedTestsRows
'CoverageRows' = $CoverageRows
}

$CustomObject = New-Object -TypeName PSObject -Property $ObjectProperties
return $CustomObject
}
12 changes: 7 additions & 5 deletions PSCodeHealth/Public/Invoke-PSCodeHealth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Function Invoke-PSCodeHealth {
}
$JsContent = Set-PSCodeHealthPlaceholdersValue -TemplatePath "$PSScriptRoot\..\Assets\HealthReport.js" -PlaceholdersData $JsPlaceholders

$TableData = New-PSCodeHealthTableData -HealthReport $HealthReport

$HtmlPlaceholders = @{
REPORT_TITLE = $HealthReport.ReportTitle
CSS_CONTENT = Get-Content -Path "$PSScriptRoot\..\Assets\HealthReport.css"
Expand All @@ -197,25 +199,25 @@ Function Invoke-PSCodeHealth {
SCRIPTANALYZER_TOTAL = $HealthReport.ScriptAnalyzerFindingsTotal
SCRIPTANALYZER_AVERAGE = $HealthReport.ScriptAnalyzerFindingsAverage
FUNCTIONS_WITHOUT_HELP = $HealthReport.FunctionsWithoutHelp
BEST_PRACTICES_TABLE_ROWS = ''
BEST_PRACTICES_TABLE_ROWS = $TableData.BestPracticesRows
COMPLEXITY_HIGHEST = $HealthReport.ComplexityHighest
NESTING_DEPTH_HIGHEST = $HealthReport.NestingDepthHighest
LINES_OF_CODE_AVERAGE = $HealthReport.LinesOfCodeAverage
COMPLEXITY_AVERAGE = $HealthReport.ComplexityAverage
NESTING_DEPTH_AVERAGE = $HealthReport.NestingDepthAverage
MAINTAINABILITY_TABLE_ROWS = ''
MAINTAINABILITY_TABLE_ROWS = $TableData.MaintainabilityRows
NUMBER_OF_TESTS = $HealthReport.NumberOfTests
NUMBER_OF_FAILED_TESTS = $HealthReport.NumberOfFailedTests
NUMBER_OF_PASSED_TESTS = $HealthReport.NumberOfPassedTests
COMMANDS_MISSED = $HealthReport.CommandsMissedTotal
FAILED_TESTS_TABLE_ROWS = ''
COVERAGE_TABLE_ROWS = ''
FAILED_TESTS_TABLE_ROWS = $TableData.FailedTestsRows
COVERAGE_TABLE_ROWS = $TableData.CoverageRows
JS_CONTENT = $JsContent
}
$HtmlContent = Set-PSCodeHealthPlaceholdersValue -TemplatePath "$PSScriptRoot\..\Assets\HealthReport.html" -PlaceholdersData $HtmlPlaceholders

$Null = New-Item -Path $HtmlReportPath -ItemType File -Force
Set-Content -Path $HtmlReportPath -Value $HtmlContent -Encoding UTF8
Set-Content -Path $HtmlReportPath -Value $HtmlContent
If ( $PassThru ) {
return $HealthReport
}
Expand Down
Empty file.
6 changes: 0 additions & 6 deletions Tests/Unit/Public/Invoke-PSCodeHealth2.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ Describe 'Invoke-PSCodeHealth (again)' {
}
Context 'The HtmlReportPath parameter is specified but not PassThru' {

$PesterResult = $Mocks.'Invoke-Pester'.'NumberOfTests' | Where-Object { $_ }
$HealthReportParams = @{
Path = "$TestDrive\2PublicFunctions.psm1"
TestsResult = $PesterResult
HtmlReportPath = "$TestDrive\Report.html"
}
$Result = Invoke-PSCodeHealth @HealthReportParams
Expand All @@ -298,10 +296,8 @@ Describe 'Invoke-PSCodeHealth (again)' {
}
Context 'The HtmlReportPath and PassThru parameters are both specified' {

$PesterResult = $Mocks.'Invoke-Pester'.'NumberOfTests' | Where-Object { $_ }
$HealthReportParams = @{
Path = "$TestDrive\2PublicFunctions.psm1"
TestsResult = $PesterResult
HtmlReportPath = "$TestDrive\Report2.html"
PassThru = $True
}
Expand All @@ -320,10 +316,8 @@ Describe 'Invoke-PSCodeHealth (again)' {
}
Context 'The PassThru parameter is specified but not HtmlReportPath' {

$PesterResult = $Mocks.'Invoke-Pester'.'NumberOfTests' | Where-Object { $_ }
$HealthReportParams = @{
Path = "$TestDrive\2PublicFunctions.psm1"
TestsResult = $PesterResult
HtmlReportPath = $Null
PassThru = $True
}
Expand Down

0 comments on commit a502db1

Please sign in to comment.