Skip to content

Commit b451007

Browse files
authored
Fix tasks that fail for modules without exported commands (#148)
1 parent 049c1e0 commit b451007

4 files changed

+81
-4
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Fixed
9+
10+
- `Generate_Markdown_For_Public_Commands.build`
11+
- Now the task will not try to generate markdown if the module does not
12+
have any publicly exported commands ([issue #135](https://github.com/dsccommunity/DscResource.DocGenerator/issues/147)).
13+
- Now has error handling if the script that is called using the call
14+
operator `&` fails.
15+
`Generate_External_Help_File_For_Public_Commands`
16+
- Now the task will not fail if there are no extern help file generated,
17+
which is the case for modules that does not have any publicly exported
18+
commands ([issue #135](https://github.com/dsccommunity/DscResource.DocGenerator/issues/147)).
19+
- Now has error handling if the script that is called using the call
20+
operator `&` fails.
21+
822
## [0.12.2] - 2024-05-31
923

1024
### Added

source/tasks/Generate_External_Help_File_For_Public_Commands.build.ps1

+18-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ New-ExternalHelp -Path '$DocOutputFolder' -OutputPath '$builtModuleLocalePath' -
116116
#>
117117
& $pwshPath -Command $generateMarkdownScriptBlock -ExecutionPolicy 'ByPass' -NoProfile
118118

119-
# Add a newline to the end of the help file to pass HQRM tests.
120-
Add-NewLine -FileInfo (Get-Item -Path "$builtModuleLocalePath/$ProjectName-help.xml") -AtEndOfFile
119+
if (-not $?)
120+
{
121+
throw "Failed to generate external help file for the module '$ProjectName'."
122+
}
123+
124+
$externalHelpFile = Get-Item -Path (Join-Path -Path $builtModuleLocalePath -ChildPath "$ProjectName-help.xml") -ErrorAction 'Ignore'
125+
126+
if ($externalHelpFile)
127+
{
128+
# Add a newline to the end of the help file to pass HQRM tests.
129+
Add-NewLine -FileInfo $externalHelpFile -AtEndOfFile
130+
131+
Write-Build -Color 'Green' -Text "External help file generated for the module '$ProjectName'."
132+
}
133+
else
134+
{
135+
Write-Warning -Message "External help file not found at '$builtModuleLocalePath/$ProjectName-help.xml'. This is normal if there were no exported commands in the module."
136+
}
121137
}

source/tasks/Generate_Markdown_For_Public_Commands.build.ps1

+22-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,19 @@ Import-Module -name '$DependentModule'
166166
}
167167

168168
$generateMarkdownScript += @"
169-
`n# Import the module to generate help for
170-
Import-Module -Name '$ProjectName' -ErrorAction Stop
169+
`n# Import the module that help is generate for
170+
`$importModule = Import-Module -Name '$ProjectName' -Passthru -ErrorAction 'Stop'
171+
172+
if (-not `$importModule)
173+
{
174+
throw 'Failed to import the module ''$ProjectName''.'
175+
}
176+
elseif (`$importModule.ExportedCommands.Count -eq 0)
177+
{
178+
Write-Warning -Message 'No public commands found in the module ''$ProjectName''. Skipping'
179+
180+
return
181+
}
171182
172183
`$newMarkdownHelpParams = @{
173184
Module = '$ProjectName'
@@ -200,4 +211,13 @@ New-MarkdownHelp @newMarkdownHelpParams
200211
other modules that are loaded in the current process.
201212
#>
202213
& $pwshPath -Command $generateMarkdownScriptBlock -ExecutionPolicy 'ByPass' -NoProfile
214+
215+
if (-not $?)
216+
{
217+
throw "Failed to generate markdown documentation for the public commands for module '$ProjectName'."
218+
}
219+
else
220+
{
221+
Write-Build -Color 'Green' -Text 'Markdown for command documentation created for module '$ProjectName'.'
222+
}
203223
}

tests/unit/tasks/Generate_External_Help_File_For_Public_Commands.Tests.ps1

+27
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,31 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
138138

139139
Should -Exist -ActualValue (Join-Path -Path $TestDrive.FullName -ChildPath 'en-US/MockModule-help.xml') -Because 'the task should have generated the external help file from the markdown.'
140140
}
141+
142+
Context 'When there is no external help file created' {
143+
It 'Should run the build task without throwing' {
144+
Mock -CommandName Write-Warning
145+
Mock -CommandName Get-Item -ParameterFilter {
146+
$Path -eq (Join-Path -Path $TestDrive.FullName -ChildPath 'en-US/MockModule-help.xml')
147+
} -MockWith {
148+
return $null
149+
}
150+
151+
{
152+
$taskParameters = @{
153+
ProjectName = 'MockModule'
154+
ProjectPath = $TestDrive.FullName
155+
OutputDirectory = $TestDrive.FullName
156+
# Using the markdown created when the project was built.
157+
DocOutputFolder = $TestDrive.FullName | Join-Path -ChildPath 'WikiContent'
158+
SourcePath = "$($TestDrive.FullName)/source"
159+
HelpCultureInfo = 'en-US'
160+
}
161+
162+
Invoke-Build -Task $buildTaskName -File $script:buildScript.Definition @taskParameters
163+
} | Should -Not -Throw
164+
165+
Assert-MockCalled -CommandName Write-Warning -Exactly -Times 1 -Scope It
166+
}
167+
}
141168
}

0 commit comments

Comments
 (0)