ISEColorTheme.Cmdlets.Tests.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
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path

$PrivateFunctions = Get-ChildItem "$here\Private\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"}
$PublicFunctions = Get-ChildItem "$here\Public\" -Filter '*.ps1' -Recurse | Where-Object {$_.name -NotMatch "Tests.ps1"}

$PrivateFunctionsTests = Get-ChildItem "$here\Private\" -Filter '*Tests.ps1' -Recurse 
$PublicFunctionsTests = Get-ChildItem "$here\Public\" -Filter '*Tests.ps1' -Recurse 

$Rules = Get-ScriptAnalyzerRule

Import-Module "$Here\*.psd1"

if ($PrivateFunctions.count -gt 0) {
Describe "Testing all Private Functions in this Repo to be be correctly formatted" {

    foreach($PrivateFunction in $PrivateFunctions)
    {

    Context "Testing Private Function - $($PrivateFunction.BaseName) for Standard Processing" {
    
    Import-Module $PrivateFunction.FullName
    
          It "Is valid Powershell (Has no script errors)" {

                $contents = Get-Content -Path $PrivateFunction.FullName -ErrorAction Stop
                $errors = $null
                $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
                $errors.Count | Should Be 0
            }
            

    foreach ($rule in $rules) {

                    It “passes the PSScriptAnalyzer Rule $rule“ {

                        (Invoke-ScriptAnalyzer -Path $PrivateFunction.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0

                    }
                 }

            }
    
    }
 }
}

if ($PublicFunctions.count -gt 0) {
Describe "Testing all Public Functions in this Repo to be be correctly formatted" {

    foreach($PublicFunction in $PublicFunctions)
    {

    Context "Testing Public Function - $($PublicFunction.BaseName) for Standard Processing" {
    
    Import-Module $PublicFunction.FullName
          
          It "Is valid Powershell (Has no script errors)" {

                $contents = Get-Content -Path $PublicFunction.FullName -ErrorAction Stop
                $errors = $null
                $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors)
                $errors.Count | Should Be 0
            }
            

    foreach ($rule in $rules) {

                    It “passes the PSScriptAnalyzer Rule $rule“ {

                        (Invoke-ScriptAnalyzer -Path $PublicFunction.FullName -IncludeRule $rule.RuleName ).Count | Should Be 0

                        }
                    }

    }

    $function = Get-Command $PublicFunction.BaseName 
        
        Context "Testing that the function - $($function.Name) - is compliant" {
            It "Function $($function.Name) Has show-help comment block" {

                $function.Definition.Contains('<#') | should be 'True'
                $function.Definition.Contains('#>') | should be 'True'
            }

            It "Function $($function.Name) Has show-help comment block has a.SYNOPSIS" {

                $function.Definition.Contains('.SYNOPSIS') -or $function.Definition.Contains('.Synopsis') | should be 'True'

            }

            It "Function $($function.Name) Has show-help comment block has an example" {

                $function.Definition.Contains('.EXAMPLE') | should be 'True'
            }

            It "Function $($function.Name) Is an advanced function" {

                $function.CmdletBinding | should be 'True'
                $function.Definition.Contains('param') -or  $function.Definition.Contains('Param') | should be 'True'
            }
            }
        }
    Remove-Module $PublicFunction.BaseName
    }
}

if ($PublicFunctionsTests.count -gt 0) {

    foreach($PublicFunctionTest in $PublicFunctionsTests)
        { . $PublicFunctionTest.FullName }

    }

if ($PrivateFunctionsTests.count -gt 0) {

    foreach($PrivateFunctionTest in $PrivateFunctionsTests)
        { . $PrivateFunctionTest.FullName }

    }