Skip to content

Commit 6b8774a

Browse files
Update to latest Bicep Core libraries (#392)
- Updating Bicep libraries current stable versions. - Conforming to breaking changes introduced in the latest version. - Updating other utility libraries - Fixing some data tests (newer MSTest version was failing for some of them)
1 parent bfb0120 commit 6b8774a

File tree

20 files changed

+119
-85
lines changed

20 files changed

+119
-85
lines changed

src/Analyzer.BicepProcessor.UnitTests/Analyzer.BicepProcessor.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<PackageReference Include="Microsoft.NET.Test.Sdk" />
99
<PackageReference Include="MSTest.TestAdapter" />
1010
<PackageReference Include="MSTest.TestFramework" />
11-
<PackageReference Include="coverlet.collector" />
11+
<PackageReference Include="coverlet.collector">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
1215
<PackageReference Include="System.IO.Abstractions" />
1316
<PackageReference Include="Sarif.Sdk" />
1417
</ItemGroup>

src/Analyzer.BicepProcessor/Analyzer.BicepProcessor.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010
<ItemGroup>
1111
<PackageReference Include="Azure.Bicep.Core" />
1212
<PackageReference Include="Newtonsoft.Json" />
13-
<PackageReference Include="System.IO.Abstractions" />
1413
</ItemGroup>
1514
</Project>

src/Analyzer.BicepProcessor/BicepTemplateProcessor.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.IO.Abstractions;
78
using System.Linq;
@@ -15,14 +16,18 @@
1516
using Bicep.Core.Extensions;
1617
using Bicep.Core.Features;
1718
using Bicep.Core.FileSystem;
19+
using Bicep.Core.Navigation;
1820
using Bicep.Core.Registry;
1921
using Bicep.Core.Registry.Auth;
22+
using Bicep.Core.Registry.PublicRegistry;
2023
using Bicep.Core.Semantics.Namespaces;
2124
using Bicep.Core.Syntax;
2225
using Bicep.Core.Text;
2326
using Bicep.Core.TypeSystem.Providers;
2427
using Bicep.Core.Utils;
2528
using Bicep.Core.Workspaces;
29+
using Bicep.IO.Abstraction;
30+
using Bicep.IO.FileSystem;
2631
using Microsoft.Extensions.DependencyInjection;
2732
using BicepEnvironment = Bicep.Core.Utils.Environment;
2833
using IOFileSystem = System.IO.Abstractions.FileSystem;
@@ -41,14 +46,16 @@ public static class BicepTemplateProcessor
4146
/// <param name="services"></param>
4247
/// <returns></returns>
4348
public static IServiceCollection AddBicepCore(this IServiceCollection services) => services
44-
.AddSingleton<INamespaceProvider, DefaultNamespaceProvider>()
49+
.AddSingleton<INamespaceProvider, NamespaceProvider>()
4550
.AddSingleton<IResourceTypeProviderFactory, ResourceTypeProviderFactory>()
4651
.AddSingleton<IContainerRegistryClientFactory, ContainerRegistryClientFactory>()
52+
.AddSingleton<IPublicRegistryModuleMetadataProvider, PublicRegistryModuleMetadataProvider>()
4753
.AddSingleton<ITemplateSpecRepositoryFactory, TemplateSpecRepositoryFactory>()
4854
.AddSingleton<IModuleDispatcher, ModuleDispatcher>()
4955
.AddSingleton<IArtifactRegistryProvider, DefaultArtifactRegistryProvider>()
5056
.AddSingleton<ITokenCredentialFactory, TokenCredentialFactory>()
5157
.AddSingleton<IFileResolver, FileResolver>()
58+
.AddSingleton<IFileExplorer, FileSystemFileExplorer>()
5259
.AddSingleton<IEnvironment, BicepEnvironment>()
5360
.AddSingleton<IFileSystem, IOFileSystem>()
5461
.AddSingleton<IConfigurationManager, ConfigurationManager>()
@@ -91,47 +98,49 @@ public static (string, BicepMetadata) ConvertBicepToJson(string bicepPath)
9198
throw new Exception($"Bicep issues found:{SysEnvironment.NewLine}{string.Join(SysEnvironment.NewLine, bicepIssues)}");
9299
}
93100

94-
string GetPathRelativeToEntryPoint(string absolutePath) => Path.GetRelativePath(
95-
Path.GetDirectoryName(compilation.SourceFileGrouping.EntryPoint.FileUri.AbsolutePath), absolutePath);
96-
97-
// Collect all needed module info from SourceFileGrouping metadata
98-
var moduleInfo = compilation.SourceFileGrouping.FileUriResultByArtifactReference.Select(sourceFileAndMetadata =>
99-
{
100-
var bicepSourceFile = sourceFileAndMetadata.Key as BicepSourceFile;
101-
var pathRelativeToEntryPoint = GetPathRelativeToEntryPoint(bicepSourceFile.FileUri.AbsolutePath);
102-
var modules = sourceFileAndMetadata.Value
103-
.Select(artifactRefAndUriResult =>
101+
string entryPointDirectory = Path.GetDirectoryName(compilation.SourceFileGrouping.EntryPoint.Uri.AbsolutePath);
102+
103+
bool IsResolvedLocalModuleReference(KeyValuePair<IArtifactReferenceSyntax, ArtifactResolutionInfo> artifact) =>
104+
// Only include local module references (not modules imported from public/private registries, i.e. those that match IsModuleRegistryPathRegex),
105+
// as it is more useful for user to see line number of the module declaration itself,
106+
// rather than the line number in the module (as the user does not control the template in the registry directly).
107+
artifact.Key is ModuleDeclarationSyntax moduleDeclaration &&
108+
moduleDeclaration.Path is StringSyntax moduleDeclarationPath &&
109+
!moduleDeclarationPath.SegmentValues.Any(IsModuleRegistryPathRegex.IsMatch) &&
110+
artifact.Value.Result.IsSuccess();
111+
112+
// Create SourceFileModuleInfo collection by gathering all needed module info from SourceFileGrouping metadata.
113+
// Group by the source file path to allow for easy construction of SourceFileModuleInfo.
114+
var moduleInfo = compilation.SourceFileGrouping.ArtifactLookup
115+
.Where(IsResolvedLocalModuleReference)
116+
.GroupBy(artifact => artifact.Value.Origin)
117+
.Select(grouping =>
118+
{
119+
var bicepSourceFile = grouping.Key;
120+
var pathRelativeToEntryPoint = Path.GetRelativePath(
121+
Path.GetDirectoryName(compilation.SourceFileGrouping.EntryPoint.Uri.AbsolutePath), bicepSourceFile.Uri.AbsolutePath);
122+
123+
// Use the grouping value (KeyValuePair<IArtifactReferenceSyntax,ArtifactResolutionInfo>) to create
124+
// a dictionary of module line numbers to file paths.
125+
// This represents the modules in the source file, and where (what lines) they are referenced.
126+
var modules = grouping.Select(artifactRefAndUriResult =>
104127
{
105-
// Do not include modules imported from public/private registries, as it is more useful for user to see line number
106-
// of the module declaration itself instead of line number in the module as the user does not control template in registry directly
107-
if (artifactRefAndUriResult.Key is not ModuleDeclarationSyntax moduleDeclaration
108-
|| moduleDeclaration.Path is not StringSyntax moduleDeclarationPath
109-
|| moduleDeclarationPath.SegmentValues.Any(v => IsModuleRegistryPathRegex.IsMatch(v)))
110-
{
111-
return null;
112-
}
113-
114-
if (!artifactRefAndUriResult.Value.IsSuccess())
115-
{
116-
return null;
117-
}
118-
119-
var moduleLine = TextCoordinateConverter.GetPosition(bicepSourceFile.LineStarts, moduleDeclaration.Span.Position).line;
120-
var modulePath = new FileInfo(artifactRefAndUriResult.Value.Unwrap().AbsolutePath).FullName; // converts path to current platform
128+
var module = artifactRefAndUriResult.Key as ModuleDeclarationSyntax;
129+
var moduleLine = TextCoordinateConverter.GetPosition(bicepSourceFile.LineStarts, module.Span.Position).line;
130+
var modulePath = new FileInfo(artifactRefAndUriResult.Value.Result.Unwrap().AbsolutePath).FullName; // converts path to current platform
121131

122132
// Use relative paths for bicep to match file paths used in bicep modules and source map
123133
if (modulePath.EndsWith(".bicep"))
124134
{
125-
modulePath = GetPathRelativeToEntryPoint(modulePath);
135+
modulePath = Path.GetRelativePath(entryPointDirectory, modulePath);
126136
}
127137

128138
return new { moduleLine, modulePath };
129139
})
130-
.WhereNotNull()
131140
.ToDictionary(c => c.moduleLine, c => c.modulePath);
132141

133-
return new SourceFileModuleInfo(pathRelativeToEntryPoint, modules);
134-
});
142+
return new SourceFileModuleInfo(pathRelativeToEntryPoint, modules);
143+
});
135144

136145
var bicepMetadata = new BicepMetadata()
137146
{

src/Analyzer.BicepProcessor/SourceMapFeatureProvider.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Azure.ResourceManager.Resources;
33
using Bicep.Core.Features;
4+
using Bicep.IO.Abstraction;
45

56
namespace Microsoft.Azure.Templates.Analyzer.BicepProcessor
67
{
@@ -39,7 +40,7 @@ public SourceMapFeatureProvider(IFeatureProvider features)
3940
public string AssemblyVersion => features.AssemblyVersion;
4041

4142
/// <inheritdoc/>
42-
public string CacheRootDirectory => features.CacheRootDirectory;
43+
public IDirectoryHandle CacheRootDirectory => features.CacheRootDirectory;
4344

4445
/// <inheritdoc/>
4546
public bool SymbolicNameCodegenEnabled => features.SymbolicNameCodegenEnabled;
@@ -54,33 +55,33 @@ public SourceMapFeatureProvider(IFeatureProvider features)
5455
public bool SourceMappingEnabled => true;
5556

5657
/// <inheritdoc/>
57-
public bool UserDefinedFunctionsEnabled => features.UserDefinedFunctionsEnabled;
58+
public bool TestFrameworkEnabled => features.TestFrameworkEnabled;
5859

5960
/// <inheritdoc/>
60-
public bool DynamicTypeLoadingEnabled => features.DynamicTypeLoadingEnabled;
61+
public bool AssertsEnabled => features.AssertsEnabled;
6162

6263
/// <inheritdoc/>
63-
public bool PrettyPrintingEnabled => features.PrettyPrintingEnabled;
64+
public bool OptionalModuleNamesEnabled => features.OptionalModuleNamesEnabled;
6465

6566
/// <inheritdoc/>
66-
public bool TestFrameworkEnabled => features.TestFrameworkEnabled;
67+
public bool ResourceDerivedTypesEnabled => features.ResourceDerivedTypesEnabled;
6768

6869
/// <inheritdoc/>
69-
public bool AssertsEnabled => features.AssertsEnabled;
70+
public bool LegacyFormatterEnabled => features.LegacyFormatterEnabled;
7071

7172
/// <inheritdoc/>
72-
public bool MicrosoftGraphPreviewEnabled => features.MicrosoftGraphPreviewEnabled;
73+
public bool LocalDeployEnabled => features.LocalDeployEnabled;
7374

7475
/// <inheritdoc/>
75-
public bool PublishSourceEnabled => features.PublishSourceEnabled;
76+
public bool ExtendableParamFilesEnabled => features.ExtendableParamFilesEnabled;
7677

7778
/// <inheritdoc/>
78-
public bool ProviderRegistryEnabled => features.ProviderRegistryEnabled;
79+
public bool SecureOutputsEnabled => features.SecureOutputsEnabled;
7980

8081
/// <inheritdoc/>
81-
public bool OptionalModuleNamesEnabled => features.OptionalModuleNamesEnabled;
82+
public bool ResourceInfoCodegenEnabled => features.ResourceInfoCodegenEnabled;
8283

8384
/// <inheritdoc/>
84-
public bool ResourceDerivedTypesEnabled => features.ResourceDerivedTypesEnabled;
85+
public bool ExtensibilityV2EmittingEnabled => features.ExtensibilityV2EmittingEnabled;
8586
}
8687
}

src/Analyzer.Core.BuiltInRuleTests/Analyzer.Core.BuiltInRuleTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
<PackageReference Include="Microsoft.NET.Test.Sdk" />
88
<PackageReference Include="MSTest.TestAdapter" />
99
<PackageReference Include="MSTest.TestFramework" />
10-
<PackageReference Include="coverlet.collector" />
10+
<PackageReference Include="coverlet.collector">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
1114
</ItemGroup>
1215
<ItemGroup>
1316
<ProjectReference Include="..\Analyzer.Core\Analyzer.Core.csproj" />

src/Analyzer.Core.UnitTests/Analyzer.Core.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<PackageReference Include="Microsoft.NET.Test.Sdk" />
99
<PackageReference Include="MSTest.TestAdapter" />
1010
<PackageReference Include="MSTest.TestFramework" />
11-
<PackageReference Include="coverlet.collector" />
11+
<PackageReference Include="coverlet.collector">
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
</PackageReference>
1215
</ItemGroup>
1316
<ItemGroup>
1417
<ProjectReference Include="..\Analyzer.Core\Analyzer.Core.csproj" />

src/Analyzer.JsonRuleEngine.FunctionalTests/Analyzer.JsonRuleEngine.FunctionalTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<PackageReference Include="Microsoft.NET.Test.Sdk" />
1010
<PackageReference Include="MSTest.TestAdapter" />
1111
<PackageReference Include="MSTest.TestFramework" />
12-
<PackageReference Include="coverlet.collector" />
12+
<PackageReference Include="coverlet.collector">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
1316
</ItemGroup>
1417
<ItemGroup>
1518
<ProjectReference Include="..\Analyzer.JsonRuleEngine\Analyzer.JsonRuleEngine.csproj" />

src/Analyzer.JsonRuleEngine.UnitTests/Analyzer.JsonRuleEngine.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
<PackageReference Include="Moq" />
1111
<PackageReference Include="MSTest.TestAdapter" />
1212
<PackageReference Include="MSTest.TestFramework" />
13-
<PackageReference Include="coverlet.collector" />
13+
<PackageReference Include="coverlet.collector">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
1417
</ItemGroup>
1518
<ItemGroup>
1619
<ProjectReference Include="..\Analyzer.JsonRuleEngine\Analyzer.JsonRuleEngine.csproj" />

src/Analyzer.JsonRuleEngine.UnitTests/ExistsOperatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ExistsOperatorTests
1515
[DataRow(true, DisplayName = "Property value is a boolean")]
1616
[DataRow(1, DisplayName = "Property value is an integer")]
1717
[DataRow(0.1, DisplayName = "Property value is a float")]
18-
[DataRow(new object[] { }, DisplayName = "Property value is an array")]
18+
[DataRow((object)(new object[] { }), DisplayName = "Property value is an array")]
1919
[DataRow(null, DisplayName = "Property value is null")]
2020
public void EvaluateExpression_PropertyExists_ExistsExpressionIsTrue(object jTokenValue)
2121
{

src/Analyzer.JsonRuleEngine.UnitTests/ExpressionConverterTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ void ValidateExpression(ExpressionDefinition expression, Type expectedSpecificTy
214214
}
215215

216216
[DataTestMethod]
217-
[DataRow(DisplayName = "Not Expression")]
218-
[DataRow("not", typeof(NotExpressionDefinition), DisplayName = "Not Expression with Where condition")]
219-
public void ReadJson_ValidNotExpression_ReturnsCorrectTypeAndValues(params object[] whereCondition)
217+
[DataRow(new object[] { }, DisplayName = "Not Expression")]
218+
[DataRow(new object[] { "not", typeof(NotExpressionDefinition) }, DisplayName = "Not Expression with Where condition")]
219+
public void ReadJson_ValidNotExpression_ReturnsCorrectTypeAndValues(object[] whereCondition)
220220
{
221221
var expressionTemplate = $@"
222222
{{
@@ -313,10 +313,10 @@ public void ReadJson_StructuredExpressionWithInvalidExpression_ThrowsParsingExce
313313
}
314314

315315
[TestMethod]
316-
[DataRow(DisplayName = "No operators")]
317-
[DataRow("hasValue", true, "exists", true, DisplayName = "HasValue and Exists")]
316+
[DataRow(new object[] { }, DisplayName = "No operators")]
317+
[DataRow(["hasValue", true, "exists", true], DisplayName = "HasValue and Exists")]
318318
[ExpectedException(typeof(JsonSerializationException))]
319-
public void ReadJson_LeafWithInvalidOperatorCount_ThrowsParsingException(params object[] operators)
319+
public void ReadJson_LeafWithInvalidOperatorCount_ThrowsParsingException(object[] operators)
320320
{
321321
var leafDefinition = "{\"resourceType\": \"resource\", \"path\": \"path\"";
322322

src/Analyzer.JsonRuleEngine.UnitTests/HasValueOperatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class HasValueOperatorTests
1414
[DataRow(true, DisplayName = "Property value is a boolean")]
1515
[DataRow(1, DisplayName = "Property value is an integer")]
1616
[DataRow(0.1, DisplayName = "Property value is a float")]
17-
[DataRow(new object[] { }, DisplayName = "Property value is an array")]
17+
[DataRow((object)(new object[] { }), DisplayName = "Property value is an array")]
1818
public void EvaluateExpression_PropertyHasValue_HasValueIsTrue(object jTokenValue)
1919
{
2020
var jToken = TestUtilities.ToJToken(jTokenValue);

src/Analyzer.PowerShellRuleEngine.UnitTests/Analyzer.PowerShellRuleEngine.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
<PackageReference Include="Microsoft.NET.Test.Sdk" />
88
<PackageReference Include="MSTest.TestAdapter" />
99
<PackageReference Include="MSTest.TestFramework" />
10-
<PackageReference Include="coverlet.collector" />
10+
<PackageReference Include="coverlet.collector">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
1114
<PackageReference Include="Sarif.Sdk" />
1215
<PackageReference Include="System.IO.Abstractions" />
1316
</ItemGroup>

src/Analyzer.PowerShellRuleEngine.UnitTests/PowerShellRuleEngineTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void AssemblyInitialize(TestContext context)
4949
[DataRow("template_and_resource_level_results.json", true, 14, new int[] { 1, 1, 1, 1, 8, 11, 14, 17, 1, 17, 17, 1, 17, 1 }, DisplayName = "Running all the rules against a template with errors reported in both analysis stages")]
5050
[DataRow("template_and_resource_level_results.json", false, 5, new int[] { 11, 17, 17, 17, 17 }, DisplayName = "Running only the security rules against a template with errors reported in both analysis stages")]
5151
// TODO add test case for error, warning (rule with severity level of warning?) and informational (also rule with that severity level?)
52-
public void AnalyzeTemplate_ValidTemplate_ReturnsExpectedEvaluations(string templateFileName, bool runsAllRules, int expectedErrorCount, dynamic expectedLineNumbers)
52+
public void AnalyzeTemplate_ValidTemplate_ReturnsExpectedEvaluations(string templateFileName, bool runsAllRules, int expectedErrorCount, int[] expectedLineNumbers)
5353
{
5454
Assert.AreEqual(expectedErrorCount, expectedLineNumbers.Length);
5555

@@ -97,7 +97,7 @@ public void AnalyzeTemplate_ValidTemplate_ReturnsExpectedEvaluations(string temp
9797
Assert.AreEqual(expectedErrorCount, failedEvaluations.Count);
9898

9999
// PSRule evaluations can change order depending on the OS:
100-
foreach (var expectedLineNumber in expectedLineNumbers)
100+
foreach (int expectedLineNumber in expectedLineNumbers)
101101
{
102102
var matchingEvaluation = failedEvaluations.Find(evaluation => evaluation.Result.SourceLocation.LineNumber == expectedLineNumber);
103103
failedEvaluations.Remove(matchingEvaluation);

src/Analyzer.Reports.UnitTests/Analyzer.Reports.UnitTests.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
<PackageReference Include="Moq" />
1717
<PackageReference Include="MSTest.TestAdapter" />
1818
<PackageReference Include="MSTest.TestFramework" />
19-
<PackageReference Include="coverlet.collector" />
19+
<PackageReference Include="coverlet.collector">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
22+
</PackageReference>
2023
</ItemGroup>
2124
<ItemGroup>
2225
<ProjectReference Include="..\Analyzer.Core\Analyzer.Core.csproj" />

0 commit comments

Comments
 (0)