1
- # Tested with Pester module version 4.4.0
1
+ # Requires -Modules Pester
2
2
3
- # Name of this module found by parsing name of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
3
+ # Obtain name of this module by parsing name of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
4
4
$ThisModule = $PSCommandPath -replace ' \.Tests\.ps1$'
5
5
$ThisModuleName = $ThisModule | Split-Path - Leaf
6
6
7
- # Path of the module based on location of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
7
+ # Obtain path of the module based on location of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
8
8
$ThisModulePath = Split-Path (Split-Path - Parent $PSCommandPath ) - Parent
9
9
10
10
# Make sure one or multiple versions of the module are not loaded
11
11
Get-Module - Name $ThisModuleName | Remove-Module
12
12
13
+ # Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed
14
+ # Manifest file path
15
+ $ManifestFile = " $ThisModulePath \$ThisModuleName .psd1"
16
+
17
+ # Import the module and store the information about the module
18
+ $ModuleInformation = Import-module - Name $ManifestFile - PassThru
19
+
13
20
# Internal Files
14
21
$InternalDirectoryFiles = (
15
22
' APIKey.ps1' ,
@@ -46,17 +53,42 @@ $ResourceDirectoryFiles = (
46
53
' Users.ps1'
47
54
)
48
55
49
- Describe ' $ThisModuleName Module Tests' {
56
+ # Manifest Elements
57
+ $ManifestFileElements = (
58
+ ' RootModule' ,
59
+ ' Author' ,
60
+ ' CompanyName' ,
61
+ ' Description' ,
62
+ ' Copyright' ,
63
+ ' PowerShellVersion' ,
64
+ ' NestedModules' ,
65
+ ' HelpInfoURI'
66
+ )
67
+
68
+
69
+ Test-ModuleManifest - Path .\ITGlueAPI.psd1
70
+ write-host $ModuleInformation.ModuleVersion - ForegroundColor Cyan
71
+
72
+ Describe " $ThisModuleName Module Tests" {
50
73
51
74
Context ' Test Module' {
52
75
It " has the root module $ThisModuleName .psm1" {
53
76
" $ThisModulePath \$ThisModuleName .psm1" | Should Exist
54
77
}
55
78
56
- It " has a manifest file of $ThisModuleName .psd1" {
57
- " $ThisModulePath \$ThisModuleName .psd1" | Should Exist
58
- # TODO - This test is currently failing...
59
- # "$ThisModulePath\$ThisModuleName.psd1" | Should Contain "ITGlueAPI.psm1"
79
+ Context " Test Manifest File (.psd1)" {
80
+ It " Should pass Test-ModuleManifest" {
81
+ $errors = $null
82
+ $errors = Test-ModuleManifest - Path $ThisModulePath \$ThisModuleName.psd1 - ErrorAction Stop
83
+ $errors.Count | Should Be 1
84
+ }
85
+
86
+ # Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed
87
+ ForEach ($ManifestFileElement in $ManifestFileElements ) {
88
+ It " Should contains $ManifestFileElement " {
89
+ $ModuleInformation .$ManifestFileElement | Should not BeNullOrEmpty
90
+ }
91
+ }
60
92
}
61
93
62
94
It " $ThisModuleName \Resources directory has functions" {
@@ -78,7 +110,19 @@ Describe '$ThisModuleName Module Tests' {
78
110
It " $InternalFile should exist" {
79
111
" $ThisModulePath \Internal\$InternalFile " | Should Exist
80
112
}
113
+ It " $InternalFile is valid PowerShell code" {
114
+ $psFile = Get-Content - Path " $ThisModulePath \Internal\$InternalFile " - ErrorAction Stop
115
+ $errors = $null
116
+ $null = [System.Management.Automation.PSParser ]::Tokenize($psfile , [ref ]$errors )
117
+ $errors.Count | Should Be 0
118
+ }
81
119
}
120
+ # Context "$InternalFile has tests" {
121
+ $InternalFileTest = $InternalFile -replace ' \.ps1$'
122
+ It " $InternalFileTest .Tests.ps1 should exist" {
123
+ $InternalFileTest.Tests.ps1 | Should Exist
124
+ }
125
+ # }
82
126
}
83
127
84
128
# Check that Resource files exist
@@ -87,6 +131,21 @@ Describe '$ThisModuleName Module Tests' {
87
131
It " $ResourceFile should exist" {
88
132
" $ThisModulePath \Resources\$ResourceFile " | Should Exist
89
133
}
134
+ It " $ResourceFile is valid PowerShell code" {
135
+ $psFile = Get-Content - Path " $ThisModulePath \Resources\$ResourceFile " - ErrorAction Stop
136
+ $errors = $null
137
+ $null = [System.Management.Automation.PSParser ]::Tokenize($psfile , [ref ]$errors )
138
+ $errors.Count | Should Be 0
139
+ }
140
+ }
141
+ # TODO - add tests to check for tests files
142
+ }
143
+
144
+ Context " PowerShell $ThisModuleName Import Test" {
145
+ # Credit - borrowed with care from https://github.com/TheMattCollins0/MattTools/blob/master/Tests/ModuleImport.Tests.ps1 and modified as needed
146
+ It " Should import PowerShell $ThisModuleName succesfully" {
147
+ Import-Module - Name $ThisModulePath - ErrorVariable ImportError
148
+ $ImportError | Should Be $null
90
149
}
91
150
}
92
151
}
0 commit comments