Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ImportModuleManifest to wrap Get-Module
I thought Get-Module would error if it could not parse the manifest. Instead it returns an empty ModuleInfo object.

This wrapper will correct that while preserving the old error handling...
  • Loading branch information
Jaykul committed Jan 14, 2020
commit 59dd957da0153c42b9bc0c5626b05481c08c9edb
36 changes: 36 additions & 0 deletions Source/Private/ImportModuleManifest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function ImportModuleManifest {
[CmdletBinding()]
param(
[Alias("PSPath")]
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$Path
)
process {
# Get all the information in the module manifest
$ModuleInfo = Get-Module $Path -ListAvailable -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable Problems

# Some versions fails silently. If the GUID is empty, we didn't get anything at all
if ($ModuleInfo.Guid -eq [Guid]::Empty) {
Write-Error "Cannot parse '$Path' as a module manifest, try Test-ModuleManifest for details"
return
}

# Some versions show errors are when the psm1 doesn't exist (yet), but we don't care
$ErrorsWeIgnore = "^" + (@(
"Modules_InvalidRequiredModulesinModuleManifest"
"Modules_InvalidRootModuleInModuleManifest"
) -join "|^")

# If there are any OTHER problems we'll fail
if ($Problems = $Problems.Where({ $_.FullyQualifiedErrorId -notmatch $ErrorsWeIgnore })) {
foreach ($problem in $Problems) {
Write-Error $problem
}
# Short circuit - don't output the ModuleInfo if there were errors
return
}

# Workaround the fact that Get-Module returns the DefaultCommandPrefix as Prefix
Update-Object -InputObject $ModuleInfo -UpdateObject @{ DefaultCommandPrefix = $ModuleInfo.Prefix; Prefix = "" }
}
}
59 changes: 59 additions & 0 deletions Tests/Private/ImportModuleManifest.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#requires -Module ModuleBuilder
Describe "ImportModuleManifest" {

Context "Mandatory Parameter" {
$CommandInfo = InModuleScope ModuleBuilder { Get-Command ImportModuleManifest }

It 'has a mandatory Path parameter for the PSPath by pipeline' {
$Path = $CommandInfo.Parameters['Path']
$Path | Should -Not -BeNullOrEmpty
$Path.ParameterType | Should -Be ([string])
$Path.Aliases | Should -Be ("PSPath")
$Path.Attributes.Where{ $_ -is [Parameter] }.Mandatory | Should -Be $true
$Path.Attributes.Where{ $_ -is [Parameter] }.ValueFromPipelineByPropertyName | Should -Be $true
}

}

Context "Parsing Manifests" {
It "Does not cause errors for non-existent root modules" {
New-ModuleManifest -Path TestDrive:\BadRoot.psd1 -Author TestName -RootModule NoSuchFile

InModuleScope ModuleBuilder {
ImportModuleManifest -Path TestDrive:\BadRoot.psd1 -ErrorAction Stop
}
}

It "Returns the ModuleInfo with DefaultCommandPrefix instead of Prefix" {
New-ModuleManifest -Path TestDrive:\TestPrefix.psd1 -Author TestName -DefaultCommandPrefix PrePre

$Prefix = InModuleScope ModuleBuilder {
$DebugPreference = "Continue"
Get-ChildItem TestDrive:\TestPrefix.psd1 | ImportModuleManifest
$DebugPreference = "SilentlyContinue"
}

$Prefix.Prefix | Should -BeNullOrEmpty
$Prefix.DefaultCommandPrefix | Should -Be "PrePre"
}

It "Does cause errors for manifests that are invalid" {
New-ModuleManifest -Path TestDrive:\Invalid.psd1 -Author TestName -ModuleVersion "1.0.0"
Set-Content TestDrive:\Invalid.psd1 (
(Get-Content -Path TestDrive:\Invalid.psd1 -Raw) -replace "1.0.0"
)

{
InModuleScope ModuleBuilder {
ImportModuleManifest -Path TestDrive:\Invalid.psd1 -ErrorAction Stop -WarningAction Stop
}
} | Should -Throw
}
}

Context "Invalid module manifest" {
# In the current PowerShell 5.1 and 6.1
# We can't make Get-Module -ListAvailable throw on a manifest
# So I can't test the if($Problems = ... code
}
}