Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
Created August 22, 2016 02:28
Show Gist options
  • Save mattmcnabb/d7313e966119664bbf489f2693486cf3 to your computer and use it in GitHub Desktop.
Save mattmcnabb/d7313e966119664bbf489f2693486cf3 to your computer and use it in GitHub Desktop.
Blog_pester-testing-your-module-manifest
$ModulePath = Split-Path -Parent $MyInvocation.MyCommand.Path
$ModuleName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1"
$ManifestPath = "$ModulePath\$ModuleName.psd1"
# test the module manifest - exports the right functions, processes the right formats, and is generally correct
Describe "Manifest" {
$Manifest = $null
It "has a valid manifest" {
{
$Script:Manifest = Test-ModuleManifest -Path $ManifestPath -ErrorAction Stop -WarningAction SilentlyContinue
} | Should Not Throw
}
It "has a valid name" {
$Script:Manifest.Name | Should Be $ModuleName
}
It "has a valid root module" {
$Script:Manifest.RootModule | Should Be "$ModuleName.psm1"
}
It "has a valid Description" {
$Script:Manifest.Description | Should Not BeNullOrEmpty
}
It "has a valid guid" {
$Script:Manifest.Guid | Should Be '9dd7e9a4-8525-4fd1-aa13-3a063df4b264'
}
It "has a valid prefix" {
$Script:Manifest.Prefix | Should Not BeNullOrEmpty
}
It "has a valid copyright" {
$Script:Manifest.CopyRight | Should Not BeNullOrEmpty
}
It 'exports all public functions' {
$FunctionFiles = Get-ChildItem "$ModulePath\Scripts\Public" -Filter *.ps1 | Select -ExpandProperty BaseName
$FunctionNames = $FunctionFiles | foreach {$_ -replace '-', "-$($Script:Manifest.Prefix)"}
$ExFunctions = $Script:Manifest.ExportedFunctions.Values.Name
foreach ($FunctionName in $FunctionNames)
{
$ExFunctions -contains $FunctionName | Should Be $true
}
}
}
$ModulePath = Split-Path -Parent $MyInvocation.MyCommand.Path
$ModuleName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -Replace ".Tests.ps1"
$ManifestPath = "$ModulePath\$ModuleName.psd1"
# test the module manifest - exports the right functions, processes the right formats, and is generally correct
Describe "Manifest" {
$ManifestHash = Invoke-Expression (Get-Content $ManifestPath -Raw)
It "has a valid manifest" {
{
$null = Test-ModuleManifest -Path $ManifestPath -ErrorAction Stop -WarningAction SilentlyContinue
} | Should Not Throw
}
It "has a valid root module" {
$ManifestHash.RootModule | Should Be "$ModuleName.psm1"
}
It "has a valid Description" {
$ManifestHash.Description | Should Not BeNullOrEmpty
}
It "has a valid guid" {
$ManifestHash.Guid | Should Be '9dd7e9a4-8525-4fd1-aa13-3a063df4b264'
}
It "has a valid prefix" {
$ManifestHash.DefaultCommandPrefix | Should Not BeNullOrEmpty
}
It "has a valid copyright" {
$ManifestHash.CopyRight | Should Not BeNullOrEmpty
}
It "exports all public functions" {
$ExFunctions = $ManifestHash.FunctionsToExport
$FunctionFiles = Get-ChildItem "$ModulePath\Scripts\Public" -Filter *.ps1 | Select-Object -ExpandProperty BaseName
$FunctionNames = $FunctionFiles
foreach ($FunctionName in $FunctionNames)
{
$ExFunctions -contains $FunctionName | Should Be $true
}
}
}
$ManifestHash = Invoke-Expression (Get-Content $ManifestPath -Raw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment