ScriptCop.EzFormat.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "", Justification="Formatters can Write-Hosts")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Justification="PSScriptAnalyzer can't see the reference")]
param()
$moduleRoot = Get-Module "ScriptCop" | Split-Path

if (-not $moduleRoot) { return }


$formatting = @()

$formatting += Write-FormatView -TypeName ScriptCopError -Property Problem, ItemWithProblem -Wrap -GroupByProperty Rule
<#$formatting += Write-FormatView -TypeName ScriptCop.Test.Pass.Output -Action {
$writeColor = if ($_.Passed) {
    "DarkGreen"
} else {
    "Red"
}
$testStatus = if ($_.Passed) {
    "--- Passed --- "
} else {
    "*** Failed *** "
}
 
if (-not ($request -and $response)) {
 
 
$msg = " $($_.TestPass)"
 
$testStatus = $testStatus.PadLeft($host.ui.RawUI.BufferSize.Width - $msg.Length)
Write-Host " "
Write-Host "${Msg}$testStatus" -ForegroundColor $writeColor -NoNewline
Write-Host " "
$null= ($_.Results | Out-String)
 
 
''
} else {
"<div style='background-color:$writeColor;color:#ffffff'>
    <div style='float:left;width:20%;font-size:1.22em'>
        <h3>
          $($_.TestPass)
        </h3>
    </div>
    <div style='float:right;width:20%;text-align:right;'>
        <span style='color:#ffffff;font-size:1.22em'>$testStatus</span>
    </div>
    <br style='clear:both'/>
    <div style='width:80%;margin-left:20%'>
        $($_.Results | Out-html)
    </div>
</div>
"
}
}
$formatting += Write-FormatView -TypeName ScriptCop.Test.Output -action {
    $writeColor = if ($_.Passed) {
        "DarkGreen"
    } else {
        "Red"
    }
    $testStatus = if ($_.Passed) {
        "--- Passed --- "
    } else {
        "*** Failed *** "
    }
 
if (-not ($Request -and $response)) {
 
    $msg = " $($_.TestCase)"
    $testStatus = $testStatus.PadLeft($host.ui.RawUI.BufferSize.Width - $msg.Length)
    Write-Host "${Msg}$testStatus" -ForegroundColor $writeColor -NoNewline
 
    if ($_.Errors) {
Write-Host "$($_.Errors |Out-String)" -ForegroundColor $writeColor
    }
    ''
 
} else {
"
<div style='float:left;width:80%;font-size:1.11em'>
    <h4>
        $($_.TestCase)
    </h4>
    $(if ($_.Errors) {
"<pre>
$($_.Errors | Out-String)
</pre>"
    })
</div>
<div style='float:right;width:20%;font-size:1.11em;text-align:right'>
    <span style='font-size:1.11em'>$testStatus</span>
</div>
<br style='clear:both' />
"
}
}
#>

$formatting += Write-FormatView -TypeName ScriptCop.Test.Pass.Summary -Action {
$writeColor = if ($_.PercentPassingTestCases -eq 100) {
    "DarkGreen"
} elseif ($_.PercentPassingTestCases -ge 75)  {
    "DarkYellow"
} else {
    "Red"
}
    if (-not ($request -and $response)) {

$msg = "$($_.Module)"

$testStatus = [Math]::round($_.PercentPassingTestCases, 2) + " % Passed (Test Cases)"
$testStatus = $testStatus.PadLeft($host.ui.RawUI.BufferSize.Width - $msg.Length)
Write-Host "$msg{$testStatus}" -ForegroundColor $writecolor
Write-Host " "

$totalTestCasesMsg = "Total Test Cases : $($_.TotalTestCases)".PadRight(($host.ui.Length.rawui.buffersize / 2) -1 )
$passingTestCasesMsg = "$($_.PassingTestCases) ( $([Math]::Round($_.PercentPassingTestCases,2))% Passed".PadLeft(($host.ui.Length.rawui.buffersize / 2) -1)
$totalTestPassesMsg = "Total Test Passes : $($_.TotalTestPasses)".PadRight(($host.ui.Length.rawui.buffersize / 2) -1)
$passingTestPassesMsg = "$($_.PassingTestPasses) $([Math]::Round($_.PercentPassingTestPasses,2))% Passed".PadLeft(($host.ui.Length.rawui.buffersize / 2) -1)


Write-Host "${totalTestCasesMsg}${PassingTestCasesMsg}" -ForegroundColor $writecolor
Write-Host "${totalTestPassesMsg}${PassingTestPassesMsg}" -ForegroundColor $writecolor
Write-Host " "

    } else {
$summary = $_
$passingTestCasesGraphObject = New-Object PSObject |
Add-Member NoteProperty Passed $($summary.PassingTestCases) -Force -PassThru |
Add-Member NoteProperty Failed $($summary.TotalTestCases - $summary.PassingTestCases) -Force -PassThru


$passingTestPassesGraphObject =
New-Object PSObject |
Add-Member NoteProperty Passed $($summary.PassingTestPasses) -Force -PassThru |
Add-Member NoteProperty Failed $($summary.TotalTestPasses - $summary.PassingTestPasses) -Force -PassThru
"
<div>
    <h2>$($_.Module)</h2>
    <div style='width:40%;margin-left:5%;margin-right:5%;float:left;'>
        $($passingTestCasesGraphObject | Out-HTML -AsPieGraph -ColorList "#006400", "#800000" -GraphWidth 250 -GraphHeight 250 -Header "Test Cases")
    </div>
    <div style='width:40%;margin-left:5%;margin-right:5%;float:left;'>
        $($passingTestCasesGraphObject | Out-HTML -AsPieGraph -ColorList "#006400", "#800000" -GraphWidth 250 -GraphHeight 250 -Header "Test Passes")
    </div>
    <br style='clear:both' />
</div>
"

    }
}
$formatting += Write-FormatView -TypeName ScriptCop.Command.Coverage.Report -Action {
    if (-not ($request -and $response)) {
        $_ | Select-Object -Property PercentageCommandCoverage,
            NumberOfCommandsCovered,
            TotalNumberOfCommands,
            OverallParameterCoverage,
            ParameterCoverageInCoveredCommands,
            NumberOfParametersCovered,
            TotalNumberOfParameters |
            Out-Host
        ''
    } else {
$summary = $_
$overallCommandCoverage = New-Object PSObject |
Add-Member NoteProperty Covered $($summary.NumberOfCommandsCovered) -Force -PassThru |
Add-Member NoteProperty Uncovered $($summary.TotalNumberOfCommands - $summary.NumberOfCommandsCovered) -Force -PassThru


$coveredCommandParameterCoverage =
New-Object PSObject |
Add-Member NoteProperty Covered $($summary.NumberOfParametersCovered) -Force -PassThru |
Add-Member NoteProperty Uncovered $($summary.CoveredParameterTotal - $summary.NumberOfParametersCovered) -Force -PassThru


$totalParameterCoverage =
New-Object PSObject |
Add-Member NoteProperty Covered $($summary.NumberOfParametersCovered) -Force -PassThru |
Add-Member NoteProperty Uncovered $($summary.TotalNumberOfParameters - $summary.NumberOfParametersCovered) -Force -PassThru
"
<div>
    <h3>$($_.Module) - Command Coverage</h3>
    <div style='width:30%;margin-left:2.5%;margin-right:2.5%;float:left;'>
        $($overallCommandCoverage | Out-HTML -AsPieGraph -ColorList "#006400", "#800000" -GraphWidth 200 -GraphHeight 200 -Header "Overall Command Coverage")
    </div>
    <div style='width:30%;margin-left:2.5%;margin-right:2.5%;float:left;'>
        $($coveredCommandParameterCoverage | Out-HTML -AsPieGraph -ColorList "#006400", "#800000" -GraphWidth 200 -GraphHeight 200 -Header "Parameter Coverage (of Covered commands)")
    </div>
    <div style='width:30%;margin-left:2.5%;margin-right:2.5%;float:left;'>
        $($totalParameterCoverage | Out-HTML -AsPieGraph -ColorList "#006400", "#800000" -GraphWidth 200 -GraphHeight 200 -Header "Overall Parameter Coverage")
    </div>
    <br style='clear:both' />
</div>
"

    }
}

$formatting += @(Get-ChildItem -Path (Join-Path $moduleRoot 'Formatting') | Import-FormatView)

$formatting | Out-FormatData | Set-Content "$moduleRoot\ScriptCop.Format.ps1xml"