Skip to content

Commit 4e3160c

Browse files
Add validation for Bicep diagnostics
1 parent 44dd9f3 commit 4e3160c

6 files changed

Lines changed: 623 additions & 0 deletions

File tree

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.201" PrivateAssets="All"/>
99
</ItemGroup>
1010
<ItemGroup>
11+
<PackageVersion Include="Azure.Bicep.RpcClient" Version="0.41.2" />
1112
<PackageVersion Include="Azure.Bicep.Types" Version="0.6.50" />
1213
<PackageVersion Include="Azure.Bicep.Types.Az" Version="0.2.813" />
1314
<PackageVersion Include="Azure.Deployments.Testing.Utilities" Version="1.527.0" />
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Azure.Deployments.Testing.Utilities;
11+
using Microsoft.VisualStudio.TestTools.UnitTesting;
12+
using Bicep.RpcClient;
13+
using FluentAssertions;
14+
using Bicep.RpcClient.Models;
15+
using System.Diagnostics.CodeAnalysis;
16+
using System.Text.Json.Serialization;
17+
using System.Text.Json;
18+
using System.Text.Encodings.Web;
19+
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
20+
21+
namespace TemplateRefGenerator.Tests;
22+
23+
[TestClass]
24+
public class BicepCompilationTests
25+
{
26+
[NotNull]
27+
public TestContext? TestContext { get; set; }
28+
29+
[TestMethod]
30+
[TestCategory(BaselineHelper.BaselineTestCategory)]
31+
[EmbeddedFilesTestData(@"^Files/compilation/bicep-diagnostics.md$")]
32+
public async Task All_bicep_samples_are_valid(EmbeddedFile embeddedFile)
33+
{
34+
// This test verifies there are no errors, and that the warnings match those that are expected
35+
var baselineFolder = BaselineFolder.BuildOutputFolder(TestContext, embeddedFile);
36+
var resultsFile = baselineFolder.GetBaselineFile(embeddedFile.FileName);
37+
RemarksLoader remarksLoader = new();
38+
39+
BicepClientFactory bicepClientFactory = new(new());
40+
using var bicep = await bicepClientFactory.DownloadAndInitialize(new()
41+
{
42+
BicepVersion = "0.41.2"
43+
}, CancellationToken.None);
44+
45+
var markdownOutput = """
46+
# Bicep Sample Diagnostics
47+
48+
""";
49+
50+
var providers = remarksLoader.GetProviderNamespacesWithRemarks();
51+
var tempPath = Path.GetTempPath();
52+
Directory.CreateDirectory(tempPath);
53+
var tempFile = Path.Combine(tempPath, Guid.NewGuid() + ".bicep");
54+
foreach (var provider in providers.OrderBy(x => x, StringComparer.OrdinalIgnoreCase))
55+
{
56+
var bicepSamples = remarksLoader.GetRemarks(provider).BicepSamples ?? [];
57+
58+
foreach (var sample in bicepSamples.OrderBy(x => x.Path, StringComparer.OrdinalIgnoreCase))
59+
{
60+
var bicepContents = remarksLoader.GetCodeSample(provider, sample);
61+
File.WriteAllText(tempFile, bicepContents);
62+
63+
var result = await bicep.Compile(new(tempFile));
64+
65+
result.Diagnostics.Count(x => string.Equals(x.Level, "Error", StringComparison.OrdinalIgnoreCase)).Should().Be(0, because: $"Bicep sample '{sample.Path}' should not have compilation errors");
66+
67+
if (result.Diagnostics.Any())
68+
{
69+
markdownOutput += $"""
70+
## {sample.Path}
71+
72+
""";
73+
foreach (var diag in result.Diagnostics.OrderBy(x => x.Range.Start.Line).ThenBy(x => x.Range.Start.Char))
74+
{
75+
markdownOutput += $"""
76+
* [[{diag.Code}] {diag.Level}: {diag.Message}](../../../../settings/remarks/{provider}/{sample.Path}#L{diag.Range.Start.Line + 1}-L{diag.Range.End.Line + 1})
77+
78+
""";
79+
}
80+
}
81+
}
82+
}
83+
84+
resultsFile.WriteToOutputFolder(markdownOutput);
85+
resultsFile.ShouldHaveExpectedValue();
86+
}
87+
}

0 commit comments

Comments
 (0)