Tests/AzureHelpers.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
$ModulePath = Split-Path -Path $PSScriptRoot -Parent
$ModuleName = Split-Path -Path $ModulePath -Leaf

# Make sure one or multiple versions of the module are not loaded
Get-Module -Name $ModuleName | Remove-Module

# Import the module and store the information about the module
$ModuleLoadPath = Join-Path $ModulePath "$ModuleName.psd1"
$ModuleInformation = Import-Module -Name $ModuleLoadPath -PassThru
$ModuleInformation | Format-List

# Get the functions present in the Manifest
$ExportedFunctions = $ModuleInformation.ExportedFunctions.Values.Name

# Get the functions present in the Public folder
$PS1Functions = Get-ChildItem -Path $(Join-Path $ModulePath 'Public' '*.ps1')

 # Get the RequiredModules
 $requiredModules = $ModuleInformation.RequiredModules.ModuleName
 
 if ($requiredModules) {
     # Install the required modules
     foreach ($module in $requiredModules) {
         Install-Module $module -Force
     }
 }

Describe "$ModuleName Module - Testing Manifest File (.psd1)" {
    Context "Manifest" {
        It "Should contain RootModule" {
            $ModuleInformation.RootModule | Should -Not -BeNullOrEmpty
        }

        It "Should contain ModuleVersion" {
            $ModuleInformation.Version | Should -Not -BeNullOrEmpty
        }

        It "Should contain GUID" {
            $ModuleInformation.Guid | Should -Not -BeNullOrEmpty
        }

        It "Should contain Author" {
            $ModuleInformation.Author | Should -Not -BeNullOrEmpty
        }

        It "Should contain Description" {
            $ModuleInformation.Description | Should -Not -BeNullOrEmpty
        }

        It "Compare the count of Function Exported and the PS1 files found" {
            $status = $ExportedFunctions.Count -eq $PS1Functions.Count
            $status | Should -Be $true
        }

        It "Compare the missing function" {
            If ($ExportedFunctions.count -ne $PS1Functions.count) {
                $Compare = Compare-Object -ReferenceObject $ExportedFunctions -DifferenceObject $PS1Functions.Basename
                $Compare.InputObject -Join ',' | Should -BeNullOrEmpty
            }
        }
    }
}

Get-Module -Name $ModuleName | Remove-Module