Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
asilverman committed Oct 12, 2023
1 parent 48a96c1 commit fea7164
Show file tree
Hide file tree
Showing 40 changed files with 451 additions and 259 deletions.
17 changes: 11 additions & 6 deletions src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,18 @@ public async Task Build_Valid_SingleFile_WithTemplateSpecReference_ShouldSucceed
actualLocation: compiledFilePath);
}

[TestMethod]
public async Task Provider_Artifacts_Restore_From_Registry_ShouldSucceed()
[DataTestMethod]
[DataRow("br:mcr.microsoft.com/bicep/providers/az", true)]
[DataRow("br/public:az", true)]
//[DataRow("br/contoso:az", true)]
// Negative
//[DataRow("az", false)]
public async Task Build_Valid_SingleFile_WithProviderDeclarationStatement(string providerDeclarationSyntax, bool shouldSucceed)
{
// SETUP
// 1. create a mock registry client
var registryUri = new Uri($"https://{LanguageConstants.BicepPublicMcrRegistry}");
var repository = $"bicep/providers/az";
var repository = "bicep/providers/az";
var (clientFactory, blobClients) = DataSetsExtensions.CreateMockRegistryClients(false, (registryUri, repository));
var myClient = blobClients[(registryUri, repository)];

Expand Down Expand Up @@ -142,8 +147,8 @@ public async Task Provider_Artifacts_Restore_From_Registry_ShouldSucceed()
await myClient.UploadBlobAsync(new MemoryStream());

// 3. create a main.bicep and save it to a output directory
var bicepFile = """
import 'az@2.0.0'
var bicepFile = $"""
import '{providerDeclarationSyntax}@2.0.0'
""";
var tempDirectory = FileHelper.GetUniqueTestOutputPath(TestContext);
Directory.CreateDirectory(tempDirectory);
Expand All @@ -161,7 +166,7 @@ public async Task Provider_Artifacts_Restore_From_Registry_ShouldSucceed()
// 6. assert 'bicep build' completed successfully
using (new AssertionScope())
{
result.Should().Be(0);
result.Should().Be(shouldSucceed ? 0 : 1);
output.Should().BeEmpty();
AssertNoErrors(error);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Bicep.Cli/Commands/PublishCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Bicep.Core.Features;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry;
using Bicep.Core.Registry.Oci;
using Bicep.Core.SourceCode;

namespace Bicep.Cli.Commands
Expand Down Expand Up @@ -98,7 +99,7 @@ private async Task PublishModuleAsync(ArtifactReference target, Stream compiledA

private ArtifactReference ValidateReference(string targetModuleReference, Uri targetModuleUri)
{
if (!this.moduleDispatcher.TryGetModuleReference(targetModuleReference, targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder))
if (!this.moduleDispatcher.TryGetArtifactReference(targetModuleReference, "module", targetModuleUri).IsSuccess(out var moduleReference, out var failureBuilder))
{
// TODO: We should probably clean up the dispatcher contract so this sort of thing isn't necessary (unless we change how target module is set in this command)
var message = failureBuilder(DiagnosticBuilder.ForDocumentStart()).Message;
Expand Down
14 changes: 7 additions & 7 deletions src/Bicep.Core.IntegrationTests/RegistryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public async Task ModuleRestoreContentionShouldProduceConsistentState(bool publi

var moduleReferences = dataSet.RegistryModules.Values
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(7);
Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task ModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnumerab
var configuration = BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled;
var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand All @@ -249,7 +249,7 @@ public async Task ModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnumerab
dispatcher.GetArtifactRestoreStatus(moduleReference, out _).Should().Be(ArtifactRestoreStatus.Unknown);
}

dispatcher.TryGetLocalModuleEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
dispatcher.TryGetLocalArtifactEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
moduleFileUri.Should().NotBeNull();

var moduleFilePath = moduleFileUri!.LocalPath;
Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task ForceModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnu

var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand All @@ -318,7 +318,7 @@ public async Task ForceModuleRestoreWithStuckFileLockShouldFailAfterTimeout(IEnu
dispatcher.GetArtifactRestoreStatus(moduleReference, out _).Should().Be(ArtifactRestoreStatus.Unknown);
}

dispatcher.TryGetLocalModuleEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
dispatcher.TryGetLocalArtifactEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
moduleFileUri.Should().NotBeNull();

var moduleFilePath = moduleFileUri!.LocalPath;
Expand Down Expand Up @@ -384,7 +384,7 @@ public async Task ForceModuleRestoreShouldRestoreAllModules(IEnumerable<External
var configuration = BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled;
var moduleReferences = moduleInfos
.OrderBy(m => m.Metadata.Target)
.Select(m => dispatcher.TryGetModuleReference(m.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.Select(m => dispatcher.TryGetArtifactReference(m.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) ? @ref : throw new AssertFailedException($"Invalid module target '{m.Metadata.Target}'."))
.ToImmutableList();

moduleReferences.Should().HaveCount(moduleCount);
Expand All @@ -395,7 +395,7 @@ public async Task ForceModuleRestoreShouldRestoreAllModules(IEnumerable<External
dispatcher.GetArtifactRestoreStatus(moduleReference, out _).Should().Be(ArtifactRestoreStatus.Unknown);
}

dispatcher.TryGetLocalModuleEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
dispatcher.TryGetLocalArtifactEntryPointUri(moduleReferences[0]).IsSuccess(out var moduleFileUri).Should().BeTrue();
moduleFileUri.Should().NotBeNull();

var moduleFilePath = moduleFileUri!.LocalPath;
Expand Down
7 changes: 4 additions & 3 deletions src/Bicep.Core.Samples/DataSetsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Bicep.Core.Configuration;
using Bicep.Core.FileSystem;
using Bicep.Core.Registry.Oci;
using Bicep.Core.Modules;
using Bicep.Core.Registry;
using Bicep.Core.Semantics;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static Mock<IContainerRegistryClientFactory> CreateMockRegistryClients(Im
{
var target = publishInfo.Metadata.Target;

if (!dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciModuleReference targetReference)
if (!dispatcher.TryGetArtifactReference(target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not OciArtifactReference targetReference)
{
throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact.");
}
Expand Down Expand Up @@ -148,7 +149,7 @@ public static ITemplateSpecRepositoryFactory CreateMockTemplateSpecRepositoryFac

foreach (var (moduleName, templateSpecInfo) in templateSpecs)
{
if (!dispatcher.TryGetModuleReference(templateSpecInfo.Metadata.Target, RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference)
if (!dispatcher.TryGetArtifactReference(templateSpecInfo.Metadata.Target, "module", RandomFileUri()).IsSuccess(out var @ref) || @ref is not TemplateSpecModuleReference reference)
{
throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{templateSpecInfo.Metadata.Target}'. Specify a reference to a template spec.");
}
Expand Down Expand Up @@ -190,7 +191,7 @@ public static async Task PublishModuleToRegistryAsync(IContainerRegistryClientFa
.AddSingleton(featureProviderFactory)
).Construct<IModuleDispatcher>();

var targetReference = dispatcher.TryGetModuleReference(target, RandomFileUri()).IsSuccess(out var @ref) ? @ref
var targetReference = dispatcher.TryGetArtifactReference(target, "TODO", RandomFileUri()).IsSuccess(out var @ref) ? @ref
: throw new InvalidOperationException($"Module '{moduleName}' has an invalid target reference '{target}'. Specify a reference to an OCI artifact.");

var result = CompilationHelper.Compile(moduleSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void RootConfiguration_LeadingTildeInCacheRootDirectory_ExpandPath(string
var configuration = new RootConfiguration(
BicepTestConstants.BuiltInConfiguration.Cloud,
BicepTestConstants.BuiltInConfiguration.ModuleAliases,
BicepTestConstants.BuiltInConfiguration.ProviderAliases,
BicepTestConstants.BuiltInConfiguration.Analyzers,
cacheRootDirectory,
BicepTestConstants.BuiltInConfiguration.ExperimentalFeaturesEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void If_OutputReferencesSecureParamProperty_ShouldFail(string text, param
CompileAndTest(text, OnCompileErrors.IncludeErrors, expectedMessages, config => new(
config.Cloud,
config.ModuleAliases,
config.ProviderAliases,
config.Analyzers,
config.CacheRootDirectory,
config.ExperimentalFeaturesEnabled with { UserDefinedTypes = true },
Expand Down Expand Up @@ -228,6 +229,7 @@ public void If_OutputReferencesParamWithSecureProperty_ShouldFail(string text, p
CompileAndTest(text, OnCompileErrors.IncludeErrors, expectedMessages, config => new(
config.Cloud,
config.ModuleAliases,
config.ProviderAliases,
config.Analyzers,
config.CacheRootDirectory,
config.ExperimentalFeaturesEnabled with { UserDefinedTypes = true },
Expand Down Expand Up @@ -279,6 +281,7 @@ public void If_OutputReferencesNonSecureParamProperty_ShouldPass(string text, pa
CompileAndTest(text, OnCompileErrors.IncludeErrors, expectedMessages, config => new(
config.Cloud,
config.ModuleAliases,
config.ProviderAliases,
config.Analyzers,
config.CacheRootDirectory,
config.ExperimentalFeaturesEnabled with { UserDefinedTypes = true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private static RootConfiguration CreateConfigurationWithFakeToday(RootConfigurat
return new RootConfiguration(
original.Cloud,
original.ModuleAliases,
original.ProviderAliases,
new AnalyzersConfiguration(
JsonElementFactory.CreateElement(@"
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Bicep.Core.Configuration;
using Bicep.Core.FileSystem;
using Bicep.Core.Modules;
using Bicep.Core.Registry.Oci;
using Bicep.Core.UnitTests.Assertions;
using FluentAssertions;
using FluentAssertions.Execution;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void ValidReferenceShouldBeUriParseable(ValidCase @case)
[DataTestMethod]
public void InvalidReferencesShouldProduceExpectedError(string value, string expectedCode, string expectedError)
{
OciModuleReference.TryParse(null, value, BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled, RandomFileUri()).IsSuccess(out var @ref, out var failureBuilder).Should().BeFalse();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, null, value, BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled, RandomFileUri()).IsSuccess(out var @ref, out var failureBuilder).Should().BeFalse();
@ref.Should().BeNull();
failureBuilder!.Should().NotBeNull();

Expand Down Expand Up @@ -145,7 +146,7 @@ public void MismatchedReferencesShouldNotBeEqual(string package1, string package
[DataRow("foo bar ÄÄÄ")]
public void TryParse_InvalidAliasName_ReturnsFalseAndSetsErrorDiagnostic(string aliasName)
{
OciModuleReference.TryParse(aliasName, "", BicepTestConstants.BuiltInConfiguration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, aliasName, "", BicepTestConstants.BuiltInConfiguration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();

reference.Should().BeNull();
errorBuilder!.Should().HaveCode("BCP211");
Expand All @@ -159,7 +160,7 @@ public void TryParse_AliasNotInConfiguration_ReturnsFalseAndSetsErrorDiagnostic(
{
var configuration = BicepTestConstants.CreateMockConfiguration(configurationPath: configurationPath);

OciModuleReference.TryParse(aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();

reference.Should().BeNull();
errorBuilder!.Should().NotBeNull();
Expand All @@ -171,7 +172,7 @@ public void TryParse_AliasNotInConfiguration_ReturnsFalseAndSetsErrorDiagnostic(
[DynamicData(nameof(GetInvalidAliasData), DynamicDataSourceType.Method)]
public void TryParse_InvalidAlias_ReturnsFalseAndSetsErrorDiagnostic(string aliasName, string referenceValue, RootConfiguration configuration, string expectedCode, string expectedMessage)
{
OciModuleReference.TryParse(aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeFalse();

reference.Should().BeNull();
errorBuilder!.Should().NotBeNull();
Expand All @@ -183,22 +184,22 @@ public void TryParse_InvalidAlias_ReturnsFalseAndSetsErrorDiagnostic(string alia
[DynamicData(nameof(GetValidAliasData), DynamicDataSourceType.Method)]
public void TryGetModuleReference_ValidAlias_ReplacesReferenceValue(string aliasName, string referenceValue, string fullyQualifiedReferenceValue, RootConfiguration configuration)
{
OciModuleReference.TryParse(aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeTrue();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, aliasName, referenceValue, configuration, RandomFileUri()).IsSuccess(out var reference, out var errorBuilder).Should().BeTrue();

reference.Should().NotBeNull();
reference!.FullyQualifiedReference.Should().Be(fullyQualifiedReferenceValue);
}


private static OciModuleReference Parse(string package)
private static OciArtifactReference Parse(string package)
{
OciModuleReference.TryParse(null, package, BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled, RandomFileUri()).IsSuccess(out var parsed, out var failureBuilder).Should().BeTrue();
OciArtifactReference.TryParse(OciArtifactReferenceType.Module, null, package, BicepTestConstants.BuiltInConfigurationWithAllAnalyzersDisabled, RandomFileUri()).IsSuccess(out var parsed, out var failureBuilder).Should().BeTrue();
failureBuilder!.Should().BeNull();
parsed.Should().NotBeNull();
return parsed!;
}

private static (OciModuleReference, OciModuleReference) ParsePair(string first, string second) => (Parse(first), Parse(second));
private static (OciArtifactReference, OciArtifactReference) ParsePair(string first, string second) => (Parse(first), Parse(second));

private static IEnumerable<object[]> GetValidCases()
{
Expand Down
Loading

0 comments on commit fea7164

Please sign in to comment.