Skip to content

Commit

Permalink
Fixed null reference in convention #725 (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed May 20, 2021
1 parent a6d6d8b commit b076f36
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ See [upgrade notes][upgrade-notes] for helpful information when upgrading from p

## Unreleased

What's changed since pre-release v1.4.0-B2105019:

- Bug fixes:
- Fixed null reference in convention for nested exceptions. [#725](https://github.com/microsoft/PSRule/issues/725)

## v1.4.0-B2105019 (pre-release)

What's changed since pre-release v1.4.0-B2105004:
Expand Down
3 changes: 1 addition & 2 deletions src/PSRule/Commands/InvokeConventionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ protected override void ProcessRecord()
{
// Evaluate script block
context.PushScope(Scope);
var invokeResult = RuleConditionHelper.Create(Body.Invoke());
WriteObject(invokeResult);
Body.Invoke();
}
finally
{
Expand Down
5 changes: 5 additions & 0 deletions src/PSRule/Runtime/RunspaceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ public void Error(Exception ex)
var scriptStackTrace = errorRecord != null ? GetStackTrace(errorRecord) : null;
var category = errorRecord != null ? errorRecord.CategoryInfo.Category : ErrorCategory.NotSpecified;
var errorId = errorRecord != null ? GetErrorId(errorRecord) : null;
if (RuleRecord == null)
{
Writer.WriteError(errorRecord);
return;
}
RuleRecord.Outcome = RuleOutcome.Error;
RuleRecord.Error = new ErrorInfo(
message: ex.Message,
Expand Down
5 changes: 5 additions & 0 deletions tests/PSRule.Tests/FromFileConventions.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ Export-PSRuleConvention 'Convention.Expansion' -If { $TargetObject.Name -eq 'Tes
$PSRule.Import(@($newObject, $newObject));
}

# Synopsis: A convention for unit testing
Export-PSRuleConvention 'Convention.WithException' -Begin {
throw 'Some exception';
}

# Synopsis: A rule for testing conventions
Rule 'ConventionTest' {
$Assert.HasFieldValue($PSRule.Data, 'count', 1);
Expand Down
8 changes: 7 additions & 1 deletion tests/PSRule.Tests/PSRule.Conventions.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ $outputPath = Join-Path -Path $rootPath -ChildPath out/tests/PSRule.Tests/Conven
Remove-Item -Path $outputPath -Force -Recurse -Confirm:$False -ErrorAction Ignore;
$Null = New-Item -Path $outputPath -ItemType Directory -Force;

Describe 'PSRule -- Conventions' -Tag 'Conventions' {
Describe 'PSRule -- Conventions' -Tag 'Convention' {
$rulePath = Join-Path -Path $here -ChildPath 'FromFileConventions.Rule.ps1';

Context 'With -Convention' {
Expand Down Expand Up @@ -91,5 +91,11 @@ Describe 'PSRule -- Conventions' -Tag 'Conventions' {
$result[0].TargetName | Should -Be 'TestObject1';
$result[1].TargetName | Should -Be 'TestObject2';
}

It 'Handles nested exceptions' {
$Null = Invoke-PSRule @invokeParams -Name 'ConventionTest' -Convention 'Convention.WithException' -ErrorVariable errors -ErrorAction SilentlyContinue;
$errors | Should -Not -BeNullOrEmpty;
$errors.Exception.Message | Should -Be 'Some exception';
}
}
}

0 comments on commit b076f36

Please sign in to comment.