Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bicep.Cli.IntegrationTests/BuildParamsCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ param objParam object

var result = await Bicep(CreateDefaultSettings(), "build-params", childPath, "--stdout");
result.Should().Fail();
result.Stderr.Should().Contain("Error BCP338: Failed to evaluate parameter \"objParam\"");
result.Stderr.Should().Contain("Error BCP402: The spread operator \"...\" can only be used in this context for an expression assignable to type \"object\".");
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using 'main.bicep'
//@[18:23) ParameterAssignment three. Type: 'param three'. Declaration start char: 12, length: 27
//@[18:23) BaseParameters base. Type: baseParameters. Declaration start char: 12, length: 27

extends 'shared.bicepparam'

Expand Down
221 changes: 221 additions & 0 deletions src/Bicep.Core.UnitTests/Semantics/BaseParametersSymbolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Bicep.Core.Semantics;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bicep.Core.UnitTests.Semantics
{
[TestClass]
public class BaseParametersSymbolTests
{
[TestMethod]
public void FileSymbol_should_include_base_parameters_symbol_when_extends_is_present()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));

var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};

var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();

var baseSymbol = model.Root.Declarations.OfType<BaseParametersSymbol>().Single();

baseSymbol.Name.Should().Be(LanguageConstants.BaseIdentifier);
baseSymbol.ParentAssignments.Select(x => x.Name).Should().BeEquivalentTo(["three"]);
model.Root.Declarations.Should().Contain(baseSymbol);
}

[TestMethod]
public void FileSymbol_should_not_include_base_parameters_symbol_when_extends_is_absent()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));

var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
param one = 'param one'
param two = 'param two'
"""
};

var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();

model.Root.Declarations.OfType<BaseParametersSymbol>().Should().BeEmpty();
model.Root.Declarations.Should().NotContain(x => x.Name == LanguageConstants.BaseIdentifier);
}

[TestMethod]
public void Base_parameters_symbol_should_include_all_inherited_assignments()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));

var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = 'param four'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};

var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();

var baseSymbol = model.Root.Declarations.OfType<BaseParametersSymbol>().Single();

baseSymbol.ParentAssignments.Select(x => x.Name).Should().BeEquivalentTo(["three", "four"]);
}

[TestMethod]
public void Base_variable_access_should_have_object_type_with_read_only_parent_properties()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));

var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four string = ''
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = 'param four'
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
"""
};

var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();

var twoAssignment = model.Root.ParameterAssignments.Single(x => x.Name == "two");
var baseAccess = ((PropertyAccessSyntax)twoAssignment.DeclaringParameterAssignment.Value).BaseExpression
.Should().BeOfType<VariableAccessSyntax>().Subject;

var baseType = model.GetTypeInfo(baseAccess).Should().BeOfType<ObjectType>().Subject;

baseType.Properties.Should().ContainKeys("three", "four");
baseType.Properties["three"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
baseType.Properties["four"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
}

[TestMethod]
public void Base_variable_access_should_not_throw_when_inherited_params_include_object_and_array_values()
{
var services = new ServiceBuilder()
.WithEmptyAzResources()
.WithFeatureOverrides(new(ExtendableParamFilesEnabled: true));

var files = new Dictionary<Uri, string>
{
[new Uri("file:///main.bicep")] = """
param one string = ''
param two string = ''
param three string = ''
param four object = {
name: 'four'
value: 'four'
}
param five array = [
{
name: 'five'
value: 'five'
}
]
""",
[new Uri("file:///shared.bicepparam")] = """
using none
param three = 'param three'
param four = {
name: 'param four'
}
param five = [
{
name: 'param five'
}
]
""",
[new Uri("file:///main.bicepparam")] = """
using 'main.bicep'
extends 'shared.bicepparam'
param one = 'param one'
param two = base.three
param five = []
"""
};

var compilation = services.BuildCompilation(files, new Uri("file:///main.bicepparam"));
var model = compilation.GetEntrypointSemanticModel();

FluentActions.Invoking(() => model.GetAllDiagnostics().ToArray()).Should().NotThrow();

var baseAccess = ((PropertyAccessSyntax)model.Root.ParameterAssignments
.Single(x => x.Name == "two")
.DeclaringParameterAssignment
.Value).BaseExpression;

var baseType = model.GetTypeInfo(baseAccess).Should().BeOfType<ObjectType>().Subject;

baseType.Properties.Should().ContainKeys("three", "four", "five");
baseType.Properties["four"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
baseType.Properties["five"].Flags.Should().HaveFlag(TypePropertyFlags.ReadOnly);
}
}
}
8 changes: 8 additions & 0 deletions src/Bicep.Core/Semantics/FileSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public FileSymbol(
var outputDeclarations = ImmutableArray.CreateBuilder<OutputSymbol>();
var assertDeclarations = ImmutableArray.CreateBuilder<AssertSymbol>();
var parameterAssignments = ImmutableArray.CreateBuilder<ParameterAssignmentSymbol>();
var baseParametersDeclarations = ImmutableArray.CreateBuilder<BaseParametersSymbol>();
var testDeclarations = ImmutableArray.CreateBuilder<TestSymbol>();
var importedTypes = ImmutableArray.CreateBuilder<ImportedTypeSymbol>();
var importedVariables = ImmutableArray.CreateBuilder<ImportedVariableSymbol>();
Expand Down Expand Up @@ -95,6 +96,9 @@ public FileSymbol(
case ParameterAssignmentSymbol parameterAssignment:
parameterAssignments.Add(parameterAssignment);
break;
case BaseParametersSymbol baseParameters:
baseParametersDeclarations.Add(baseParameters);
break;
case TestSymbol test:
testDeclarations.Add(test);
break;
Expand Down Expand Up @@ -129,6 +133,7 @@ public FileSymbol(
OutputDeclarations = outputDeclarations.ToImmutable();
AssertDeclarations = assertDeclarations.ToImmutable();
ParameterAssignments = parameterAssignments.ToImmutable();
BaseParametersDeclarations = baseParametersDeclarations.ToImmutable();
TestDeclarations = testDeclarations.ToImmutable();
ImportedTypes = importedTypes.ToImmutable();
ImportedVariables = importedVariables.ToImmutable();
Expand Down Expand Up @@ -156,6 +161,7 @@ public FileSymbol(
.Concat(this.OutputDeclarations)
.Concat(this.AssertDeclarations)
.Concat(this.ParameterAssignments)
.Concat(this.BaseParametersDeclarations)
.Concat(this.TestDeclarations)
.Concat(this.ImportedTypes)
.Concat(this.ImportedVariables)
Expand Down Expand Up @@ -205,6 +211,8 @@ public FileSymbol(

public ImmutableArray<ParameterAssignmentSymbol> ParameterAssignments { get; }

public ImmutableArray<BaseParametersSymbol> BaseParametersDeclarations { get; }

public ImmutableArray<ExtensionConfigAssignmentSymbol> ExtensionConfigAssignments { get; }

public ImmutableArray<ImportedTypeSymbol> ImportedTypes { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/TypeSystem/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private TypeSymbol GetSyntheticBaseParametersType(ImplicitBaseIdentifierSyntax s
var namedProperties = baseSymbol.ParentAssignments
.GroupBy(pa => pa.Name, LanguageConstants.IdentifierComparer)
.Select(group => group.First())
.Select(pa => new NamedTypeProperty(pa.Name, GetTypeInfo(pa.DeclaringParameterAssignment.Value), TypePropertyFlags.ReadOnly));
.Select(pa => new NamedTypeProperty(pa.Name, pa.Type, TypePropertyFlags.ReadOnly));

return new ObjectType(
name: "baseParameters",
Expand Down
Loading
Loading