Skip to content

Commit

Permalink
code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nonik0 committed Oct 19, 2023
1 parent c57923b commit 716f83e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
13 changes: 5 additions & 8 deletions src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static (string, BicepMetadata) ConvertBicepToJson(string bicepPath)
}

using var stringWriter = new StringWriter();
var compilation = BicepCompiler.CreateCompilation(new Uri(bicepPath)).Result; // TODO: sync call OK?
var compilation = BicepCompiler.CreateCompilation(new Uri(bicepPath)).Result;
var emitter = new TemplateEmitter(compilation.GetEntrypointSemanticModel());
var emitResult = emitter.Emit(stringWriter);

Expand All @@ -97,27 +97,24 @@ string GetPathRelativeToEntryPoint(string absolutePath) => Path.GetRelativePath(
var bicepSourceFile = sourceFileAndMetadata.Key as BicepSourceFile;
var pathRelativeToEntryPoint = GetPathRelativeToEntryPoint(bicepSourceFile.FileUri.AbsolutePath);
var modules = sourceFileAndMetadata.Value
.Select(artifactRefAndResult =>
.Select(artifactRefAndUriResult =>
{
var artifact = artifactRefAndResult.Key;
var uriResult = artifactRefAndResult.Value;
// Do not include modules imported from public/private registries, as it is more useful for user to see line number
// of the module declaration itself instead of line number in the module as the user does not control template in registry directly
if (artifactRefAndResult.Key is not ModuleDeclarationSyntax moduleDeclaration
if (artifactRefAndUriResult.Key is not ModuleDeclarationSyntax moduleDeclaration
|| moduleDeclaration.Path is not StringSyntax moduleDeclarationPath
|| moduleDeclarationPath.SegmentValues.Any(v => IsModuleRegistryPathRegex.IsMatch(v)))
{
return null;
}
if (!artifactRefAndResult.Value.IsSuccess())
if (!artifactRefAndUriResult.Value.IsSuccess())
{
return null;
}
var moduleLine = TextCoordinateConverter.GetPosition(bicepSourceFile.LineStarts, moduleDeclaration.Span.Position).line;
var modulePath = new FileInfo(artifactRefAndResult.Value.Unwrap().AbsolutePath).FullName; // converts path to current platform
var modulePath = new FileInfo(artifactRefAndUriResult.Value.Unwrap().AbsolutePath).FullName; // converts path to current platform
// Use relative paths for bicep to match file paths used in bicep modules and source map
if (modulePath.EndsWith(".bicep"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Azure.Deployments.Core.Definitions.Schema;
using Azure.Deployments.Core.Json;
using Azure.Deployments.Templates.Engines;
using Azure.Deployments.Templates.Exceptions;
using Microsoft.Azure.Templates.Analyzer.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.ResourceStack.Common.Collections;
Expand Down Expand Up @@ -1118,7 +1119,9 @@ public void ProcessTemplate_ValidTemplateUsingManagementGroupFunction_ProcessTem
}

[TestMethod]
public void ProcessTemplate_ValidTemplateWithPartialParameterList_ProcessTemplateFunction()
[DataRow(false)]
[DataRow(true)]
public void ProcessTemplate_ValidTemplateWithPartialParameterList_ProcessTemplateFunction(bool generateMissingParameters)
{
string parametersJson = @"{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"",
Expand Down Expand Up @@ -1156,9 +1159,18 @@ public void ProcessTemplate_ValidTemplateWithPartialParameterList_ProcessTemplat

var armTemplateProcessor = new ArmTemplateProcessor(templateJson);

JToken template = armTemplateProcessor.ProcessTemplate(parametersJson, null, generateMissingParameters: true);

Assert.AreEqual(2, template["parameters"].Count());
if (generateMissingParameters)
{
JToken template = armTemplateProcessor.ProcessTemplate(parametersJson, null, generateMissingParameters);
Assert.AreEqual(2, template["parameters"].Count());
Assert.IsNotNull(template["parameters"]["trafficRoutingMethod"]);
Assert.IsNotNull(template["parameters"]["location"]);
}
else
{
Assert.ThrowsException<TemplateValidationException>(
() => armTemplateProcessor.ProcessTemplate(parametersJson, null, generateMissingParameters));
}
}

private string GenerateTemplateWithOutputs(string outputValue)
Expand Down
2 changes: 1 addition & 1 deletion src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ internal InsensitiveDictionary<JToken> PopulateParameters(string parameters)

if (parametersObject["parameters"] == null)
{
throw new Exception("Parameteres property is not specified in the ARM Template parameters provided. Please ensure ARM Template parameters follows the following JSON schema https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#");
throw new Exception("Parameters property is not specified in the ARM Template parameters provided. Please ensure ARM Template parameters follows the following JSON schema https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#");
}

foreach (var parameter in parametersObject.InsensitiveToken("parameters").Value<JObject>()?.Properties() ?? Enumerable.Empty<JProperty>())
Expand Down
4 changes: 4 additions & 0 deletions src/Analyzer.TemplateProcessor/PlaceholderInputGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ internal static string GeneratePlaceholderParameters(string armTemplate, string
{
jsonParameters = paramsObject;
}
else
{
throw new Exception("Parameters property is not specified in the ARM Template parameters provided. Please ensure ARM Template parameters follows the following JSON schema https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#");
}
}

JToken parameters = jsonTemplate.InsensitiveToken("parameters");
Expand Down

0 comments on commit 716f83e

Please sign in to comment.