Skip to content

Commit

Permalink
Fixes for LocalizedData variable #1083 #1477 (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Mar 17, 2023
1 parent de957b6 commit 5dcde18
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since pre-release v2.8.0-B0076:

- Bug fixes:
- Fixed LocalizedData is not exposed to if pre-conditions by @BernieWhite.
[#1083](https://github.com/microsoft/PSRule/issues/1083)
- Fixed LocalizedData is not exposed to conventions by @BernieWhite.
[#1477](https://github.com/microsoft/PSRule/issues/1477)

## v2.8.0-B0076 (pre-release)

What's changed since pre-release v2.8.0-B0034:
Expand Down
13 changes: 7 additions & 6 deletions src/PSRule/Definitions/Conventions/ScriptBlockConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,37 @@ internal ScriptBlockConvention(

public override void Initialize(RunspaceContext context, IEnumerable input)
{
InvokeConventionBlock(_Initialize, input);
InvokeConventionBlock(context, Source, _Initialize, input);
}

public override void Begin(RunspaceContext context, IEnumerable input)
{
InvokeConventionBlock(_Begin, input);
InvokeConventionBlock(context, Source, _Begin, input);
}

public override void Process(RunspaceContext context, IEnumerable input)
{
InvokeConventionBlock(_Process, input);
InvokeConventionBlock(context, Source, _Process, input);
}

public override void End(RunspaceContext context, IEnumerable input)
{
InvokeConventionBlock(_End, input);
InvokeConventionBlock(context, Source, _End, input);
}

private static void InvokeConventionBlock(LanguageScriptBlock block, IEnumerable input)
private static void InvokeConventionBlock(RunspaceContext context, SourceFile source, LanguageScriptBlock block, IEnumerable input)
{
if (block == null)
return;

try
{
context.EnterLanguageScope(source);
block.Invoke();
}
finally
{

context.ExitLanguageScope(source);
}
}

Expand Down
32 changes: 30 additions & 2 deletions tests/PSRule.Tests/ConventionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ public void ConventionOrder()
Assert.Equal(11, actual2);
}

/// <summary>
/// Test that localized data is accessible to conventions at each block.
/// </summary>
[Fact]
public void WithLocalizedData()
{
var testObject1 = new TestObject { Name = "TestObject1" };
var option = GetOption();
option.Rule.Include = new string[] { "WithLocalizedDataPrecondition" };
option.Convention.Include = new string[] { "Convention.WithLocalizedData" };
var writer = new TestWriter(option);
var builder = PipelineBuilder.Invoke(GetSource(), option, null);
var pipeline = builder.Build(writer);

Assert.NotNull(pipeline);
pipeline.Begin();
pipeline.Process(PSObject.AsPSObject(testObject1));
pipeline.End();
Assert.NotEmpty(writer.Information);
Assert.Equal("LocalizedMessage for en. Format=Initialize.", writer.Information[0] as string);
Assert.Equal("LocalizedMessage for en. Format=Begin.", writer.Information[1] as string);
Assert.Equal("LocalizedMessage for en. Format=Precondition.", writer.Information[2] as string);
Assert.Equal("LocalizedMessage for en. Format=Process.", writer.Information[3] as string);
Assert.Equal("LocalizedMessage for en. Format=End.", writer.Information[4] as string);
}

#region Helper methods

private static Source[] GetSource()
Expand All @@ -70,9 +96,11 @@ private static Source[] GetSource()
return builder.Build();
}

private PSRuleOption GetOption(string path = null)
private static PSRuleOption GetOption(string path = null)
{
return path == null ? new PSRuleOption() : PSRuleOption.FromFile(path);
var option = path == null ? new PSRuleOption() : PSRuleOption.FromFile(path);
option.Output.Culture = new[] { "en-US" };
return option;
}

private static string GetSourcePath(string fileName)
Expand Down
16 changes: 16 additions & 0 deletions tests/PSRule.Tests/FromFileConventions.Rule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ Rule 'ConventionTest' {
$store = $PSRule.GetService('Store');
$Assert.HasFieldValue($store, 'Name', 'TestObject1');
}

# Synopsis: A convention for unit testing localized data.
Export-PSRuleConvention 'Convention.WithLocalizedData' -Initialize {
Write-Information -MessageData ($LocalizedData.WithLocalizedDataMessage -f 'Initialize')
} -Begin {
Write-Information -MessageData ($LocalizedData.WithLocalizedDataMessage -f 'Begin')
} -Process {
Write-Information -MessageData ($LocalizedData.WithLocalizedDataMessage -f 'Process')
} -End {
Write-Information -MessageData ($LocalizedData.WithLocalizedDataMessage -f 'End')
}

# Syopsis: Test localized data in pre-condition.
Rule 'WithLocalizedDataPrecondition' -If { Write-Information -MessageData ($LocalizedData.WithLocalizedDataMessage -f 'Precondition'); $True; } {
$Assert.Pass();
}
3 changes: 3 additions & 0 deletions tests/PSRule.Tests/PSRule.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<None Update="Dockerfile">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="en\PSRule-rules.psd1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="en\SuppressWithNonProdTag.md">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
11 changes: 11 additions & 0 deletions tests/PSRule.Tests/TestWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ internal sealed class TestWriter : PipelineWriter
{
internal List<ErrorRecord> Errors = new();
internal List<string> Warnings = new();
internal List<object> Information = new();
internal List<object> Output = new();

public TestWriter(PSRuleOption option)
Expand Down Expand Up @@ -51,5 +52,15 @@ public override void WriteObject(object sendToPipeline, bool enumerateCollection
Output.Add(sendToPipeline);
}
}

public override bool ShouldWriteInformation()
{
return true;
}

public override void WriteInformation(InformationRecord informationRecord)
{
Information.Add(informationRecord.MessageData);
}
}
}

0 comments on commit 5dcde18

Please sign in to comment.