Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for various issues noticed with provider syntax #13542

Merged
merged 2 commits into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Bicep.Core.IntegrationTests/ExamplesTests.cs
Expand Up @@ -39,7 +39,7 @@ private async Task RunExampleTest(EmbeddedFile embeddedBicep, FeatureProviderOve
{
DiagnosticAssertions.DoWithDiagnosticAnnotations(
file,
diagnostics.Where(d => !IsPermittedMissingTypeDiagnostic(d)).Where(d => !d.Code.Equals("BCP395")),
diagnostics.Where(d => !IsPermittedMissingTypeDiagnostic(d)),
diagnostics =>
{
diagnostics.Should().BeEmpty("{0} should not have warnings or errors", file.FileUri.LocalPath);
Expand Down
121 changes: 79 additions & 42 deletions src/Bicep.Core.IntegrationTests/ExtensibilityTests.cs

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/Bicep.Core.IntegrationTests/OutputsTests.cs
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.IntegrationTests.Extensibility;
using Bicep.Core.TypeSystem.Types;
Expand All @@ -25,6 +26,15 @@ public class OutputsTests

private ServiceBuilder ServicesWithExtensibility => new ServiceBuilder()
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true, ResourceTypedParamsAndOutputsEnabled: true))
.WithConfigurationPatch(c => c.WithProvidersConfiguration("""
{
"az": "builtin:",
"kubernetes": "builtin:",
"microsoftGraph": "builtin:",
"foo": "builtin:",
"bar": "builtin:"
}
"""))
.WithNamespaceProvider(new TestExtensibilityNamespaceProvider(BicepTestConstants.ResourceTypeProviderFactory));

[TestMethod]
Expand Down Expand Up @@ -225,7 +235,7 @@ public void Output_cannot_use_extensibility_resource_type()
{
var result = CompilationHelper.Compile(ServicesWithExtensibility,
"""
provider 'bar@1.0.0' with {
provider bar with {
connectionString: 'asdf'
} as stg

Expand All @@ -236,7 +246,7 @@ public void Output_cannot_use_extensibility_resource_type()
output out resource = container
""");

result.ExcludingLinterDiagnostics().ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[]
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP227", DiagnosticLevel.Error, "The type \"container\" cannot be used as a parameter or output type. Extensibility types are currently not supported as parameters or outputs."),
});
Expand Down
14 changes: 12 additions & 2 deletions src/Bicep.Core.IntegrationTests/ParametersTests.cs
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.IntegrationTests.Extensibility;
using Bicep.Core.Syntax;
Expand All @@ -24,6 +25,15 @@ public class ParameterTests

private ServiceBuilder ServicesWithExtensibility => new ServiceBuilder()
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true, ResourceTypedParamsAndOutputsEnabled: true))
.WithConfigurationPatch(c => c.WithProvidersConfiguration("""
{
"az": "builtin:",
"kubernetes": "builtin:",
"microsoftGraph": "builtin:",
"foo": "builtin:",
"bar": "builtin:"
}
"""))
.WithNamespaceProvider(new TestExtensibilityNamespaceProvider(BicepTestConstants.ResourceTypeProviderFactory));

[TestMethod]
Expand Down Expand Up @@ -167,14 +177,14 @@ public void Parameter_can_have_warnings_for_missing_type()
public void Parameter_cannot_use_extensibility_resource_type()
{
var result = CompilationHelper.Compile(ServicesWithExtensibility, """
provider 'bar@1.0.0' with {
provider bar with {
connectionString: 'asdf'
} as stg

param container resource 'stg:container'
output name string = container.name // silence unused params warning
""");
result.ExcludingLinterDiagnostics().ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[]
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[]
{
("BCP227", DiagnosticLevel.Error, "The type \"container\" cannot be used as a parameter or output type. Extensibility types are currently not supported as parameters or outputs."),
("BCP062", DiagnosticLevel.Error, "The referenced declaration with name \"container\" is not valid."),
Expand Down
73 changes: 36 additions & 37 deletions src/Bicep.Core.IntegrationTests/ProviderImportTests.cs
Expand Up @@ -72,13 +72,13 @@ public ResultWithDiagnostic<NamespaceType> TryGetNamespace(ResourceTypesProvider
}

[TestMethod]
public async Task Imports_are_disabled_unless_feature_is_enabled()
public async Task Providers_are_disabled_unless_feature_is_enabled()
{
var services = new ServiceBuilder();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'az@1.0.0'
provider az
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP203", DiagnosticLevel.Error, "Using provider statements requires enabling EXPERIMENTAL feature \"Extensibility\"."),
// BCP084 is raised because BCP203 prevented the compiler from binding a namespace to the `az` symbol (an ErrorType was bound instead).
("BCP084", DiagnosticLevel.Error, "The symbolic name \"az\" is reserved. Please use a different symbolic name. Reserved namespaces are \"az\", \"sys\"."),
Expand Down Expand Up @@ -106,9 +106,9 @@ public async Task Provider_Statement_With_Invalid_Keyword_Should_Emit_Diagnostic
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'sys@1.0.0' blahblah
provider sys blahblah
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP305", DiagnosticLevel.Error, "Expected the \"with\" keyword, \"as\" keyword, or a new line character at this location."),
});
}
Expand All @@ -118,9 +118,9 @@ public async Task Provider_Statement_Without_Brace_Should_Raise_Error()
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'kubernetes@1.0.0' with
provider kubernetes with
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP018", DiagnosticLevel.Error, "Expected the \"{\" character at this location."),
});
}
Expand All @@ -130,12 +130,12 @@ public async Task Provider_Statement_Without_As_Keyword_Should_Raise_Error()
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'kubernetes@1.0.0' with {
provider kubernetes with {
kubeConfig: 'foo'
namespace: 'bar'
} something
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP012", DiagnosticLevel.Error, "Expected the \"as\" keyword at this location."),
});
}
Expand All @@ -145,12 +145,12 @@ public async Task Provider_Statement_Without_Alias_Name_Should_Raise_Error()
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'kubernetes@1.0.0' with {
provider kubernetes with {
kubeConfig: 'foo'
namespace: 'bar'
} as
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP202", DiagnosticLevel.Error, "Expected a provider alias name at this location."),
});
}
Expand All @@ -160,9 +160,9 @@ public async Task Provider_Statement_Without_Alias_Name_For_Sys_Should_Raise_Err
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, """
provider 'sys@1.0.0' as
provider sys as
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP202", DiagnosticLevel.Error, "Expected a provider alias name at this location."),
});
}
Expand All @@ -173,18 +173,17 @@ public async Task Using_import_instead_of_provider_raises_warning()
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
import 'az@1.0.0' as foo
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP381", DiagnosticLevel.Warning, "Declaring provider namespaces with the \"import\" keyword has been deprecated. Please use the \"provider\" keyword instead."),
("BCP395", DiagnosticLevel.Warning, "Declaring provider namespaces using the '<providerName>@<version>' expression has been deprecated. Please use an identifier instead."),
});
}

[DataRow("az")]
[DataRow("sys")]
[TestMethod]
public async Task Using_legacy_import_syntax_raises_warning_for_az_provider(string providerName)
public async Task Using_legacy_import_syntax_raises_warning_for_az_provider()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), $"""
provider '{providerName}@1.0.0' as {providerName}
provider 'az@1.0.0' as az
""");

result.Should().HaveDiagnostics(new[] {
Expand All @@ -196,11 +195,11 @@ public async Task Using_legacy_import_syntax_raises_warning_for_az_provider(stri
public async Task Import_configuration_is_blocked_by_default()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0' with {
provider az with {
foo: 'bar'
}
""");
result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP205", DiagnosticLevel.Error, "Provider namespace \"az\" does not support configuration."),
});
}
Expand All @@ -220,7 +219,7 @@ public async Task Imports_return_error_with_unrecognized_namespace()
public async Task Using_import_statements_frees_up_the_namespace_symbol()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0' as newAz
provider az as newAz

var az = 'Fake AZ!'
var myRg = newAz.resourceGroup()
Expand All @@ -229,15 +228,15 @@ public async Task Using_import_statements_frees_up_the_namespace_symbol()
output rgLocation string = myRg.location
""");

result.ExcludingDiagnostics("BCP395").Should().NotHaveAnyDiagnostics();
result.Should().NotHaveAnyDiagnostics();
}

[TestMethod]
public async Task You_can_swap_imported_namespaces_if_you_really_really_want_to()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0' as sys
provider 'sys@1.0.0' as az
provider az as sys
provider sys as az

var myRg = sys.resourceGroup()

Expand All @@ -246,15 +245,15 @@ public async Task You_can_swap_imported_namespaces_if_you_really_really_want_to(
""");

result.Should().GenerateATemplate();
result.ExcludingDiagnostics("BCP395").Should().NotHaveAnyDiagnostics();
result.Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("$.outputs.rgLocation.metadata.description", "why on earth would you do this?");
}

[TestMethod]
public async Task Overwriting_single_built_in_namespace_with_import_is_prohibited()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0' as sys
provider az as sys

var myRg = sys.resourceGroup()

Expand All @@ -268,14 +267,14 @@ public async Task Overwriting_single_built_in_namespace_with_import_is_prohibite
public async Task Singleton_imports_cannot_be_used_multiple_times()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0' as az1
provider 'az@1.0.0' as az2
provider az as az1
provider az as az2

provider 'sys@1.0.0' as sys1
provider 'sys@1.0.0' as sys2
provider sys as sys1
provider sys as sys2
""");

result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP207", DiagnosticLevel.Error, "Namespace \"az\" is declared multiple times. Remove the duplicates."),
("BCP207", DiagnosticLevel.Error, "Namespace \"az\" is declared multiple times. Remove the duplicates."),
("BCP207", DiagnosticLevel.Error, "Namespace \"sys\" is declared multiple times. Remove the duplicates."),
Expand All @@ -287,14 +286,14 @@ public async Task Singleton_imports_cannot_be_used_multiple_times()
public async Task Import_names_must_not_conflict_with_other_symbols()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), """
provider 'az@1.0.0'
provider 'kubernetes@1.0.0' with {
provider az
provider kubernetes with {
kubeConfig: ''
namespace: ''
} as az
""");

result.ExcludingDiagnostics("BCP395").Should().HaveDiagnostics(new[] {
result.Should().HaveDiagnostics(new[] {
("BCP028", DiagnosticLevel.Error, "Identifier \"az\" is declared multiple times. Remove or rename the duplicates."),
("BCP028", DiagnosticLevel.Error, "Identifier \"az\" is declared multiple times. Remove or rename the duplicates."),
});
Expand Down Expand Up @@ -409,7 +408,7 @@ public async Task Config_with_optional_properties_can_be_skipped()
[TestMethod]
public async Task MicrosoftGraph_imports_succeed_with_preview_feature_enabled()
{
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), @"provider 'microsoftGraph@1.0.0' as graph");
var result = await CompilationHelper.RestoreAndCompile(await GetServices(), @"provider microsoftGraph as graph");

result.Should().HaveDiagnostics(new[] {
("BCP204", DiagnosticLevel.Error, "Provider namespace \"microsoftGraph\" is not recognized."),
Expand All @@ -418,9 +417,9 @@ public async Task MicrosoftGraph_imports_succeed_with_preview_feature_enabled()
var serviceWithPreview = new ServiceBuilder()
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true, MicrosoftGraphPreviewEnabled: true));

result = await CompilationHelper.RestoreAndCompile(serviceWithPreview, @"provider 'microsoftGraph@1.0.0' as graph");
result = await CompilationHelper.RestoreAndCompile(serviceWithPreview, @"provider microsoftGraph as graph");

result.ExcludingDiagnostics("BCP395").Should().NotHaveAnyDiagnostics();
result.Should().NotHaveAnyDiagnostics();
}
}
}
4 changes: 2 additions & 2 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Expand Up @@ -5683,7 +5683,7 @@ public void Test_Issue12347()
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true))
.WithConfigurationPatch(x => x.WithAnalyzers(x.Analyzers.SetValue("core.rules.use-recent-api-versions.level", "error"))),
("main.bicep", """
provider 'kubernetes@1.0.0' with {
provider kubernetes with {
kubeConfig: 'config'
namespace: ''
} as k8s
Expand Down Expand Up @@ -5721,7 +5721,7 @@ public void Test_Issue12347()
}
"""));

result.ExcludingLinterDiagnostics().ExcludingDiagnostics("BCP395").Should().NotHaveAnyDiagnostics();
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}

// https://github.com/Azure/bicep/issues/13250
Expand Down
@@ -1,7 +1,7 @@
@secure()
param kubeConfig string

provider 'kubernetes@1.0.0' with {
provider kubernetes with {
kubeConfig: kubeConfig
namespace: 'default'
}
Expand Down
@@ -1,4 +1,4 @@
provider 'microsoftGraph@1.0.0' as graph
provider microsoftGraph as graph

param appRoleId string = 'bc76c90e-eb7f-4a29-943b-49e88762d09d'
param scopeId string = 'f761933c-643b-424f-a169-f9313d23a913'
Expand Down
@@ -1,4 +1,4 @@
provider 'microsoftGraph@1.0.0'
provider microsoftGraph

resource resourceApp 'Microsoft.Graph/applications@beta' existing = {
uniqueName: 'resourceApp'
Expand Down
Expand Up @@ -1891,7 +1891,7 @@ public void ProvidesFixWithMostRecentVersion_CustomAgeZero()
public void LinterIgnoresNotAzureResources()
{
CompileAndTestWithFakeDateAndTypes(@"
provider 'kubernetes@1.0.0' with {
provider kubernetes with {
namespace: 'default'
kubeConfig: ''
}
Expand Down

This file was deleted.