forked from ModuleBuild/ModuleBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleManifest.Tests.ps1
More file actions
205 lines (181 loc) · 9.03 KB
/
Copy pathModuleManifest.Tests.ps1
File metadata and controls
205 lines (181 loc) · 9.03 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#Requires -Modules Pester
<#
This pester test verifies the PowerShell module manifest file has valid content that will be required to
upload to the PowerShell Gallary.
.EXAMPLE
PS> Invoke-Pester -Script @{Path = '.\src\tests\ModuleManifest.Tests.ps1'; Parameters = @{ ManifestPath = 'C:\Users\zloeber\Dropbox\Zach_Docs\Projects\Git\PSAD'; Author = 'Zachary Loeber'; Website = 'https://github.com/zloeber/PSAD'; Tags = @('ADSI', 'Active_Directory', 'DC') }}
Runs some standard tests and a few manual validations (Tags, Author, and Website).
.EXAMPLE
PS> Invoke-Pester -Script @{Path = '.\src\tests\ModuleManifest.Tests.ps1'; Parameters = @{ ManifestPath = '.\PSAD.psd1'}}
Runs several generic tests against the PSAD.psd1 manifest to ensure all required values for uploading to the PowerShell Gallery simply exist.
#>
[CmdLetBinding(DefaultParameterSetName='Default')]
Param(
[Parameter(Mandatory = $True, ParameterSetName='Default')]
[Parameter(Mandatory = $True, ParameterSetName='Manual')]
[Parameter(Mandatory = $True, ParameterSetName='ModuleBuild')]
[string]$ManifestPath,
[Parameter(Mandatory = $True, ParameterSetName='ModuleBuild')]
[string]$ModuleBuildJSONPath,
[Parameter(ParameterSetName='Manual')]
[string]$Author,
[Parameter(ParameterSetName='Manual')]
[string]$Copyright,
[Parameter(ParameterSetName='Manual')]
[string]$Website,
[Parameter(ParameterSetName='Manual')]
[string]$LicenseURI,
[Parameter(ParameterSetName='Manual')]
[string]$Version,
[Parameter(ParameterSetName='Manual')]
[string[]]$Tags
)
if (($ManifestPath.EndsWith('psd1')) -and (Test-Path $ManifestPath)) {
# Grab the short module name
$ModuleName = (Split-Path $ManifestPath -Leaf).Split('.')[0]
Describe 'Module Manifest - Standard Tests' {
Context "Testing $ManifestPath" {
It 'should be a valid module manifest file' {
{
$Script:Manifest = Test-ModuleManifest -Path $ManifestPath -ErrorAction Stop -WarningAction SilentlyContinue
} | Should Not Throw
}
It 'should have a valid RootModule value' {
$Script:Manifest.RootModule | Should Be "$ModuleName.psm1"
}
It 'should have a valid GUID' {
($Script:Manifest.Guid).Guid | Should BeLike '????????-????-????-????-????????????'
}
It 'should have a valid PowerShellVersion value' {
$Script:Manifest.PowerShellVersion | Should Not BeNullOrEmpty
}
}
}
switch ($PSCmdlet.ParameterSetName) {
'Default' {
# Passed just a module manifest file name with no other information to validate
# This will check for several entries that they simply exist.
Describe 'Module Manifest - Generic Tests' {
Context "Testing $ManifestPath" {
It 'should have a valid Module version' {
($Script:Manifest.Version).ToString() -as [Version] | Should Not BeNullOrEmpty
}
It 'should have a valid module description' {
$Script:Manifest.Description | Should Not BeNullOrEmpty
}
It 'should have a valid module author' {
$Script:Manifest.Author | Should Not BeNullOrEmpty
}
It 'should have a valid module project website' {
$Script:Manifest.ProjectUri.OriginalString | Should Not BeNullOrEmpty
}
It 'should have a valid license URL' {
$Script:Manifest.LicenseUri | Should Not BeNullOrEmpty
}
It 'should have some tags' {
@($Script:Manifest.PrivateData.PSData.Tags).Count -gt 0 | Should Be $true
}
}
}
}
'Manual' {
# Passed a manifest name and several manual entries to validate
Describe 'Module Manifest - Manual Tests' {
Context "Testing $ManifestPath" {
if (-not [string]::IsNullOrEmpty($Version)) {
It "should be module version '$Version'" {
($Script:Manifest.Version).ToString() -as [Version] | Should Be $Version
}
}
if (-not [string]::IsNullOrEmpty($Description)) {
It "should have a module description of '$Description'" {
$Script:Manifest.Description | Should Be $Description
}
}
if (-not [string]::IsNullOrEmpty($Author)) {
It "should have the module author of '$Author'" {
$Script:Manifest.Author | Should Be $Author
}
}
if (-not [string]::IsNullOrEmpty($Copyright)) {
It "should have a Copyright of '$Copyright'" {
$Script:Manifest.Copyright | Should Be $Copyright
}
}
if (-not [string]::IsNullOrEmpty($Website)) {
It "should have the project website of '$Website'" {
$Script:Manifest.ProjectUri.OriginalString | Should Be $Website
}
}
if (-not [string]::IsNullOrEmpty($LicenseURI)) {
It "should have the license URI of '$LicenseURI'" {
$Script:Manifest.LicenseUri | Should Be $LicenseURI
}
}
if ($Tags.Count -gt 0) {
It "should have these tags: $($Tags -join ',')" {
Compare-Object $Script:Manifest.PrivateData.PSData.Tags $Tags | Should Be $Null
}
}
}
}
}
'ModuleBuild' {
# Passed a manifest name and a ModuleBuild json configuration file to validate
if (($ModuleBuildJSONPath.EndsWith('psd1')) -and (Test-Path $ModuleBuildJSONPath)) {
try {
$ModuleInfo = get-content $ModuleBuildJSONPath | ConvertFrom-Json
}
catch {
throw "$ModuleBuildJSONPath either does not exist or is not a json file!"
}
}
else {
throw "$ModuleBuildJSONPath either does not exist or is not a json file!"
}
# Passed a manifest name and several manual entries to validate
Describe 'Module Manifest - ModuleBuild Tests' {
Context "Testing $ManifestPath" {
if (-not [string]::IsNullOrEmpty($Version)) {
It "should be module version '$Version'" {
($Script:Manifest.Version).ToString() -as [Version] | Should Be $ModuleInfo.ModuleVersion
}
}
if (-not [string]::IsNullOrEmpty($Description)) {
It "should have a module description of '$Description'" {
$Script:Manifest.Description | Should Be $ModuleInfo.ModuleDescription
}
}
if (-not [string]::IsNullOrEmpty($Author)) {
It "should have the module author of '$Author'" {
$Script:Manifest.Author | Should Be $ModuleInfo.ModuleAuthor
}
}
if (-not [string]::IsNullOrEmpty($Copyright)) {
It "should have a Copyright of '$Copyright'" {
$Script:Manifest.Copyright | Should Be $ModuleInfo.ModuleCopyright
}
}
if (-not [string]::IsNullOrEmpty($Website)) {
It "should have the project website of '$Website'" {
$Script:Manifest.ProjectUri.OriginalString | Should Be $ModuleInfo.ModuleWebsite
}
}
if (-not [string]::IsNullOrEmpty($LicenseURI)) {
It "should have the license URI of '$LicenseURI'" {
$Script:Manifest.LicenseUri | Should Be $ModuleInfo.ModuleLicenseURI
}
}
if ($Tags.Count -gt 0) {
It "should have these tags: $($ModuleInfo.ModuleTags -join ',')" {
Compare-Object $Script:Manifest.PrivateData.PSData.Tags $ModuleInfo.ModuleTags | Should Be $Null
}
}
}
}
}
}
}
else {
Write-Error "$ManifestPath was not found!"
}