Skip to content

Commit 665cf90

Browse files
nateschereradityapatwardhan
authored andcommitted
Fix errors for New-MarkdownHelp ps1 input (#446) (#450)
1 parent d8e1970 commit 665cf90

File tree

2 files changed

+102
-31
lines changed

2 files changed

+102
-31
lines changed

src/platyPS/platyPS.psm1

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ function New-MarkdownHelp
192192
}
193193

194194
$commandName = $mamlObject.Name
195+
195196
# create markdown
196197
if ($NoMetadata)
197198
{
@@ -206,10 +207,20 @@ function New-MarkdownHelp
206207
}
207208
else
208209
{
209-
$a = @{
210-
Name = $commandName
210+
# Get-Command requires that script input be a path
211+
if ($mamlObject.Name.EndsWith(".ps1"))
212+
{
213+
$getCommandName = Resolve-Path $Command
214+
}
215+
# For cmdlets, nothing needs to be done
216+
else
217+
{
218+
$getCommandName = $commandName
211219
}
212220

221+
$a = @{
222+
Name = $getCommandName
223+
}
213224
if ($module) {
214225
# for module case, scope it just to this module
215226
$a['Module'] = $module
@@ -1810,39 +1821,42 @@ function GetHelpFileName
18101821
return $CommandInfo.HelpFile
18111822
}
18121823
}
1813-
1814-
# overwise, lets guess it
1815-
$module = @($CommandInfo.Module) + ($CommandInfo.Module.NestedModules) |
1816-
Where-Object {$_.ModuleType -ne 'Manifest'} |
1817-
Where-Object {$_.ExportedCommands.Keys -contains $CommandInfo.Name}
1818-
1819-
if (-not $module)
1824+
# only run module evaluations if the input command isn't a script
1825+
if ($CommandInfo.CommandType -ne "ExternalScript")
18201826
{
1821-
Write-Warning -Message ($LocalizedData.ModuleNotFoundFromCommand -f '[GetHelpFileName]', $CommandInfo.Name)
1822-
return
1823-
}
1827+
# overwise, lets guess it
1828+
$module = @($CommandInfo.Module) + ($CommandInfo.Module.NestedModules) |
1829+
Where-Object {$_.ModuleType -ne 'Manifest'} |
1830+
Where-Object {$_.ExportedCommands.Keys -contains $CommandInfo.Name}
18241831

1825-
if ($module.Count -gt 1)
1826-
{
1827-
Write-Warning -Message ($LocalizedData.MultipleModulesFoundFromCommand -f '[GetHelpFileName]', $CommandInfo.Name)
1828-
$module = $module | Select-Object -First 1
1829-
}
1832+
if (-not $module)
1833+
{
1834+
Write-Warning -Message ($LocalizedData.ModuleNotFoundFromCommand -f '[GetHelpFileName]', $CommandInfo.Name)
1835+
return
1836+
}
18301837

1831-
if (Test-Path $module.Path -Type Leaf)
1832-
{
1833-
# for regular modules, we can deduct the filename from the module path file
1834-
$moduleItem = Get-Item -Path $module.Path
1835-
if ($moduleItem.Extension -eq '.psm1') {
1836-
$fileName = $moduleItem.BaseName
1837-
} else {
1838-
$fileName = $moduleItem.Name
1838+
if ($module.Count -gt 1)
1839+
{
1840+
Write-Warning -Message ($LocalizedData.MultipleModulesFoundFromCommand -f '[GetHelpFileName]', $CommandInfo.Name)
1841+
$module = $module | Select-Object -First 1
1842+
}
1843+
1844+
if (Test-Path $module.Path -Type Leaf)
1845+
{
1846+
# for regular modules, we can deduct the filename from the module path file
1847+
$moduleItem = Get-Item -Path $module.Path
1848+
if ($moduleItem.Extension -eq '.psm1') {
1849+
$fileName = $moduleItem.BaseName
1850+
} else {
1851+
$fileName = $moduleItem.Name
1852+
}
1853+
}
1854+
else
1855+
{
1856+
# if it's something like Dynamic module,
1857+
# we guess the desired help file name based on the module name
1858+
$fileName = $module.Name
18391859
}
1840-
}
1841-
else
1842-
{
1843-
# if it's something like Dynamic module,
1844-
# we guess the desired help file name based on the module name
1845-
$fileName = $module.Name
18461860
}
18471861

18481862
return "$fileName-help.xml"

test/Pester/PlatyPs.Tests.ps1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,63 @@ Describe 'New-MarkdownHelp' {
146146
}
147147
}
148148

149+
Context 'from external script' {
150+
It 'fully qualified path' {
151+
$SeedData = @"
152+
<#
153+
.SYNOPSIS
154+
Synopsis Here.
155+
156+
.DESCRIPTION
157+
Description Here.
158+
159+
.INPUTS
160+
None
161+
162+
.OUTPUTS
163+
None
164+
165+
.EXAMPLE
166+
.\Invoke-HelloWorld.ps1
167+
168+
#>
169+
170+
Write-Host 'Hello World!'
171+
"@
172+
Set-Content -Value $SeedData -Path TestDrive:\Invoke-HelloWorld.ps1 -NoNewline
173+
$files = New-MarkdownHelp -Command "TestDrive:\Invoke-HelloWorld.ps1" -OutputFolder TestDrive:\output -Force
174+
($files | Measure-Object).Count | Should Be 1
175+
}
176+
It 'relative path' {
177+
$SeedData = @"
178+
<#
179+
.SYNOPSIS
180+
Synopsis Here.
181+
182+
.DESCRIPTION
183+
Description Here.
184+
185+
.INPUTS
186+
None
187+
188+
.OUTPUTS
189+
None
190+
191+
.EXAMPLE
192+
.\Invoke-HelloWorld.ps1
193+
194+
#>
195+
196+
Write-Host 'Hello World!'
197+
"@
198+
Set-Content -Value $SeedData -Path TestDrive:\Invoke-HelloWorld.ps1 -NoNewline
199+
$Location = Get-Location
200+
Set-Location TestDrive:\
201+
$files = New-MarkdownHelp -Command "TestDrive:\Invoke-HelloWorld.ps1" -OutputFolder TestDrive:\output -Force
202+
Set-Location $Location
203+
($files | Measure-Object).Count | Should Be 1
204+
}
205+
}
149206
Context 'AlphabeticParamsOrder' {
150207
function global:Get-Alpha
151208
{

0 commit comments

Comments
 (0)