Skip to content

Commit

Permalink
Improve handling on empty title #122
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jan 18, 2021
1 parent c86b7f3 commit 68f31a7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ What's changed since v0.7.0:
- Engine features:
- Added support for custom configuration key values. [#121](https://github.com/BernieWhite/PSDocs/issues/121)
- See `about_PSDocs_Configuration` for more details.
- General improvements:
- Improve handling when an empty document title is set. [#122](https://github.com/BernieWhite/PSDocs/issues/122)
- Bug fixes:
- Fixed use of error action preference with `Include` keyword. [#127](https://github.com/BernieWhite/PSDocs/issues/127)

Expand Down
7 changes: 7 additions & 0 deletions src/PSDocs/Commands/TitleCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using PSDocs.Pipeline;
using System.Management.Automation;

namespace PSDocs.Commands
Expand All @@ -7,10 +8,16 @@ namespace PSDocs.Commands
internal sealed class TitleCommand : KeywordCmdlet
{
[Parameter(Position = 0, Mandatory = true)]
[AllowNull, AllowEmptyString]
public string Text { get; set; }

protected override void BeginProcessing()
{
if (string.IsNullOrEmpty(Text))
{
GetPipeline().Writer.WarnTitleEmpty();
return;
}
GetBuilder().Title(Text);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/PSDocs/Pipeline/PipelineWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public static void WarnSourcePathNotFound(this IPipelineWriter writer)
writer.WriteWarning(PSDocsResources.SourceNotFound);
}

public static void WarnTitleEmpty(this IPipelineWriter writer)
{
if (writer == null)
return;

writer.WriteWarning(PSDocsResources.TitleEmpty);
}

public static void ErrorInvariantCulture(this IPipelineWriter writer)
{
if (writer == null)
Expand Down
9 changes: 9 additions & 0 deletions src/PSDocs/Resources/PSDocsResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/PSDocs/Resources/PSDocsResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,7 @@
<value>The source was not found.</value>
<comment>Occurs when the source file specified does not exist.</comment>
</data>
<data name="TitleEmpty" xml:space="preserve">
<value>The specified document title is empty and will be skipped.</value>
</data>
</root>
7 changes: 7 additions & 0 deletions tests/PSDocs.Tests/FromFile.Keyword.Doc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,11 @@ Document 'MultipleTitle' {
Title 'Title 2'
}

# Synopsis: Tests Title with empty or null string
Document 'EmptyTitle' {
$value = ''
Title $notValue
Title $value
}

#endregion Title
7 changes: 7 additions & 0 deletions tests/PSDocs.Tests/PSDocs.Title.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Describe 'PSDocs -- Title keyword' -Tag Title {
Path = $docFilePath
InputObject = $testObject
PassThru = $True
ErrorAction = [System.Management.Automation.ActionPreference]::Stop
}
It 'With single title' {
$result = Invoke-PSDocument @invokeParams -Name 'SingleTitle';
Expand All @@ -34,5 +35,11 @@ Describe 'PSDocs -- Title keyword' -Tag Title {
$result = Invoke-PSDocument @invokeParams -Name 'MultipleTitle';
$result | Should -Match "^(`# Title 2(\r|\n|\r\n))";
}
It 'With empty title' {
$Null = Invoke-PSDocument @invokeParams -Name 'EmptyTitle' -WarningAction SilentlyContinue -WarningVariable outWarnings;
$outWarnings = @($outWarnings);
$outWarnings | Should -Not -BeNullOrEmpty;
$outWarnings.Length | Should -Be 2;
}
}
}

0 comments on commit 68f31a7

Please sign in to comment.