Skip to content

Commit 59dd957

Browse files
committed
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...
1 parent 0d78ae5 commit 59dd957

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function ImportModuleManifest {
2+
[CmdletBinding()]
3+
param(
4+
[Alias("PSPath")]
5+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
6+
[string]$Path
7+
)
8+
process {
9+
# Get all the information in the module manifest
10+
$ModuleInfo = Get-Module $Path -ListAvailable -WarningAction SilentlyContinue -ErrorAction SilentlyContinue -ErrorVariable Problems
11+
12+
# Some versions fails silently. If the GUID is empty, we didn't get anything at all
13+
if ($ModuleInfo.Guid -eq [Guid]::Empty) {
14+
Write-Error "Cannot parse '$Path' as a module manifest, try Test-ModuleManifest for details"
15+
return
16+
}
17+
18+
# Some versions show errors are when the psm1 doesn't exist (yet), but we don't care
19+
$ErrorsWeIgnore = "^" + (@(
20+
"Modules_InvalidRequiredModulesinModuleManifest"
21+
"Modules_InvalidRootModuleInModuleManifest"
22+
) -join "|^")
23+
24+
# If there are any OTHER problems we'll fail
25+
if ($Problems = $Problems.Where({ $_.FullyQualifiedErrorId -notmatch $ErrorsWeIgnore })) {
26+
foreach ($problem in $Problems) {
27+
Write-Error $problem
28+
}
29+
# Short circuit - don't output the ModuleInfo if there were errors
30+
return
31+
}
32+
33+
# Workaround the fact that Get-Module returns the DefaultCommandPrefix as Prefix
34+
Update-Object -InputObject $ModuleInfo -UpdateObject @{ DefaultCommandPrefix = $ModuleInfo.Prefix; Prefix = "" }
35+
}
36+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#requires -Module ModuleBuilder
2+
Describe "ImportModuleManifest" {
3+
4+
Context "Mandatory Parameter" {
5+
$CommandInfo = InModuleScope ModuleBuilder { Get-Command ImportModuleManifest }
6+
7+
It 'has a mandatory Path parameter for the PSPath by pipeline' {
8+
$Path = $CommandInfo.Parameters['Path']
9+
$Path | Should -Not -BeNullOrEmpty
10+
$Path.ParameterType | Should -Be ([string])
11+
$Path.Aliases | Should -Be ("PSPath")
12+
$Path.Attributes.Where{ $_ -is [Parameter] }.Mandatory | Should -Be $true
13+
$Path.Attributes.Where{ $_ -is [Parameter] }.ValueFromPipelineByPropertyName | Should -Be $true
14+
}
15+
16+
}
17+
18+
Context "Parsing Manifests" {
19+
It "Does not cause errors for non-existent root modules" {
20+
New-ModuleManifest -Path TestDrive:\BadRoot.psd1 -Author TestName -RootModule NoSuchFile
21+
22+
InModuleScope ModuleBuilder {
23+
ImportModuleManifest -Path TestDrive:\BadRoot.psd1 -ErrorAction Stop
24+
}
25+
}
26+
27+
It "Returns the ModuleInfo with DefaultCommandPrefix instead of Prefix" {
28+
New-ModuleManifest -Path TestDrive:\TestPrefix.psd1 -Author TestName -DefaultCommandPrefix PrePre
29+
30+
$Prefix = InModuleScope ModuleBuilder {
31+
$DebugPreference = "Continue"
32+
Get-ChildItem TestDrive:\TestPrefix.psd1 | ImportModuleManifest
33+
$DebugPreference = "SilentlyContinue"
34+
}
35+
36+
$Prefix.Prefix | Should -BeNullOrEmpty
37+
$Prefix.DefaultCommandPrefix | Should -Be "PrePre"
38+
}
39+
40+
It "Does cause errors for manifests that are invalid" {
41+
New-ModuleManifest -Path TestDrive:\Invalid.psd1 -Author TestName -ModuleVersion "1.0.0"
42+
Set-Content TestDrive:\Invalid.psd1 (
43+
(Get-Content -Path TestDrive:\Invalid.psd1 -Raw) -replace "1.0.0"
44+
)
45+
46+
{
47+
InModuleScope ModuleBuilder {
48+
ImportModuleManifest -Path TestDrive:\Invalid.psd1 -ErrorAction Stop -WarningAction Stop
49+
}
50+
} | Should -Throw
51+
}
52+
}
53+
54+
Context "Invalid module manifest" {
55+
# In the current PowerShell 5.1 and 6.1
56+
# We can't make Get-Module -ListAvailable throw on a manifest
57+
# So I can't test the if($Problems = ... code
58+
}
59+
}

0 commit comments

Comments
 (0)