Skip to content

Commit

Permalink
Fixed error action of include keyword #127
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jan 18, 2021
1 parent 9ebc42e commit d0c2852
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 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.
- Bug fixes:
- Fixed use of error action preference with `Include` keyword. [#127](https://github.com/BernieWhite/PSDocs/issues/127)

## v0.7.0

Expand Down
15 changes: 14 additions & 1 deletion src/PSDocs/Commands/IncludeCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

using PSDocs.Models;
using PSDocs.Resources;
using PSDocs.Runtime;
using System.IO;
using System.Management.Automation;

namespace PSDocs.Commands
Expand Down Expand Up @@ -29,7 +31,18 @@ protected override void BeginProcessing()

protected override void EndProcessing()
{
WriteObject(ModelHelper.Include(BaseDirectory, Culture, FileName, UseCulture));
var result = ModelHelper.Include(BaseDirectory, Culture, FileName, UseCulture);
if (result == null || !result.Exists)
{
WriteError(new ErrorRecord(
exception: new FileNotFoundException(PSDocsResources.IncludeNotFound, result.Path),
errorId: "PSDocs.Runtime.IncludeNotFound",
errorCategory: ErrorCategory.ObjectNotFound,
targetObject: result.Path
));
return;
}
WriteObject(result);
}
}
}
16 changes: 15 additions & 1 deletion src/PSDocs/Models/Include.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@

using System.IO;

namespace PSDocs.Models
{
public sealed class Include : DocumentNode
{
private string _Path;

public override DocumentNodeType Type => DocumentNodeType.Include;

public string Path { get; set; }
public string Path
{
get { return _Path; }
set
{
_Path = value;
Exists = File.Exists(_Path);
}
}

public string Content { get; set; }

internal bool Exists { get; private set; }

public override string ToString()
{
return Content;
Expand Down
16 changes: 8 additions & 8 deletions src/PSDocs/Models/ModelHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

using PSDocs.Configuration;
using PSDocs.Resources;
using System.IO;

namespace PSDocs.Models
Expand Down Expand Up @@ -55,15 +54,16 @@ public static Include Include(string baseDirectory, string culture, string fileN
{
baseDirectory = PSDocumentOption.GetRootedPath(baseDirectory);
var absolutePath = Path.IsPathRooted(fileName) ? fileName : Path.Combine(baseDirectory, (useCulture ? culture : string.Empty), fileName);
if (!File.Exists(absolutePath))
throw new FileNotFoundException(PSDocsResources.IncludeNotFound, absolutePath);

var text = File.ReadAllText(absolutePath);
return new Include
var result = new Include
{
Path = absolutePath,
Content = text,
Path = absolutePath
};
if (result.Exists)
{
var text = File.ReadAllText(absolutePath);
result.Content = text;
}
return result;
}
}
}
5 changes: 5 additions & 0 deletions tests/PSDocs.Tests/FromFile.Keyword.Doc.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ Document 'IncludeCulture' {
Include IncludeFile3.md -UseCulture -BaseDirectory tests/PSDocs.Tests/
}

# Synopsis: Test Include keyword with -ErrorAction SilentlyContinue
Document 'IncludeOptional' {
Include 'NotFile.md' -ErrorAction SilentlyContinue;
}

#endregion Include

#region Metadata
Expand Down
13 changes: 8 additions & 5 deletions tests/PSDocs.Tests/PSDocs.Include.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ $outputPath = Join-Path -Path $rootPath -ChildPath out/tests/PSDocs.Tests/Includ
Remove-Item -Path $outputPath -Force -Recurse -Confirm:$False -ErrorAction Ignore;
$Null = New-Item -Path $outputPath -ItemType Directory -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
$dummyObject = New-Object -TypeName PSObject;

Describe 'PSDocs -- Include keyword' -Tag Include {
$docFilePath = Join-Path -Path $here -ChildPath 'FromFile.Keyword.Doc.ps1';
Expand All @@ -27,12 +26,12 @@ Describe 'PSDocs -- Include keyword' -Tag Include {
$invokeParams = @{
Path = $docFilePath
OutputPath = $outputPath
# PassThru = $True
ErrorAction = [System.Management.Automation.ActionPreference]::Stop
}

It 'Should include a relative path' {
$outputDoc = "$outputPath\IncludeRelative.md";
$result = Invoke-PSDocument @invokeParams -InputObject $rootPath -Name 'IncludeRelative';
$Null = Invoke-PSDocument @invokeParams -InputObject $rootPath -Name 'IncludeRelative';

Test-Path -Path $outputDoc | Should -Be $True;
$outputDoc | Should -FileContentMatch 'This is included from an external file.';
Expand All @@ -41,14 +40,14 @@ Describe 'PSDocs -- Include keyword' -Tag Include {

It 'Should include an absolute path' {
$outputDoc = "$outputPath\IncludeAbsolute.md";
$result = Invoke-PSDocument @invokeParams -InputObject $rootPath -Name 'IncludeAbsolute';
$Null = Invoke-PSDocument @invokeParams -InputObject $rootPath -Name 'IncludeAbsolute';

Test-Path -Path $outputDoc | Should -Be $True;
$outputDoc | Should -FileContentMatch 'This is included from an external file.';
}

It 'Should include from culture' {
$result = Invoke-PSDocument @invokeParams -Culture 'en-AU','en-US' -Name 'IncludeCulture';
$Null = Invoke-PSDocument @invokeParams -Culture 'en-AU','en-US' -Name 'IncludeCulture';

$outputDoc = "$outputPath\en-AU\IncludeCulture.md";
Test-Path -Path $outputDoc | Should -Be $True;
Expand All @@ -58,5 +57,9 @@ Describe 'PSDocs -- Include keyword' -Tag Include {
Test-Path -Path $outputDoc | Should -Be $True;
$outputDoc | Should -FileContentMatch 'This is en-US.';
}

It 'Should include when file exists' {
$Null = Invoke-PSDocument @invokeParams -Name 'IncludeOptional';
}
}
}

0 comments on commit d0c2852

Please sign in to comment.