Skip to content

Commit

Permalink
Add compliance rule for metric 'CommandsMissed' in group PerFunctionM…
Browse files Browse the repository at this point in the history
…etrics
  • Loading branch information
MathieuBuisson committed Jun 15, 2017
1 parent f0c16c4 commit a780c54
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 52 deletions.
11 changes: 9 additions & 2 deletions PSCodeHealth/PSCodeHealthSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"HigherIsBetter": true
}
},
{
"CommandsMissed": {
"WarningThreshold": 6,
"FailThreshold": 12,
"HigherIsBetter": false
}
},
{
"Complexity": {
"WarningThreshold": 15,
Expand Down Expand Up @@ -109,8 +116,8 @@
},
{
"CommandsMissedTotal": {
"WarningThreshold": 20,
"FailThreshold": 40,
"WarningThreshold": 200,
"FailThreshold": 400,
"HigherIsBetter": false
}
},
Expand Down
2 changes: 1 addition & 1 deletion PSCodeHealth/Public/Get-PSCodeHealthComplianceRule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Function Get-PSCodeHealthComplianceRule {
[string]$SettingsGroup,

[Parameter(Mandatory=$False,Position=2)]
[ValidateSet('LinesOfCode','ScriptAnalyzerFindings','TestCoverage','Complexity','MaximumNestingDepth','LinesOfCodeTotal',
[ValidateSet('LinesOfCode','ScriptAnalyzerFindings','TestCoverage','CommandsMissed','Complexity','MaximumNestingDepth','LinesOfCodeTotal',
'LinesOfCodeAverage','ScriptAnalyzerFindingsTotal','ScriptAnalyzerErrors','ScriptAnalyzerWarnings',
'ScriptAnalyzerInformation','ScriptAnalyzerFindingsAverage','NumberOfFailedTests','TestsPassRate',
'CommandsMissedTotal','ComplexityAverage','NestingDepthAverage')]
Expand Down
2 changes: 1 addition & 1 deletion PSCodeHealth/Public/Test-PSCodeHealthCompliance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Function Test-PSCodeHealthCompliance {
[string]$SettingsGroup,

[Parameter(Mandatory=$False,Position=3)]
[ValidateSet('LinesOfCode','ScriptAnalyzerFindings','TestCoverage','Complexity','MaximumNestingDepth','LinesOfCodeTotal',
[ValidateSet('LinesOfCode','ScriptAnalyzerFindings','TestCoverage','CommandsMissed','Complexity','MaximumNestingDepth','LinesOfCodeTotal',
'LinesOfCodeAverage','ScriptAnalyzerFindingsTotal','ScriptAnalyzerErrors','ScriptAnalyzerWarnings',
'ScriptAnalyzerInformation','ScriptAnalyzerFindingsAverage','NumberOfFailedTests','TestsPassRate',
'CommandsMissedTotal','ComplexityAverage','NestingDepthAverage')]
Expand Down
9 changes: 8 additions & 1 deletion Tests/Integration/Compliance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Describe 'Test-PSCodeHealthCompliance' {
$LinesOfCode = $Result.Where({$_.MetricName -eq 'LinesOfCode'})
$ScriptAnalyzerFindings = $Result.Where({$_.MetricName -eq 'ScriptAnalyzerFindings'})
$TestCoverage = $Result.Where({$_.MetricName -eq 'TestCoverage'})
$CommandsMissed = $Result.Where({$_.MetricName -eq 'CommandsMissed'})
$Complexity = $Result.Where({$_.MetricName -eq 'Complexity'})
$MaximumNestingDepth = $Result.Where({$_.MetricName -eq 'MaximumNestingDepth'})
$LinesOfCodeTotal = $Result.Where({$_.MetricName -eq 'LinesOfCodeTotal'})
Expand Down Expand Up @@ -43,6 +44,12 @@ Describe 'Test-PSCodeHealthCompliance' {
($TestCoverage | Where-Object SettingsGroup -eq 'PerFunctionMetrics').Result |
Should Be 'Fail'
}
It 'Should return correct value for the metric : CommandsMissed' {
$CommandsMissed.Value | Should Be 20
}
It 'Should return correct compliance result for the metric : CommandsMissed' {
$CommandsMissed.Result | Should Be 'Fail'
}
It 'Should return correct value for the metric : Complexity' {
$Complexity.Value | Should Be 5
}
Expand Down Expand Up @@ -97,7 +104,7 @@ Describe 'Test-PSCodeHealthCompliance' {
$CommandsMissedTotal.Value | Should Be 77
}
It 'Should return correct compliance result for the metric : CommandsMissedTotal' {
$CommandsMissedTotal.Result | Should Be 'Fail'
$CommandsMissedTotal.Result | Should Be 'Pass'
}
It 'Should return correct value for the metric : ComplexityAverage' {
$ComplexityAverage.Value | Should Be 2
Expand Down
8 changes: 4 additions & 4 deletions Tests/Unit/Private/Merge-PSCodeHealthSetting.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Describe 'Merge-PSCodeHealthSetting' {
$Result.OverallMetrics.TestsPassRate.FailThreshold | Should Be 97
}
It 'Resulting settings are the same as the defaults for metric "CommandsMissedTotal"' {
$Result.OverallMetrics.CommandsMissedTotal.WarningThreshold | Should Be 20
$Result.OverallMetrics.CommandsMissedTotal.FailThreshold | Should Be 40
$Result.OverallMetrics.CommandsMissedTotal.WarningThreshold | Should Be 200
$Result.OverallMetrics.CommandsMissedTotal.FailThreshold | Should Be 400
}
It 'Resulting settings override the defaults for metric "LinesOfCodeTotal"' {
$Result.OverallMetrics.LinesOfCodeTotal.WarningThreshold | Should Be 1500
Expand Down Expand Up @@ -107,8 +107,8 @@ Describe 'Merge-PSCodeHealthSetting' {
$Result.OverallMetrics.TestsPassRate.FailThreshold | Should Be 97
}
It 'Resulting settings are the same as the defaults for metric "CommandsMissedTotal"' {
$Result.OverallMetrics.CommandsMissedTotal.WarningThreshold | Should Be 20
$Result.OverallMetrics.CommandsMissedTotal.FailThreshold | Should Be 40
$Result.OverallMetrics.CommandsMissedTotal.WarningThreshold | Should Be 200
$Result.OverallMetrics.CommandsMissedTotal.FailThreshold | Should Be 400
}
It 'Resulting settings are the same as the defaults for metric "LinesOfCodeTotal"' {
$Result.OverallMetrics.LinesOfCodeTotal.WarningThreshold | Should Be 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Describe 'New-PSCodeHealthComplianceResult' {
InModuleScope $ModuleName {

$ComplianceRule = Get-PSCodeHealthComplianceRule -MetricName Complexity
$ComplianceResult = 'Pass'

Context 'The specified Value is Null' {

Expand Down
25 changes: 17 additions & 8 deletions Tests/Unit/Public/Get-PSCodeHealthComplianceRule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ Import-Module "$PSScriptRoot\..\..\..\$ModuleName\$($ModuleName).psd1" -Force
Describe 'Get-PSCodeHealthComplianceRule' {
InModuleScope $ModuleName {

$DefaultSettings = ConvertFrom-Json (Get-Content -Path "$PSScriptRoot\..\..\..\PSCodeHealth\PSCodeHealthSettings.json" -Raw)

Context 'The file specified via the CustomSettingsPath parameter does not contain valid JSON' {

$InvalidJsonPath = "$PSScriptRoot\..\..\TestData\InvalidSettings.json"
Expand All @@ -28,7 +26,7 @@ Describe 'Get-PSCodeHealthComplianceRule' {
}
It 'Should return 5 objects where the SettingsGroup property is equal to "PerFunctionMetrics"' {
($Results | Where-Object SettingsGroup -eq 'PerFunctionMetrics').Count |
Should Be 5
Should Be 6
}
It 'Should return 13 objects where the SettingsGroup property is equal to "OverallMetrics"' {
($Results | Where-Object SettingsGroup -eq 'OverallMetrics').Count |
Expand All @@ -46,6 +44,12 @@ Describe 'Get-PSCodeHealthComplianceRule' {
$TestCoverageResult.FailThreshold | Should Be 70
$TestCoverageResult.HigherIsBetter | Should Be $True
}
It 'Resulting compliance rules are the same as the defaults for metric "CommandsMissed"' {
$CommandsMissedResult = $Results | Where-Object MetricName -eq CommandsMissed
$CommandsMissedResult.WarningThreshold | Should Be 6
$CommandsMissedResult.FailThreshold | Should Be 12
$CommandsMissedResult.HigherIsBetter | Should Be $False
}
It 'Resulting compliance rules are the same as the defaults for metric "Complexity"' {
$ComplexityResult = $Results | Where-Object MetricName -eq Complexity
$ComplexityResult.WarningThreshold | Should Be 15
Expand Down Expand Up @@ -81,7 +85,7 @@ Describe 'Get-PSCodeHealthComplianceRule' {
}
It 'Should return 5 objects where the SettingsGroup property is equal to "PerFunctionMetrics"' {
($Results | Where-Object SettingsGroup -eq 'PerFunctionMetrics').Count |
Should Be 5
Should Be 6
}
It 'Should return 13 objects where the SettingsGroup property is equal to "OverallMetrics"' {
($Results | Where-Object SettingsGroup -eq 'OverallMetrics').Count |
Expand All @@ -99,6 +103,12 @@ Describe 'Get-PSCodeHealthComplianceRule' {
$TestCoverageResult.FailThreshold | Should Be 70
$TestCoverageResult.HigherIsBetter | Should Be $True
}
It 'Resulting compliance rules are the same as the defaults for metric "CommandsMissed"' {
$CommandsMissedResult = $Results | Where-Object MetricName -eq CommandsMissed
$CommandsMissedResult.WarningThreshold | Should Be 6
$CommandsMissedResult.FailThreshold | Should Be 12
$CommandsMissedResult.HigherIsBetter | Should Be $False
}
It 'Resulting compliance rules are the same as the defaults for metric "Complexity"' {
$ComplexityResult = $Results | Where-Object MetricName -eq Complexity
$ComplexityResult.WarningThreshold | Should Be 15
Expand Down Expand Up @@ -287,7 +297,7 @@ Describe 'Get-PSCodeHealthComplianceRule' {
}
It 'Should return 5 objects where the SettingsGroup property is equal to "PerFunctionMetrics"' {
($Results | Where-Object SettingsGroup -eq 'PerFunctionMetrics').Count |
Should Be 5
Should Be 6
}
It 'Should return 13 objects where the SettingsGroup property is equal to "OverallMetrics"' {
($Results | Where-Object SettingsGroup -eq 'OverallMetrics').Count |
Expand Down Expand Up @@ -325,8 +335,8 @@ Describe 'Get-PSCodeHealthComplianceRule' {
}
It 'Resulting compliance rules are the same as the defaults for metric "CommandsMissedTotal"' {
$CommandsMissedTotalResult = $Results | Where-Object MetricName -eq CommandsMissedTotal
$CommandsMissedTotalResult.WarningThreshold | Should Be 20
$CommandsMissedTotalResult.FailThreshold | Should Be 40
$CommandsMissedTotalResult.WarningThreshold | Should Be 200
$CommandsMissedTotalResult.FailThreshold | Should Be 400
$CommandsMissedTotalResult.HigherIsBetter | Should Be $False
}
It 'Resulting compliance rules override the defaults for metric "LinesOfCodeTotal"' {
Expand All @@ -346,6 +356,5 @@ Describe 'Get-PSCodeHealthComplianceRule' {
Should Be 2
}
}
Remove-Variable -Name 'DefaultSettings' -Force -ErrorAction SilentlyContinue
}
}
11 changes: 9 additions & 2 deletions Tests/Unit/Public/Test-PSCodeHealthCompliance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Import-Module "$PSScriptRoot\..\..\..\$ModuleName\$($ModuleName).psd1" -Force
Describe 'Test-PSCodeHealthCompliance' {
InModuleScope $ModuleName {

$ScriptPath = $PSScriptRoot
$Mocks = ConvertFrom-Json (Get-Content -Path "$($PSScriptRoot)\..\..\TestData\MockObjects.json" -Raw )

Context 'The specified health report does not contain any FunctionHealthRecord' {
Expand Down Expand Up @@ -68,7 +67,7 @@ Describe 'Test-PSCodeHealthCompliance' {
Context 'The specified health report contains 2 FunctionHealthRecords' {
$HealthReport = $Mocks.'Invoke-PSCodeHealth'.'2FunctionHealthRecords' | Where-Object { $_ }
$HealthReport.psobject.TypeNames.Insert(0, 'PSCodeHealth.Overall.HealthReport')
$MetricsToTest = @('TestsPassRate','LinesOfCodeTotal','LinesOfCodeAverage','ScriptAnalyzerErrors','ScriptAnalyzerWarnings','Complexity','MaximumNestingDepth','LinesOfCode')
$MetricsToTest = @('TestsPassRate','LinesOfCodeTotal','LinesOfCodeAverage','ScriptAnalyzerErrors','ScriptAnalyzerWarnings','CommandsMissed','Complexity','MaximumNestingDepth','LinesOfCode')
$Results = Test-PSCodeHealthCompliance -HealthReport $HealthReport -MetricName $MetricsToTest

It 'Should return objects of the type [PSCodeHealth.Compliance.Result]' {
Expand Down Expand Up @@ -117,6 +116,14 @@ Describe 'Test-PSCodeHealthCompliance' {
$ScriptAnalyzerWarningsResult.Value | Should Be 4
$ScriptAnalyzerWarningsResult.Result | Should Be 'Pass'
}
It 'Resulting compliance rules are the same as the defaults for metric "CommandsMissed"' {
$CommandsMissedResult = $Results.Where({$_.MetricName -eq 'CommandsMissed'})
$CommandsMissedResult.WarningThreshold | Should Be 6
$CommandsMissedResult.FailThreshold | Should Be 12
$CommandsMissedResult.HigherIsBetter | Should Be $False
$CommandsMissedResult.Value | Should Be 12
$CommandsMissedResult.Result | Should Be 'Warning'
}
It 'Resulting compliance rules are the same as the defaults for metric "Complexity"' {
$ComplexityResult = $Results.Where({$_.MetricName -eq 'Complexity'})
$ComplexityResult.WarningThreshold | Should Be 15
Expand Down

0 comments on commit a780c54

Please sign in to comment.