Skip to content

Commit 9f7c114

Browse files
committed
Added tests to ensure module is configured properly
1 parent dbd2eb0 commit 9f7c114

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

Tests/Unit.GenericModule.Tests.ps1

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
$projectRoot = Resolve-Path "$PSScriptRoot\.."
2+
$moduleRoot = Split-Path (Resolve-Path "$projectRoot\*\*.psd1")
3+
$moduleName = Split-Path $moduleRoot -Leaf
4+
$modulePath = (Join-Path $moduleRoot "$moduleName.psd1")
5+
6+
Write-Host "projectRoot: $projectRoot" -f cyan
7+
Write-Host "moduleRoot: $moduleRoot" -f cyan
8+
Write-Host "moduleName: $moduleName" -f cyan
9+
Write-Host "ModulePath: $ModulePath" -f cyan
10+
11+
$ModuleManifestContent = Get-Content $modulePath
12+
13+
Describe "Generic Module Tests" -Tag UnitTest,Build {
14+
# Unload the module so it's loaded fresh for testing
15+
Remove-Module $ModuleName -ErrorAction SilentlyContinue
16+
17+
# Import Module
18+
$ModuleInformation = Import-Module $modulePath -Force -PassThru
19+
It "Module imported successfully" {
20+
$ModuleInformation.Name | Should -Be $moduleName
21+
}
22+
23+
# Evaluate AliasesToExport
24+
# Context AliasesToExport {
25+
# $AliasesToExportString = $ModuleManifestContent | Where-Object {$_ -match 'AliasesToExport'}
26+
# $DeclaredAliases = $AliasesToExportString.Split(',') |
27+
# ForEach-Object{If ($_ -match '\w+-\w+'){$Matches[0]}}
28+
29+
# It "AliasesToExport should not be a wildcard" {
30+
# $AliasesToExportString | Should -Not -Match "\'\*\'"
31+
# }
32+
33+
# $ExportedAliases = $ModuleInformation.ExportedAliases.Values.Name
34+
# ForEach ($Alias in $DeclaredAliases) {
35+
# It "Alias Should -Be Available $Alias " {
36+
# $ExportedAliases -contains $Alias | Should -Be $True
37+
# }
38+
# }
39+
# }
40+
41+
# Evaluate FunctionsToExport
42+
Context FunctionsToExport {
43+
$FunctionsToExportString = $ModuleManifestContent | Where-Object {$_ -match 'FunctionsToExport'}
44+
$DeclaredFunctions = $FunctionsToExportString.Split(',') |
45+
ForEach-Object{If ($_ -match '\w+-\w+'){$Matches[0]}}
46+
47+
It "FunctionsToExport should not be a wildcard" {
48+
$FunctionsToExportString | Should -Not -Match "\'\*\'"
49+
}
50+
51+
$PublishedFunctions = $ModuleInformation.ExportedFunctions.Values.name
52+
ForEach ($PublicFunction in $DeclaredFunctions) {
53+
It "Function Available: $PublicFunction " {
54+
$PublishedFunctions -contains $PublicFunction | Should -Be $True
55+
}
56+
}
57+
}
58+
59+
# Other Manifest Properties
60+
Context 'Other Manifest Properties' {
61+
It "RootModule property has value"{
62+
$ModuleInformation.RootModule | Should -Not -BeNullOrEmpty
63+
}
64+
It "Author property has value"{
65+
$ModuleInformation.Author | Should -Not -BeNullOrEmpty
66+
}
67+
It "Company Name property has value"{
68+
$ModuleInformation.CompanyName | Should -Not -BeNullOrEmpty
69+
}
70+
It "Description property has value"{
71+
$ModuleInformation.Description | Should -Not -BeNullOrEmpty
72+
}
73+
It "Copyright property has value"{
74+
$ModuleInformation.Copyright | Should -Not -BeNullOrEmpty
75+
}
76+
It "License property has value"{
77+
$ModuleInformation.LicenseURI | Should -Not -BeNullOrEmpty
78+
}
79+
It "Project Link property has value"{
80+
$ModuleInformation.ProjectURI | Should -Not -BeNullOrEmpty
81+
}
82+
It "Tags (For the PSGallery) property has value"{
83+
$ModuleInformation.Tags.count | Should -Not -BeNullOrEmpty
84+
}
85+
It "PSGallery Tags Should Not Contain Spaces" {
86+
ForEach ($Tag in $ModuleInformation.PrivateData.Values.Tags) {
87+
$Tag | Should -Not -Match '\s'
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)