|
| 1 | +$Here = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 2 | +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.') |
| 3 | +. "$here\$sut" |
| 4 | +"$here\$sut" |
| 5 | +Import-Module PSScriptAnalyzer |
| 6 | +$Rules = Get-ScriptAnalyzerRule |
| 7 | +$Name = $sut.Split('.')[0] |
| 8 | + |
| 9 | + Describe 'Script Analyzer Tests' { |
| 10 | + Context 'Testing $sut for Standard Processing' { |
| 11 | + foreach ($rule in $rules.Where{$_.RuleName -ne 'PSAvoidUsingWMICmdlet'}) { |
| 12 | + $i = $rules.IndexOf($rule) |
| 13 | + It "passes the PSScriptAnalyzer Rule number $i - $rule " { |
| 14 | + (Invoke-ScriptAnalyzer -Path "$here\$sut" -IncludeRule $rule.RuleName ).Count | Should Be 0 |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + Describe 'Tests For Help' { |
| 20 | + # The module-qualified command fails on Microsoft.PowerShell.Archive cmdlets |
| 21 | +$Help = Get-Help $Name -ErrorAction SilentlyContinue |
| 22 | + |
| 23 | +# If help is not found, synopsis in auto-generated help is the syntax diagram |
| 24 | +It "should not be auto-generated" { |
| 25 | + $Help.Synopsis | Should Not Match '*[<CommonParameters>]*' |
| 26 | +} |
| 27 | + |
| 28 | +# Should be a description for every function |
| 29 | +It "gets description for $Name" { |
| 30 | + $Help.Description | Should Not BeNullOrEmpty |
| 31 | +} |
| 32 | + |
| 33 | +# Should be at least one example |
| 34 | +It "gets example code from $Name" { |
| 35 | + ($Help.Examples.Example | Select-Object -First 1).Code | Should Not BeNullOrEmpty |
| 36 | +} |
| 37 | + |
| 38 | +# Should be at least one example description |
| 39 | +It "gets example help from $Name" { |
| 40 | + ($Help.Examples.Example.Remarks | Select-Object -First 1).Text | Should Not BeNullOrEmpty |
| 41 | +} |
| 42 | + |
| 43 | +Context "Test parameter help for $Name" { |
| 44 | + |
| 45 | + $Common = 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', |
| 46 | + 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable' |
| 47 | + $command = Get-Command $name |
| 48 | + $parameters = $command.ParameterSets.Parameters | Sort-Object -Property Name -Unique | Where-Object { $_.Name -notin $common } |
| 49 | + $parameterNames = $parameters.Name |
| 50 | + $HelpParameterNames = $Help.Parameters.Parameter.Name | Sort-Object -Unique |
| 51 | + |
| 52 | + foreach ($parameter in $parameters) |
| 53 | + { |
| 54 | + $parameterName = $parameter.Name |
| 55 | + $parameterHelp = $Help.parameters.parameter | Where-Object Name -EQ $parameterName |
| 56 | + |
| 57 | + # Should be a description for every parameter |
| 58 | + It "gets help for parameter: $parameterName : in $Name" { |
| 59 | + $parameterHelp.Description.Text | Should Not BeNullOrEmpty |
| 60 | + } |
| 61 | + |
| 62 | + # Required value in Help should match IsMandatory property of parameter |
| 63 | + It "help for $parameterName parameter in $Name has correct Mandatory value" { |
| 64 | + $codeMandatory = $parameter.IsMandatory.toString() |
| 65 | + $parameterHelp.Required | Should Be $codeMandatory |
| 66 | + } |
| 67 | + |
| 68 | + # Parameter type in Help should match code |
| 69 | + It "help for $Name has correct parameter type for $parameterName" { |
| 70 | + $codeType = $parameter.ParameterType.Name |
| 71 | + # To avoid calling Trim method on a null object. |
| 72 | + $helpType = if ($parameterHelp.parameterValue) { $parameterHelp.parameterValue.Trim() } |
| 73 | + $helpType | Should be $codeType |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + foreach ($helpParm in $HelpParameterNames) |
| 78 | + { |
| 79 | + # Shouldn't find extra parameters in help. |
| 80 | + It "finds help parameter in code: $helpParm" { |
| 81 | + $helpParm -in $parameterNames | Should Be $true |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + Describe "$Name Tests"{ |
| 87 | + Context 'Function' { |
| 88 | + BeforeAll { |
| 89 | + $Name= Get-Command $Name |
| 90 | + } |
| 91 | + It 'Has Cmdlet Binding set to true' { |
| 92 | + $Name.CmdletBinding |should Be True |
| 93 | + } |
| 94 | + It 'Has a Server Parameter'{ |
| 95 | + ($Name.Parameters['Server']) | Should Be $true |
| 96 | + } |
| 97 | + It 'Server Parameter shoud be a string' { |
| 98 | + $Name.Parameters['Server'].ParameterType | Should Be String |
| 99 | + } |
| 100 | + It 'Server Parameter should default to $Env:COMPUTERNAME' { |
| 101 | + # $Name.Parameters['Server'] |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + Context 'Output' { |
| 106 | + It 'Should Output The Drive Letters' { |
| 107 | + |
| 108 | + } |
| 109 | + } |
| 110 | + } |
0 commit comments