Fixed empty markdown out of bounds #516 (#532)

This commit is contained in:
Bernie White 2020-08-30 12:51:16 +10:00 коммит произвёл GitHub
Родитель 7122f70168
Коммит a1ea08f10e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 26 добавлений и 6 удалений

Просмотреть файл

@ -6,6 +6,9 @@
## Unreleased
- Bug fixes:
- Fixed out of bounds exception when empty markdown documentation is used. [#516](https://github.com/microsoft/PSRule/issues/516)
## v0.20.0-B2008010 (pre-release)
What's changed since pre-release v0.20.0-B2008002:

Просмотреть файл

@ -9,6 +9,7 @@ using System;
using System.Collections;
using System.IO;
using System.Management.Automation;
using System.Threading;
namespace PSRule.Commands
{
@ -72,7 +73,7 @@ namespace PSRule.Commands
protected override void ProcessRecord()
{
if (!IsScriptScope())
throw new RuleRuntimeException(string.Format(PSRuleResources.KeywordScriptScope, LanguageKeywords.Rule));
throw new RuleRuntimeException(string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.KeywordScriptScope, LanguageKeywords.Rule));
var context = RunspaceContext.CurrentThread;
var metadata = GetMetadata(MyInvocation.ScriptName, MyInvocation.ScriptLineNumber, MyInvocation.OffsetInLine);
@ -96,6 +97,7 @@ namespace PSRule.Commands
if (helpInfo.Synopsis == null)
helpInfo.Synopsis = metadata.Synopsis;
#pragma warning disable CA2000 // Dispose objects before losing scope, needs to be passed to pipeline
var block = new RuleBlock(
source: source,
ruleName: Name,
@ -106,6 +108,7 @@ namespace PSRule.Commands
configuration: Configure,
extent: extent
);
#pragma warning restore CA2000 // Dispose objects before losing scope, needs to be passed to pipeline
WriteObject(block);
}
@ -155,8 +158,13 @@ namespace PSRule.Commands
private static bool TryDocument(string path, out RuleDocument document)
{
document = null;
var markdown = File.ReadAllText(path);
if (string.IsNullOrEmpty(markdown))
return false;
var reader = new MarkdownReader(yamlHeaderOnly: false);
var stream = reader.Read(File.ReadAllText(path), path);
var stream = reader.Read(markdown, path);
var lexer = new RuleLexer();
document = lexer.Process(stream);
return document != null;

Просмотреть файл

@ -59,6 +59,9 @@ namespace PSRule.Parser
public TokenStream Read(string markdown, string path)
{
if (string.IsNullOrEmpty(markdown))
return _Output;
_Context = MarkdownReaderMode.None;
_Stream = new MarkdownStream(markdown);
@ -246,7 +249,7 @@ namespace PSRule.Parser
_Context = MarkdownReaderMode.None;
}
private string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokens flag)
private static string UnwrapStyleMarkers(MarkdownStream stream, out MarkdownTokens flag)
{
flag = MarkdownTokens.None;
@ -308,12 +311,12 @@ namespace PSRule.Parser
return text;
}
private string Pad(string text, char c, int left = 0, int right = 0)
private static string Pad(string text, char c, int left = 0, int right = 0)
{
return text.PadLeft(text.Length + left, c).PadRight(text.Length + left + right, c);
}
private bool IsList(string text)
private static bool IsList(string text)
{
var clean = text.Trim();
@ -328,7 +331,7 @@ namespace PSRule.Parser
return false;
}
private MarkdownTokens GetEnding(int lineEndings)
private static MarkdownTokens GetEnding(int lineEndings)
{
if (lineEndings == 0)
return MarkdownTokens.None;

Просмотреть файл

@ -1325,6 +1325,12 @@ Describe 'Get-PSRule' -Tag 'Get-PSRule','Common' {
$result | Should -Not -BeNullOrEmpty;
$result.RuleName | Should -Be 'WithNoSynopsis';
$result.Synopsis | Should -BeNullOrEmpty;
# Empty markdown
$result = Get-PSRule -Path $ruleFilePath -Name 'WithNoSynopsis' -Culture 'en-YY';
$result | Should -Not -BeNullOrEmpty;
$result.RuleName | Should -Be 'WithNoSynopsis';
$result.Synopsis | Should -BeNullOrEmpty;
}
finally {
[PSRule.Configuration.PSRuleOption]::UseCurrentCulture();

Просмотреть файл