Skip to content

Commit

Permalink
Fixed JSON output format returns exception when no results are produced
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite committed Jan 6, 2025
1 parent 3f74dfd commit 8187d48
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ What's changed since pre-release v3.0.0-B0351:
[#1842](https://github.com/microsoft/PSRule/issues/1842)
- Fixed duplicate reasons are reported for the same rule by @BernieWhite.
[#2553](https://github.com/microsoft/PSRule/issues/2553)
- Fixed JSON output format returns exception when no results are produced by @BernieWhite.
[#1832](https://github.com/microsoft/PSRule/issues/1832)

## v3.0.0-B0351 (pre-release)

Expand Down
9 changes: 9 additions & 0 deletions src/PSRule/Pipeline/Output/JsonOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

namespace PSRule.Pipeline.Output;

#nullable enable

internal sealed class JsonOutputWriter : SerializationOutputWriter<object>
{
private const string EMPTY_ARRAY = "[]";

internal JsonOutputWriter(PipelineWriter inner, PSRuleOption option, ShouldProcess shouldProcess)
: base(inner, option, shouldProcess) { }

Expand All @@ -20,6 +24,9 @@ protected override string Serialize(object[] o)

internal static string ToJson(object[] o, int? jsonIndent)
{
if (o == null || o.Length == 0)
return EMPTY_ARRAY;

using var stringWriter = new StringWriter();
using var jsonTextWriter = new JsonCommentWriter(stringWriter);

Expand Down Expand Up @@ -56,3 +63,5 @@ internal static string ToJson(object[] o, int? jsonIndent)
return stringWriter.ToString();
}
}

#nullable restore
7 changes: 7 additions & 0 deletions src/PSRule/Pipeline/Output/YamlOutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace PSRule.Pipeline.Output;

#nullable enable

internal sealed class YamlOutputWriter : SerializationOutputWriter<object>
{
internal YamlOutputWriter(PipelineWriter inner, PSRuleOption option, ShouldProcess shouldProcess)
: base(inner, option, shouldProcess) { }

protected override string Serialize(object[] o)
{
if (o == null || o.Length == 0)
return string.Empty;

return o[0] is IEnumerable<Baseline> baselines ? ToBaselineYaml(baselines) : ToYaml(o);
}

Expand Down Expand Up @@ -57,3 +62,5 @@ internal static string ToBaselineYaml(IEnumerable<Baseline> baselines)
return output.ToString();
}
}

#nullable restore
20 changes: 18 additions & 2 deletions tests/PSRule.Tests/Pipeline/Output/JsonOutputWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ namespace PSRule.Pipeline.Output;
public sealed class JsonOutputWriterTests : OutputWriterBaseTests
{
[Fact]
public void Json()
public void Output_WithIndent_ShouldReturnIndentedResults()
{
var option = GetOption();
option.Output.JsonIndent = 2;
option.Repository.Url = "https://github.com/microsoft/PSRule.UnitTest";
var output = new TestWriter(option);
var result = new InvokeResult(GetRun());
result.Add(GetPass());
result.Add(GetFail());
result.Add(GetFail("rid-003", SeverityLevel.Warning));
result.Add(GetFail("rid-004", SeverityLevel.Information));

var writer = new JsonOutputWriter(output, option, null);
writer.Begin();
writer.WriteObject(result, false);
Expand Down Expand Up @@ -132,4 +132,20 @@ public void Json()
}
]", output.Output.OfType<string>().FirstOrDefault());
}

[Fact]
public void Output_WithNoRecords_ShouldReturnEmptyArray()
{
var option = GetOption();
option.Output.JsonIndent = 2;
var output = new TestWriter(option);
var result = new InvokeResult(GetRun());
var writer = new JsonOutputWriter(output, option, null);

writer.Begin();
writer.WriteObject(result, false);
writer.End(new DefaultPipelineResult(null, Options.BreakLevel.None));

Assert.Equal("[]", output.Output.OfType<string>().FirstOrDefault());
}
}
18 changes: 15 additions & 3 deletions tests/PSRule.Tests/Pipeline/Output/YamlOutputWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ namespace PSRule.Pipeline.Output;
/// </summary>
public sealed class YamlOutputWriterTests : OutputWriterBaseTests
{

[Fact]
public void Yaml()
public void Output_WithIndent_ShouldReturnResults()
{
var option = GetOption();
option.Repository.Url = "https://github.com/microsoft/PSRule.UnitTest";
var output = new TestWriter(option);
var result = new InvokeResult(GetRun());
result.Add(GetPass());
Expand Down Expand Up @@ -109,4 +107,18 @@ over two lines.
", output.Output.OfType<string>().FirstOrDefault());
}

[Fact]
public void Output_WithNoRecords_ShouldReturnSuccessfully()
{
var option = GetOption();
var output = new TestWriter(option);
var result = new InvokeResult(GetRun());
var writer = new YamlOutputWriter(output, option, null);

writer.Begin();
writer.WriteObject(result, false);
writer.End(new DefaultPipelineResult(null, Options.BreakLevel.None));

Assert.Empty(output.Output.OfType<string>().FirstOrDefault());
}
}

0 comments on commit 8187d48

Please sign in to comment.