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

Simplify IoC for LangServer tests #8644

Merged
merged 4 commits into from
Oct 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/Bicep.Cli.IntegrationTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Bicep.Core.UnitTests.Utils;
using Bicep.Core.Workspaces;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
Expand All @@ -38,15 +39,15 @@ protected record InvocationSettings(FeatureProviderOverrides FeatureOverrides, I
ClientFactory: Repository.Create<IContainerRegistryClientFactory>().Object,
TemplateSpecRepositoryFactory: Repository.Create<ITemplateSpecRepositoryFactory>().Object);

protected static Task<(string output, string error, int result)> Bicep(InvocationSettings settings, params string[] args) =>
TextWriterHelper.InvokeWriterAction((@out, err) =>
new Program(new InvocationContext(
TestTypeHelper.CreateEmptyAzResourceTypeLoader(),
@out,
err,
featureProviderFactory: BicepTestConstants.CreateFeatureProviderFactory(settings.FeatureOverrides),
clientFactory: settings.ClientFactory,
templateSpecRepositoryFactory: settings.TemplateSpecRepositoryFactory)).RunAsync(args));
protected static Task<(string output, string error, int result)> Bicep(InvocationSettings settings, params string[] args)
=> TextWriterHelper.InvokeWriterAction((@out, err)
=> new Program(new(Output: @out, Error: err), services
=> services
.WithEmptyAzResources()
.WithFeatureOverrides(settings.FeatureOverrides)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the DI container handle multiple singletons for an interface? Does the registration for IFeatureProviderFactory performed in this method take precedence over the one performed in services.AddBicepCore() because WithFeatureOverrides is invoked first?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last registration wins - this callback is executed after services.AddBicepCore()

.AddSingleton(settings.ClientFactory)
.AddSingleton(settings.TemplateSpecRepositoryFactory))
.RunAsync(args));

protected static void AssertNoErrors(string error)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Cli/Commands/BuildCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BuildCommand : ICommand
{
private readonly ILogger logger;
private readonly IDiagnosticLogger diagnosticLogger;
private readonly InvocationContext invocationContext;
private readonly IOContext io;
private readonly CompilationService compilationService;
private readonly CompilationWriter writer;
private readonly ParametersWriter paramsWriter;
Expand All @@ -25,15 +25,15 @@ public class BuildCommand : ICommand
public BuildCommand(
ILogger logger,
IDiagnosticLogger diagnosticLogger,
InvocationContext invocationContext,
IOContext io,
CompilationService compilationService,
CompilationWriter writer,
ParametersWriter paramsWriter,
IFeatureProviderFactory featureProviderFactory)
{
this.logger = logger;
this.diagnosticLogger = diagnosticLogger;
this.invocationContext = invocationContext;
this.io = io;
this.compilationService = compilationService;
this.writer = writer;
this.paramsWriter = paramsWriter;
Expand Down
8 changes: 4 additions & 4 deletions src/Bicep.Cli/Commands/DecompileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ public class DecompileCommand : ICommand
{
private readonly ILogger logger;
private readonly IDiagnosticLogger diagnosticLogger;
private readonly InvocationContext invocationContext;
private readonly IOContext io;
private readonly CompilationService compilationService;
private readonly DecompilationWriter writer;

public DecompileCommand(
ILogger logger,
IDiagnosticLogger diagnosticLogger,
InvocationContext invocationContext,
IOContext io,
CompilationService compilationService,
DecompilationWriter writer)
{
this.logger = logger;
this.diagnosticLogger = diagnosticLogger;
this.invocationContext = invocationContext;
this.io = io;
this.compilationService = compilationService;
this.writer = writer;
}
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task<int> RunAsync(DecompileArguments args)
}
catch (Exception exception)
{
invocationContext.ErrorWriter.WriteLine(string.Format(CliResources.DecompilationFailedFormat, PathHelper.ResolvePath(args.InputFile), exception.Message));
io.Error.WriteLine(string.Format(CliResources.DecompilationFailedFormat, PathHelper.ResolvePath(args.InputFile), exception.Message));
return 1;
}

Expand Down
16 changes: 8 additions & 8 deletions src/Bicep.Cli/Commands/FormatCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ public class FormatCommand : ICommand
{
private readonly ILogger logger;
private readonly IDiagnosticLogger diagnosticLogger;
private readonly InvocationContext invocationContext;
private readonly IOContext io;
private readonly IFileResolver fileResolver;

public FormatCommand(
ILogger logger,
IDiagnosticLogger diagnosticLogger,
InvocationContext invocationContext,
IOContext io,
IFileResolver fileResolver)
{
this.logger = logger;
this.diagnosticLogger = diagnosticLogger;
this.invocationContext = invocationContext;
this.io = io;
this.fileResolver = fileResolver;
}

public int Run(FormatArguments args)
{
var inputPath = PathHelper.ResolvePath(args.InputFile);

if (IsBicepFile(inputPath))
{
var inputUri = PathHelper.FilePathToFileUrl(inputPath);
Expand All @@ -56,8 +56,8 @@ public int Run(FormatArguments args)
string output = PrettyPrinter.PrintProgram(programSyntax, options);
if (args.OutputToStdOut)
{
invocationContext.OutputWriter.Write(output);
invocationContext.OutputWriter.Flush();
io.Output.Write(output);
io.Output.Flush();
}
else
{
Expand All @@ -66,8 +66,8 @@ public int Run(FormatArguments args)

File.WriteAllText(outputPath, output);
}
return diagnosticLogger.ErrorCount > 0 ? 1 : 0;

return diagnosticLogger.ErrorCount > 0 ? 1 : 0;
}

logger.LogError(CliResources.UnrecognizedFileExtensionMessage, inputPath);
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Cli/Commands/GenerateParametersFileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ public class GenerateParametersFileCommand : ICommand
{
private readonly ILogger logger;
private readonly IDiagnosticLogger diagnosticLogger;
private readonly InvocationContext invocationContext;
private readonly IOContext io;
private readonly CompilationService compilationService;
private readonly PlaceholderParametersWriter writer;
private readonly IFeatureProviderFactory featureProviderFactory;

public GenerateParametersFileCommand(
ILogger logger,
IDiagnosticLogger diagnosticLogger,
InvocationContext invocationContext,
IOContext io,
CompilationService compilationService,
PlaceholderParametersWriter writer,
IFeatureProviderFactory featureProviderFactory)
{
this.logger = logger;
this.diagnosticLogger = diagnosticLogger;
this.invocationContext = invocationContext;
this.io = io;
this.compilationService = compilationService;
this.writer = writer;
this.featureProviderFactory = featureProviderFactory;
Expand Down
18 changes: 9 additions & 9 deletions src/Bicep.Cli/Commands/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Bicep.Cli.Commands
{
public class RootCommand : ICommand
{
private readonly InvocationContext invocationContext;
private readonly IOContext io;

public RootCommand(InvocationContext invocationContext)
public RootCommand(IOContext io)
{
this.invocationContext = invocationContext;
this.io = io;
}

public int Run(RootArguments args)
Expand Down Expand Up @@ -162,26 +162,26 @@ private void PrintHelp()

"; // this newline is intentional

invocationContext.OutputWriter.Write(output);
invocationContext.OutputWriter.Flush();
io.Output.Write(output);
io.Output.Flush();
}

private void PrintVersion()
{
var output = $@"Bicep CLI version {GetVersionString()}{Environment.NewLine}";

invocationContext.OutputWriter.Write(output);
invocationContext.OutputWriter.Flush();
io.Output.Write(output);
io.Output.Flush();
}

private void PrintLicense()
{
WriteEmbeddedResource(invocationContext.OutputWriter, "LICENSE.deflated");
WriteEmbeddedResource(io.Output, "LICENSE.deflated");
}

private void PrintThirdPartyNotices()
{
WriteEmbeddedResource(invocationContext.OutputWriter, "NOTICE.deflated");
WriteEmbeddedResource(io.Output, "NOTICE.deflated");
}

private static string GetVersionString()
Expand Down
20 changes: 0 additions & 20 deletions src/Bicep.Cli/Helpers/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,5 @@ public static IServiceCollection AddCommands(this IServiceCollection services)

return services;
}

public static IServiceCollection AddInvocationContext(this IServiceCollection services, InvocationContext context)
{
// add itself
services.AddSingleton(context);

// add contents of the context
services.AddSingleton(context.NamespaceProvider);
if (context.FeatureProviderFactory is {} factory)
{
services.AddSingleton(factory);
} else
{
services.AddSingleton<IFeatureProviderFactory, FeatureProviderFactory>();
}
services.AddSingleton(context.ClientFactory);
services.AddSingleton(context.TemplateSpecRepositoryFactory);

return services;
}
}
}
44 changes: 0 additions & 44 deletions src/Bicep.Cli/InvocationContext.cs

This file was deleted.

Loading