Skip to content

Commit

Permalink
Refactor to simplify logic for 3rd party provider implementation (Azu…
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Dec 15, 2023
1 parent fef5764 commit 5c0d0fe
Show file tree
Hide file tree
Showing 43 changed files with 4,418 additions and 4,577 deletions.
71 changes: 71 additions & 0 deletions src/Bicep.Core.IntegrationTests/DynamicAzTypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Threading.Tasks;
using Azure.Bicep.Types.Az;
using Bicep.Core.Diagnostics;
using Bicep.Core.IntegrationTests.Extensibility;
using Bicep.Core.Registry;
using Bicep.Core.Samples;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.TypeSystem.Providers.Az;
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;
using Newtonsoft.Json.Linq;

namespace Bicep.Core.IntegrationTests
{
[TestClass]
public class DynamicAzTypesTests : TestBase
{
private static readonly Lazy<IResourceTypeLoader> azTypeLoaderLazy = new(() => new AzResourceTypeLoader(new AzTypeLoader()));

private async Task<ServiceBuilder> GetServices()
{
var indexJson = FileHelper.SaveResultFile(TestContext, "types/index.json", """{"Resources": {}, "Functions": {}}""");
var clientFactory = await DataSetsExtensions.GetClientFactoryWithAzModulePublished(new System.IO.Abstractions.FileSystem(), indexJson);

var cacheRoot = FileHelper.GetUniqueTestOutputPath(TestContext);
Directory.CreateDirectory(cacheRoot);

return new ServiceBuilder()
.WithFeatureOverrides(new(ExtensibilityEnabled: true, DynamicTypeLoadingEnabled: true, CacheRootDirectory: cacheRoot))
.WithContainerRegistryClientFactory(clientFactory)
.WithAzResourceTypeLoader(azTypeLoaderLazy.Value);
}

[TestMethod]
public async Task Az_namespace_can_be_used_without_configuration()
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, ("main.bicep", @$"
provider 'br/public:az@{BicepTestConstants.BuiltinAzProviderVersion}'
"));

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

[TestMethod]
public async Task Az_namespace_errors_with_configuration()
{
var services = await GetServices();
var result = await CompilationHelper.RestoreAndCompile(services, @$"
provider 'br/public:az@{BicepTestConstants.BuiltinAzProviderVersion}' with {{}}
");

result.Should().NotGenerateATemplate();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP205", DiagnosticLevel.Error, "Provider namespace \"az\" does not support configuration."),
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,17 @@ public TestExtensibilityNamespaceProvider(IResourceTypeProviderFactory azResourc
defaultNamespaceProvider = new DefaultNamespaceProvider(azResourceTypeProviderFactory);
}

public IEnumerable<string> AvailableNamespaces => defaultNamespaceProvider.AvailableNamespaces.Concat(new[] {
FooNamespaceType.BuiltInName,
BarNamespaceType.BuiltInName,
});

public NamespaceType? TryGetNamespace(
ResourceTypesProviderDescriptor providerDescriptor,
ResourceScope resourceScope,
IFeatureProvider featureProvider,
BicepSourceFileKind sourceFileKind)
{
var namespaceType = defaultNamespaceProvider.TryGetNamespace(
providerDescriptor,
resourceScope,
featureProvider,
sourceFileKind);

return providerDescriptor.Name switch
{
FooNamespaceType.BuiltInName
=> FooNamespaceType.Create(providerDescriptor.Alias),
BarNamespaceType.BuiltInName
=> BarNamespaceType.Create(providerDescriptor.Alias),
_
=> namespaceType,
FooNamespaceType.BuiltInName => FooNamespaceType.Create(providerDescriptor.Alias),
BarNamespaceType.BuiltInName => BarNamespaceType.Create(providerDescriptor.Alias),
_ => defaultNamespaceProvider.TryGetNamespace(providerDescriptor, resourceScope, featureProvider, sourceFileKind),
};
}
}
40 changes: 4 additions & 36 deletions src/Bicep.Core.IntegrationTests/ExtensibilityTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;

using Bicep.Core.Diagnostics;
using Bicep.Core.IntegrationTests.Extensibility;
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 All @@ -16,16 +13,11 @@
namespace Bicep.Core.IntegrationTests
{
[TestClass]
public class ExtensibilityTests
public class ExtensibilityTests : TestBase
{
[NotNull]
public TestContext? TestContext { get; set; }

private ServiceBuilder Services => new ServiceBuilder()
//.WithAzResourceTypeLoader()
.WithFeatureOverrides(new(ExtensibilityEnabled: true, DynamicTypeLoadingEnabled: true, CacheRootDirectory: InMemoryFileResolver.GetFileUri("/test/.bicep").LocalPath))
.WithNamespaceProvider(new TestExtensibilityNamespaceProvider(BicepTestConstants.ResourceTypeProviderFactory)
);
.WithFeatureOverrides(new(ExtensibilityEnabled: true, DynamicTypeLoadingEnabled: true))
.WithNamespaceProvider(new TestExtensibilityNamespaceProvider(BicepTestConstants.ResourceTypeProviderFactory));

[TestMethod]
public void Bar_import_bad_config_is_blocked()
Expand Down Expand Up @@ -624,29 +616,5 @@ public void Bar_import_end_to_end_test()
}
"""));
}

[TestMethod]
public void Az_namespace_can_be_used_without_configuration()
{
var result = CompilationHelper.Compile(Services, @$"
provider 'br/public:az@{BicepTestConstants.BuiltinAzProviderVersion}'
");

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

[TestMethod]
public void Az_namespace_errors_with_configuration()
{
var result = CompilationHelper.Compile(Services, @$"
provider 'br/public:az@{BicepTestConstants.BuiltinAzProviderVersion}' with {{}}
");

result.Should().NotGenerateATemplate();
result.ExcludingLinterDiagnostics().Should().HaveDiagnostics(new[] {
("BCP205", DiagnosticLevel.Error, "Provider namespace \"az\" does not support configuration."),
});
}
}
}
Loading

0 comments on commit 5c0d0fe

Please sign in to comment.