Skip to content

Commit

Permalink
Allow test scenarios in Bicep.Core.Samles/Files to define configurati…
Browse files Browse the repository at this point in the history
…on (Azure#8649)
  • Loading branch information
jeskew authored Oct 11, 2022
1 parent d1cba46 commit dc757b4
Show file tree
Hide file tree
Showing 46 changed files with 622 additions and 282 deletions.
24 changes: 10 additions & 14 deletions src/Bicep.Cli.IntegrationTests/BuildCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public async Task Build_Valid_SingleFile_ShouldSucceed(DataSet dataSet)
await dataSet.PublishModulesToRegistryAsync(clientFactory, TestContext);
var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

var features = BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: dataSet.HasExternalModules);
var settings = new InvocationSettings(features, clientFactory, templateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);
var (output, error, result) = await Bicep(settings, "build", bicepFilePath);

using (new AssertionScope())
Expand All @@ -86,8 +85,9 @@ public async Task Build_Valid_SingleFile_ShouldSucceed(DataSet dataSet)
if (dataSet.HasExternalModules)
{
// ensure something got restored
Directory.Exists(settings.Features.CacheRootDirectory).Should().BeTrue();
Directory.EnumerateFiles(settings.Features.CacheRootDirectory, "*.json", SearchOption.AllDirectories).Should().NotBeEmpty();

Directory.Exists(settings.FeatureOverrides.CacheRootDirectory).Should().BeTrue();
Directory.EnumerateFiles(settings.FeatureOverrides.CacheRootDirectory!, "*.json", SearchOption.AllDirectories).Should().NotBeEmpty();
}

var compiledFilePath = Path.Combine(outputDirectory, DataSet.TestFileMainCompiled);
Expand All @@ -112,8 +112,7 @@ public async Task Build_Valid_SingleFile_ToStdOut_ShouldSucceed(DataSet dataSet)
await dataSet.PublishModulesToRegistryAsync(clientFactory, TestContext);
var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

var features = BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: dataSet.HasExternalModules);
var settings = new InvocationSettings(features, clientFactory, templateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);

var (output, error, result) = await Bicep(settings, "build", "--stdout", bicepFilePath);

Expand All @@ -126,7 +125,7 @@ public async Task Build_Valid_SingleFile_ToStdOut_ShouldSucceed(DataSet dataSet)

if (dataSet.HasExternalModules)
{
settings.Features.Should().HaveValidModules();
settings.FeatureOverrides.Should().HaveValidModules();
}

var compiledFilePath = Path.Combine(outputDirectory, DataSet.TestFileMainCompiled);
Expand All @@ -151,8 +150,7 @@ public async Task Build_Valid_SingleFile_After_Restore_Should_Succeed(DataSet da
await dataSet.PublishModulesToRegistryAsync(clientFactory, TestContext);
var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

var features = BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: dataSet.HasExternalModules);
var settings = new InvocationSettings(features, clientFactory, templateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);

var (restoreOutput, restoreError, restoreResult) = await Bicep(settings, "restore", bicepFilePath);
using (new AssertionScope())
Expand Down Expand Up @@ -191,8 +189,7 @@ public async Task Build_Valid_Params_File_Should_Succeed(BaselineData_Bicepparam
{
var data = baselineData.GetData(TestContext);

var features = BicepTestConstants.CreateFeatureProvider(TestContext, paramsFilesEnabled: true);
var settings = new InvocationSettings(features, BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, ParamsFilesEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);

var (output, error, result) = await Bicep(settings, "build", data.Parameters.OutputFilePath);

Expand Down Expand Up @@ -220,7 +217,7 @@ public async Task Build_Valid_SingleFile_WithDigestReference_ShouldSucceed()

var templateSpecRepositoryFactory = BicepTestConstants.TemplateSpecRepositoryFactory;

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, BicepTestConstants.TemplateSpecRepositoryFactory);

var tempDirectory = FileHelper.GetUniqueTestOutputPath(TestContext);
Directory.CreateDirectory(tempDirectory);
Expand Down Expand Up @@ -303,8 +300,7 @@ public async Task Build_Invalid_Single_Params_File_ShouldFail_WithExpectedErrorM
{
var data = baselineData.GetData(TestContext);

var features = BicepTestConstants.CreateFeatureProvider(TestContext, paramsFilesEnabled: true);
var settings = new InvocationSettings(features, BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, ParamsFilesEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var diagnostics = GetAllParamDiagnostics(data.Parameters.OutputFilePath, BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);

var (output, error, result) = await Bicep(settings, "build", data.Parameters.OutputFilePath);
Expand Down
14 changes: 7 additions & 7 deletions src/Bicep.Cli.IntegrationTests/PublishCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task Publish_InvalidTarget_ShouldProduceExpectedError()
[TestMethod]
public async Task Publish_InvalidInputFile_ShouldProduceExpectedError()
{
var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var (output, error, result) = await Bicep(settings, "publish", "WrongFile.bicep", "--target", "br:example.azurecr.io/hello/there:v1");

result.Should().Be(1);
Expand All @@ -91,7 +91,7 @@ public async Task Publish_LocalTarget_ShouldProduceExpectedError()
[TestMethod]
public async Task Publish_OciDigestTarget_ShouldProduceExpectedError()
{
var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var (output, error, result) = await Bicep(settings, "publish", "WrongFile.bicep", "--target", "br:example.com/test@sha256:80f63ced0b80b63874c808a321f472755a3c9e93987d1fa0a51e13c65e4a52b9");

result.Should().Be(1);
Expand All @@ -118,7 +118,7 @@ public async Task Publish_ValidFile_ShouldSucceed(DataSet dataSet)
// mock client factory caches the clients
var testClient = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory, templateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, templateSpecRepositoryFactory);

var (output, error, result) = await Bicep(settings, "publish", bicepFilePath, "--target", $"br:{registryStr}/{repository}:v1");
result.Should().Be(0);
Expand Down Expand Up @@ -159,7 +159,7 @@ public async Task Publish_ValidArmTemplteFile_ShouldSucceed(DataSet dataSet)
// mock client factory caches the clients
var testClient = (MockRegistryBlobClient)clientFactory.CreateAuthenticatedBlobClient(BicepTestConstants.BuiltInConfiguration, registryUri, repository);

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory, templateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory, templateSpecRepositoryFactory);

var (output, error, result) = await Bicep(settings, "publish", compiledFilePath, "--target", $"br:{registryStr}/{repository}:v1");
result.Should().Be(0);
Expand Down Expand Up @@ -201,7 +201,7 @@ public async Task Publish_RequestFailedException_ShouldFail()

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var (output, error, result) = await Bicep(settings, "publish", compiledFilePath, "--target", "br:fake/fake:v1");
using (new AssertionScope())
{
Expand Down Expand Up @@ -230,7 +230,7 @@ public async Task Publish_AggregateExceptionWithInnerRequestFailedExceptions_Sho

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var (output, error, result) = await Bicep(settings, "publish", compiledFilePath, "--target", "br:fake/fake:v1");
using (new AssertionScope())
{
Expand All @@ -248,7 +248,7 @@ public async Task Publish_InvalidFile_ShouldFail_WithExpectedErrorMessage(DataSe
var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

// publish won't actually happen, so broken client factory is fine
var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);

var diagnostics = GetAllDiagnostics(bicepFilePath, settings.ClientFactory, settings.TemplateSpecRepositoryFactory);

Expand Down
16 changes: 8 additions & 8 deletions src/Bicep.Cli.IntegrationTests/RestoreCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public async Task Restore_ShouldSucceed(DataSet dataSet)

var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);
TestContext.WriteLine($"Cache root = {settings.Features.CacheRootDirectory}");
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);
TestContext.WriteLine($"Cache root = {settings.FeatureOverrides.CacheRootDirectory}");
var (output, error, result) = await Bicep(settings, "restore", bicepFilePath);

using (new AssertionScope())
Expand All @@ -71,7 +71,7 @@ public async Task Restore_ShouldSucceed(DataSet dataSet)
if (dataSet.HasExternalModules)
{
// ensure something got restored
settings.Features.Should().HaveValidModules();
settings.FeatureOverrides.Should().HaveValidModules();
}
}

Expand All @@ -89,7 +89,7 @@ public async Task Restore_ByDigest_ShouldSucceed()

var templateSpecRepositoryFactory = BicepTestConstants.TemplateSpecRepositoryFactory;

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, BicepTestConstants.TemplateSpecRepositoryFactory);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, BicepTestConstants.TemplateSpecRepositoryFactory);

var tempDirectory = FileHelper.GetUniqueTestOutputPath(TestContext);
Directory.CreateDirectory(tempDirectory);
Expand Down Expand Up @@ -141,8 +141,8 @@ public async Task Restore_NonExistentModules_ShouldFail(DataSet dataSet)

var bicepFilePath = Path.Combine(outputDirectory, DataSet.TestFileMain);

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);
TestContext.WriteLine($"Cache root = {settings.Features.CacheRootDirectory}");
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: dataSet.HasExternalModules), clientFactory, templateSpecRepositoryFactory);
TestContext.WriteLine($"Cache root = {settings.FeatureOverrides.CacheRootDirectory}");
var (output, error, result) = await Bicep(settings, "restore", bicepFilePath);

using (new AssertionScope())
Expand Down Expand Up @@ -175,7 +175,7 @@ public async Task Restore_AggregateExceptionWithInnerRequestFailedExceptions_Sho

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var (output, error, result) = await Bicep(settings, "restore", compiledFilePath);
using(new AssertionScope())
{
Expand Down Expand Up @@ -207,7 +207,7 @@ public async Task Restore_RequestFailedException_ShouldFail()

var templateSpecRepositoryFactory = StrictMock.Of<ITemplateSpecRepositoryFactory>();

var settings = new InvocationSettings(BicepTestConstants.CreateFeatureProvider(TestContext, registryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var settings = new InvocationSettings(new(TestContext, RegistryEnabled: true), clientFactory.Object, templateSpecRepositoryFactory.Object);
var (output, error, result) = await Bicep(settings, "restore", compiledFilePath);
using (new AssertionScope())
{
Expand Down
10 changes: 2 additions & 8 deletions src/Bicep.Cli.IntegrationTests/RootCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public async Task BicepVersionShouldPrintVersionInformation()
[TestMethod]
public async Task BicepHelpShouldPrintHelp()
{
var featuresMock = Repository.Create<IFeatureProvider>();
featuresMock.Setup(m => m.RegistryEnabled).Returns(true);

var settings = CreateDefaultSettings() with { Features = featuresMock.Object };
var settings = CreateDefaultSettings() with { FeatureOverrides = new(RegistryEnabled: true) };

var (output, error, result) = await Bicep(settings, "--help");

Expand Down Expand Up @@ -132,10 +129,7 @@ public async Task BicepHelpShouldAlwaysIncludePublish()
{
// disable registry to ensure `bicep --help` is not consulting the feature provider before
// preparing the help text (as features can only be determined when an input file is specified)
var featuresMock = Repository.Create<IFeatureProvider>();
featuresMock.Setup(m => m.RegistryEnabled).Returns(false);

var settings = CreateDefaultSettings() with { Features = featuresMock.Object };
var settings = CreateDefaultSettings() with { FeatureOverrides = new(RegistryEnabled: false) };

var (output, error, result) = await Bicep(settings, "--help");

Expand Down
7 changes: 4 additions & 3 deletions src/Bicep.Cli.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Bicep.Core.Semantics;
using Bicep.Core.Text;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Utils;
using Bicep.Core.Workspaces;
using FluentAssertions;
Expand All @@ -28,12 +29,12 @@ public abstract class TestBase

protected static readonly MockRepository Repository = new(MockBehavior.Strict);

protected record InvocationSettings(IFeatureProvider Features, IContainerRegistryClientFactory ClientFactory, ITemplateSpecRepositoryFactory TemplateSpecRepositoryFactory);
protected record InvocationSettings(FeatureProviderOverrides FeatureOverrides, IContainerRegistryClientFactory ClientFactory, ITemplateSpecRepositoryFactory TemplateSpecRepositoryFactory);

protected static Task<(string output, string error, int result)> Bicep(params string[] args) => Bicep(CreateDefaultSettings(), args);

protected static InvocationSettings CreateDefaultSettings() => new(
Features: BicepTestConstants.Features,
FeatureOverrides: BicepTestConstants.FeatureOverrides,
ClientFactory: Repository.Create<IContainerRegistryClientFactory>().Object,
TemplateSpecRepositoryFactory: Repository.Create<ITemplateSpecRepositoryFactory>().Object);

Expand All @@ -43,7 +44,7 @@ protected record InvocationSettings(IFeatureProvider Features, IContainerRegistr
TestTypeHelper.CreateEmptyAzResourceTypeLoader(),
@out,
err,
featureProviderFactory: IFeatureProviderFactory.WithStaticFeatureProvider(settings.Features),
featureProviderFactory: BicepTestConstants.CreateFeatureProviderFactory(settings.FeatureOverrides),
clientFactory: settings.ClientFactory,
templateSpecRepositoryFactory: settings.TemplateSpecRepositoryFactory)).RunAsync(args));

Expand Down
Loading

0 comments on commit dc757b4

Please sign in to comment.