Skip to content

Commit

Permalink
Fixed regression when handing ambiguous mock array outputs #2801 (#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Apr 6, 2024
1 parent cc88cd5 commit fa9c091
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers

## Unreleased

What's changed since v1.35.1:

- Bug fixes:
- Fixed regression when handing ambiguous mock array outputs by @BernieWhite.
[#2801](https://github.com/Azure/PSRule.Rules.Azure/issues/2801)

## v1.35.1

What's changed since v1.35.0:
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule.Rules.Azure/Data/Template/Mocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public TValue GetValue<TValue>()

public JToken GetValue(TypePrimitive type)
{
return type == TypePrimitive.Array ? this : null;
return type == TypePrimitive.None || type == TypePrimitive.Array ? this : null;
}

public JToken GetValue(object key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@
<None Update="Tests.Bicep.35.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tests.Bicep.36.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tests.Bicep.4.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
19 changes: 18 additions & 1 deletion tests/PSRule.Rules.Azure.Tests/TemplateVisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ public void UnionMockWithArray()
[Fact]
public void ProcessTemplate_WhenIndexIntoMock_ShouldReturnMock()
{
var resources = ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _);
ProcessTemplate(GetSourcePath("Tests.Bicep.35.json"), null, out _);
}

/// <summary>
Expand All @@ -1116,6 +1116,23 @@ public void ProcessTemplate_WhenParameterNullWithDefault_ShouldUseDefault()
Assert.Equal("Standard_LRS", actual["sku"]["name"].Value<string>());
}

/// <summary>
/// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2801.
/// </summary>
[Fact]
public void ProcessTemplate_WhenMockArrayOutputAndTypeIsUnknown_ShouldReturnArray()
{
ProcessTemplate(GetSourcePath("Tests.Bicep.36.json"), null, out var templateContext);

Assert.True(templateContext.RootDeployment.TryOutput("items", out JObject result));
var items = result["value"][0]["items"].Value<JArray>();

Assert.Single(items);
var actual = items[0].Value<JObject>();
Assert.Equal("name1", actual["name"].Value<string>());
Assert.Equal("value1", actual["value"].Value<string>());
}

#region Helper methods

private static string GetSourcePath(string fileName)
Expand Down
14 changes: 14 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Test case for https://github.com/Azure/PSRule.Rules.Azure/issues/2801.

module child 'Tests.Bicep.36.child.bicep' = {
name: 'child'
}

output items array = [
{
items: child.outputs.items
}
]
12 changes: 12 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.child.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

output items array = concat(
[],
[
{
name: 'name1'
value: 'value1'
}
]
)
52 changes: 52 additions & 0 deletions tests/PSRule.Rules.Azure.Tests/Tests.Bicep.36.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.26.54.24096",
"templateHash": "12279334207887669240"
}
},
"resources": [
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2022-09-01",
"name": "child",
"properties": {
"expressionEvaluationOptions": {
"scope": "inner"
},
"mode": "Incremental",
"template": {
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.26.54.24096",
"templateHash": "14837468981850150943"
}
},
"resources": [],
"outputs": {
"items": {
"type": "array",
"value": "[concat(createArray(), createArray(createObject('name', 'name1', 'value', 'value1')))]"
}
}
}
}
}
],
"outputs": {
"items": {
"type": "array",
"value": [
{
"items": "[reference(resourceId('Microsoft.Resources/deployments', 'child'), '2022-09-01').outputs.items.value]"
}
]
}
}
}

0 comments on commit fa9c091

Please sign in to comment.