Skip to content

Commit

Permalink
extendable param files add a warning when referencing to a non-existe…
Browse files Browse the repository at this point in the history
…nt .bicepparam file (#15338)
  • Loading branch information
polatengin authored Dec 10, 2024
1 parent c730403 commit bf12c05
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/Bicep.Core.IntegrationTests/ParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
using System.Diagnostics.CodeAnalysis;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.FileSystem;
using Bicep.Core.IntegrationTests.Extensibility;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.FileSystem;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -401,6 +403,84 @@ param arrayParam array
""");
}

[TestMethod]
public void Valid_extends_should_not_fail()
{
var result = CompilationHelper.CompileParams(
("bicepconfig.json", @"
{
""experimentalFeaturesEnabled"": {
""extendableParamFiles"": true
}
}
"),("parameters.bicepparam", @"
using 'main.bicep'
extends 'shared.bicepparam'
"),
("shared.bicepparam", @"
using none
"),
("main.bicep", @"
"));

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

[TestMethod]
public void Invalid_extends_reference_does_not_exist_should_fail()
{
var result = CompilationHelper.CompileParams(
("bicepconfig.json", @"
{
""experimentalFeaturesEnabled"": {
""extendableParamFiles"": true
}
}
"),
("parameters.bicepparam", @"
using 'main.bicep'
extends 'does-not-exists.bicepparam'
param foo = ''
"),
("main.bicep", @"
param foo string
"));

var path = InMemoryFileResolver.GetFileUri("/path/to/does-not-exists.bicepparam").LocalPath;

result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP091", DiagnosticLevel.Error, $"An error occurred reading file. Could not find file '{path}'."),
});
}

[TestMethod]
public void Invalid_extends_reference_file_type_should_fail()
{
var result = CompilationHelper.CompileParams(
("bicepconfig.json", @"
{
""experimentalFeaturesEnabled"": {
""extendableParamFiles"": true
}
}
"),
("parameters.json", @"
{ ""foo"": ""bar"" }
"),
("parameters.bicepparam", @"
using 'main.bicep'
extends 'parameters.json'
param foo = ''
"),
("main.bicep", @"
param foo string
"));

result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP404", DiagnosticLevel.Error, "The \"extends\" declaration is missing a bicepparam file path reference"),
});
}

[TestMethod]
public void Invalid_extends_and_more_than_one_extends_should_fail()
{
Expand Down
15 changes: 13 additions & 2 deletions src/Bicep.Core/Emit/EmitLimitationCalculator.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 System.Diagnostics;
using Bicep.Core.DataFlow;
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.Intermediate;
using Bicep.Core.Navigation;
using Bicep.Core.Parsing;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
Expand Down Expand Up @@ -505,11 +507,20 @@ private static ImmutableDictionary<ParameterAssignmentSymbol, ParameterAssignmen

foreach (var extendsDeclaration in extendsDeclarations)
{
if (model.TryGetReferencedModel(extendsDeclaration).IsSuccess(out var extendedModel) &&
extendedModel is SemanticModel extendedSemanticModel)
var result = extendsDeclaration.TryGetReferencedModel(model.SourceFileGrouping, model.ModelLookup, b => b.ExtendsPathHasNotBeenSpecified());

if (result.IsSuccess(out var extendedModel, out var failure))
{
if (extendedModel is not SemanticModel extendedSemanticModel)
{
throw new UnreachableException("We have already verified this is a .bicepparam file");
}
generated.AddRange(extendedSemanticModel.EmitLimitationInfo.ParameterAssignments);
}
else
{
diagnostics.Write(failure);
}
}

var evaluator = new ParameterAssignmentEvaluator(model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static ResultWithDiagnostic<ISemanticModel> TryGetReferencedModel(
var isValidReference = (reference, sourceFile) switch
{
(ExtendsDeclarationSyntax, BicepParamFile) => true,
(ExtendsDeclarationSyntax, _) => false,
(_, BicepFile or ArmTemplateFile or TemplateSpecFile) => true,
_ => false,
};
Expand Down

0 comments on commit bf12c05

Please sign in to comment.