Skip to content

Instantly share code, notes, and snippets.

@bielawb
Created March 5, 2016 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bielawb/56617bfca7a6c8a1c1d2 to your computer and use it in GitHub Desktop.
Save bielawb/56617bfca7a6c8a1c1d2 to your computer and use it in GitHub Desktop.
<#
Assumptions:
-- uses v3 module manifest (RootModule rather than ModuleToProcess)
-- RootModule contains name of the file only
-- Certain pattern can be used to check only functions that follow naming convention (here: *-Cim*)
Also:
-- Actual tests include more actions related to manifest contents, thus "context" block is used
-- Actual test is testing multiple modules/ manifests, thus $manifest in the real scenario is part of foreach ($manifest in ls) {}
#>
$manifest = Get-Item -Path $env:ProgramFiles\WindowsPowerShell\Modules\CimInventory\*\CimInventory.psd1
Describe "Testing Module Manifest Metadata: $($manifest.BaseName)" {
It 'Has proper manifest' {
{ Test-ModuleManifest -Path $manifest.FullName } | Should not throw
}
Context "Testing properties within module manifest $($manifest.BaseName)" {
$moduleInfo = Test-ModuleManifest -Path $manifest.FullName -ErrorAction SilentlyContinue
if ($moduleInfo.RootModule) {
$fullPath = $manifest.FullName | Split-Path -Parent | Join-Path -ChildPath $moduleInfo.RootModule
$ast = [System.Management.Automation.Language.Parser]::ParseFile($fullPath, [ref]$null, [ref]$null)
$functionNames = $ast.FindAll(
{
$args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
$args[0].Name -like '*-Cim*'
},
$false
) | ForEach-Object Name
$exportedFunctions = $moduleInfo.ExportedFunctions.Keys
foreach ($name in $functionNames) {
It "$($moduleInfo.Name) exports function $name in manifest" {
# We aim for explicit list of functions, but for some modules we are using simple *-Prefix*
if ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($exportedFunctions)) {
$name -like $exportedFunctions | Should be $true
} else {
$exportedFunctions -contains $name | Should be $true
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment