Skip to content

Commit

Permalink
Consolidate TemplateEvaluator classes (#15581)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenglol authored Nov 15, 2024
1 parent f507ed0 commit c7bdcda
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 348 deletions.
266 changes: 0 additions & 266 deletions src/Bicep.Cli/Services/TemplateEvaluator.cs

This file was deleted.

34 changes: 31 additions & 3 deletions src/Bicep.Cli/Services/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the MIT License.

using System.Collections.Immutable;
using Azure.Deployments.Core.Definitions.Schema;
using Bicep.Core.Emit;
using Bicep.Core.Intermediate;
using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.Utils;
using Microsoft.WindowsAzure.ResourceStack.Common.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -37,9 +39,23 @@ public static TestResults Run(ImmutableArray<TestSymbol> testDeclarations)
semanticModel is SemanticModel testSemanticModel)
{
var parameters = TryGetParameters(testSemanticModel, testDeclaration);
var template = GetTemplate(testSemanticModel);
var templateJToken = GetTemplate(testSemanticModel);
TestEvaluation evaluation;

try
{
var template = TemplateEvaluator.Evaluate(templateJToken, parameters);
var allAssertions = template.Asserts?.Select(p => new AssertionResult(p.Key, (bool)p.Value.Value)).ToImmutableArray() ?? [];
var failedAssertions = allAssertions.Where(a => !a.Result).Select(a => a).ToImmutableArray();

evaluation = new TestEvaluation(template, null, allAssertions, failedAssertions);
}
catch (Exception exception)
{
var error = exception.Message;
evaluation = new TestEvaluation(null, error, [], []);
}

var evaluation = TemplateEvaluator.Evaluate(template, parameters);
var testResult = new TestResult(testDeclaration, evaluation);

testResults.Add(testResult);
Expand Down Expand Up @@ -84,7 +100,19 @@ private static JToken GetTemplate(SemanticModel model)
new TemplateWriter(model).EmitTestParameters(emitter, parametersExpression);
writer.Flush();

return textWriter.ToString().FromJson<JObject>();
var parameters = textWriter.ToString().FromJson<JObject>().Properties()
.ToDictionary(x => x.Name, x => new JObject()
{
["value"] = x.Value,
})
.ToJToken();

return new JObject()
{
["$schema"] = "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
["contentVersion"] = "1.0.0.0",
["parameters"] = parameters,
};
}

return null;
Expand Down
7 changes: 4 additions & 3 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using Bicep.Core.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -1620,7 +1621,7 @@ param intParam int

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();

var parameters = TemplateEvaluator.ParseParametersFile(result.Parameters);
var parameters = TemplateHelper.ConvertAndAssertParameters(result.Parameters);
parameters["intParam"].Should().DeepEqual(9);
}

Expand Down Expand Up @@ -1649,7 +1650,7 @@ param intParam int

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();

var parameters = TemplateEvaluator.ParseParametersFile(result.Parameters);
var parameters = TemplateHelper.ConvertAndAssertParameters(result.Parameters);
parameters["intParam"].Should().DeepEqual(9);
}

Expand Down Expand Up @@ -1925,7 +1926,7 @@ func isWindows(hostingPlanName string) string => contains('-windows-', hostingPl
result.Diagnostics.Should().BeEmpty();
result.Template.Should().NotBeNull();

var evaluated = TemplateEvaluator.Evaluate(result.Template);
var evaluated = TemplateEvaluator.Evaluate(result.Template).ToJToken();
evaluated.Should().HaveValueAtPath("outputs.out.value", "ASP999");
}

Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/Emit/TemplateEmitterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task ValidBicep_TemplateEmiterShouldProduceExpectedTemplate(DataSet
actualLocation: compiledFilePath);

// validate that the template is parseable by the deployment engine
TemplateHelper.TemplateShouldBeValid(outputFile);
UnitTests.Utils.TemplateHelper.TemplateShouldBeValid(outputFile);
}

[DataTestMethod]
Expand All @@ -100,7 +100,7 @@ public async Task ValidBicep_EmitTemplate_should_produce_expected_symbolicname_t
actualLocation: compiledFilePath);

// validate that the template is parseable by the deployment engine
TemplateHelper.TemplateShouldBeValid(outputFile);
UnitTests.Utils.TemplateHelper.TemplateShouldBeValid(outputFile);
}

[DataTestMethod]
Expand Down
Loading

0 comments on commit c7bdcda

Please sign in to comment.