SQLServerUpdatesModule.Test.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
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$module = 'SQLServerUpdatesModule'

Describe "$module Module Tests" -Tag 'module' {

    Context "Module Directory" {
        
        $directories = ('private', 'public', 'bin', 'tests', 'en-US')

        ForEach ($directory in $directories) {
            It "has the directory $directory" {
                Join-Path -Path $here -ChildPath $directory | Should Exist
            }
        }
    }

    Context "Module files" {
        $files = "$Module.psd1", "$Module.psm1", "$Module.Format.ps1xml", "about_$Module.help.txt", "$Module.Test.ps1", "$Module.build.ps1"
        foreach ($file in $files) {
            It "has the file $file" {
                Join-Path -Path $here -ChildPath $file | Should Exist
            }
        }
    }

    Context 'Module Setup' {
        It "has the root module $module.psm1" {
            "$here\$module.psm1" | Should Exist
        }

        It "has the a manifest file of $module.psm1" {
            "$here\$module.psd1" | Should Exist
            "$here\$module.psd1" | Should FileContentMatch "$module.psm1"
        }

        It "public folder has some functions" {
            $functions = Get-ChildItem (Join-Path -Path $here -ChildPath 'public') 
            $functions.Count | Should Not Be 0
        }

        It "$module is valid PowerShell code" {
            $psFile = Get-Content -Path "$here\$module.psm1" `
                -ErrorAction Stop
            $errors = $null
            $null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors)
            $errors.Count | Should Be 0
        }
    }
}
Describe "Function in module" -Tag 'function' {

    $functions = Get-ChildItem (Join-Path -Path $here -ChildPath 'public') 

    foreach ($function in $functions) {
        $functionName = $function.Name

        Context "Test Function $functionName" {
      
            It "$functionName.ps1 should exist" {
                $function.FullName | Should Exist
            }
    
            It "$functionName.ps1 should have help block" {
                $function.FullName | Should FileContentMatch '<#'
                $function.FullName | Should FileContentMatch '#>'
            }

            It "$functionName.ps1 should have a SYNOPSIS section in the help block" {
                $function.FullName | Should FileContentMatch '.SYNOPSIS'
            }
    
            It "$functionName.ps1 should have a DESCRIPTION section in the help block" {
                $function.FullName | Should FileContentMatch '.DESCRIPTION'
            }

            It "$functionName.ps1 should have a EXAMPLE section in the help block" {
                $function.FullName | Should FileContentMatch '.EXAMPLE'
            }
    
            It "$functionName.ps1 should be an advanced function" {
                $function.FullName | Should FileContentMatch 'function'
                $function.FullName | Should FileContentMatch 'cmdletbinding'
                $function.FullName | Should FileContentMatch 'param'
            }
      
            It "$functionName.ps1 should contain Write-Verbose blocks" {
                $function.FullName | Should FileContentMatch 'Write-Verbose'
            }
    
            It "$functionName.ps1 is valid PowerShell code" {
                $psFile = Get-Content -Path $function.FullName  `
                    -ErrorAction Stop
                $errors = $null
                $null = [System.Management.Automation.PSParser]::Tokenize($psFile, [ref]$errors)
                $errors.Count | Should Be 0
            }

    
        } # Context "Test Function $function"

    } # foreach ($function in $functions)

}