From 716f83ebd49359bd189ffdf3c95da799a1648dfe Mon Sep 17 00:00:00 2001 From: Nick Brown Date: Thu, 19 Oct 2023 11:50:40 -0700 Subject: [PATCH] code review comments --- .../BicepTemplateProcessor.cs | 13 +++++------- .../ArmTemplateProcessorTests.cs | 20 +++++++++++++++---- .../ArmTemplateProcessor.cs | 2 +- .../PlaceholderInputGenerator.cs | 4 ++++ 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs b/src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs index ff3599c3..7b83e6d3 100644 --- a/src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs +++ b/src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs @@ -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); @@ -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")) diff --git a/src/Analyzer.TemplateProcessor.UnitTests/ArmTemplateProcessorTests.cs b/src/Analyzer.TemplateProcessor.UnitTests/ArmTemplateProcessorTests.cs index 7aee4358..d906cfed 100644 --- a/src/Analyzer.TemplateProcessor.UnitTests/ArmTemplateProcessorTests.cs +++ b/src/Analyzer.TemplateProcessor.UnitTests/ArmTemplateProcessorTests.cs @@ -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; @@ -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#"", @@ -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( + () => armTemplateProcessor.ProcessTemplate(parametersJson, null, generateMissingParameters)); + } } private string GenerateTemplateWithOutputs(string outputValue) diff --git a/src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs b/src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs index a0474f45..c219b73a 100644 --- a/src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs +++ b/src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs @@ -549,7 +549,7 @@ internal InsensitiveDictionary 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()?.Properties() ?? Enumerable.Empty()) diff --git a/src/Analyzer.TemplateProcessor/PlaceholderInputGenerator.cs b/src/Analyzer.TemplateProcessor/PlaceholderInputGenerator.cs index 3651e1e0..8147b75c 100644 --- a/src/Analyzer.TemplateProcessor/PlaceholderInputGenerator.cs +++ b/src/Analyzer.TemplateProcessor/PlaceholderInputGenerator.cs @@ -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");