Skip to content

Commit

Permalink
Simplify/fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Weatherford authored and Stephen Weatherford committed Dec 17, 2023
1 parent ef02c60 commit 273fd6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
25 changes: 6 additions & 19 deletions src/Bicep.Core.UnitTests/BicepTestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
using Bicep.Core.Analyzers.Linter;
using Bicep.Core.Configuration;
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.Features;
using Bicep.Core.FileSystem;
using Bicep.Core.Json;
using Bicep.Core.Registry;
using Bicep.Core.Registry.Oci;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.SourceCode;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.TypeSystem.Providers.Az;
using Bicep.Core.UnitTests.Configuration;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Mock;
Expand Down Expand Up @@ -88,7 +85,7 @@ public static class BicepTestConstants

public static IEnvironment EmptyEnvironment = new TestEnvironment(ImmutableDictionary<string, string?>.Empty);

public static readonly IModuleRestoreScheduler ModuleRestoreScheduler = CreateMockModuleDispatcherAndRestoreScheduler().moduleRestoreScheduler;
public static readonly IModuleRestoreScheduler ModuleRestoreScheduler = CreateMockModuleRestoreScheduler();

public static RootConfiguration CreateMockConfiguration(Dictionary<string, object>? customConfigurationData = null, string? configurationPath = null)
{
Expand Down Expand Up @@ -131,20 +128,10 @@ public static IConfigurationManager CreateConfigurationManager(Func<RootConfigur
public static IFeatureProviderFactory CreateFeatureProviderFactory(FeatureProviderOverrides featureOverrides, IConfigurationManager? configurationManager = null)
=> new OverriddenFeatureProviderFactory(new FeatureProviderFactory(configurationManager ?? CreateFilesystemConfigurationManager()), featureOverrides);

public static (IModuleDispatcher moduleDispatcher, IModuleRestoreScheduler moduleRestoreScheduler) CreateMockModuleDispatcherAndRestoreScheduler(IArtifactRegistry[]? artifactRegistries = null)
private static IModuleRestoreScheduler CreateMockModuleRestoreScheduler()
{
var moduleDispatcher = StrictMock.Of<IModuleDispatcher>();
moduleDispatcher.Setup(x => x.RestoreModules(It.IsAny<ImmutableArray<ArtifactReference>>(), It.IsAny<bool>())).
ReturnsAsync(true);
moduleDispatcher.Setup(x => x.PruneRestoreStatuses());

MockRepository repository = new(MockBehavior.Strict);
var provider = repository.Create<IArtifactRegistryProvider>();

moduleDispatcher.Setup(m => m.TryGetModuleSources(It.IsAny<ArtifactReference>())).Returns((ArtifactReference reference) =>
(artifactRegistries ?? new IArtifactRegistry[] { }).Select(r => r.TryGetSource(reference)).FirstOrDefault(s => s is not null));

return (moduleDispatcher.Object, new ModuleRestoreScheduler(moduleDispatcher.Object));
return new ModuleRestoreScheduler(moduleDispatcher.Object);
}

public static Mock<ITelemetryProvider> CreateMockTelemetryProvider()
Expand Down
20 changes: 18 additions & 2 deletions src/Bicep.LangServer.IntegrationTests/CodeLensTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand All @@ -19,6 +20,7 @@
using Bicep.Core.Workspaces;
using Bicep.LangServer.IntegrationTests.Helpers;
using Bicep.LanguageServer.Handlers;
using Bicep.LanguageServer.Registry;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.ResourceStack.Common.Extensions;
Expand Down Expand Up @@ -58,12 +60,26 @@ private SharedLanguageHelperManager CreateServer(Uri? bicepModuleEntrypoint, str
}
moduleRegistry.Setup(m => m.TryGetSource(It.IsAny<ArtifactReference>())).Returns(sourceArchive);

var moduleDispatcher = StrictMock.Of<IModuleDispatcher>();
moduleDispatcher.Setup(x => x.RestoreModules(It.IsAny<ImmutableArray<ArtifactReference>>(), It.IsAny<bool>())).
ReturnsAsync(true);
moduleDispatcher.Setup(x => x.PruneRestoreStatuses());

MockRepository repository = new(MockBehavior.Strict);
var provider = repository.Create<IArtifactRegistryProvider>();

var artifactRegistries = moduleRegistry.Object.AsArray();

moduleDispatcher.Setup(m => m.TryGetModuleSources(It.IsAny<ArtifactReference>())).Returns((ArtifactReference reference) =>
artifactRegistries.Select(r => r.TryGetSource(reference)).FirstOrDefault(s => s is not null));

var defaultServer = new SharedLanguageHelperManager();
defaultServer.Initialize(
async () => await MultiFileLanguageServerHelper.StartLanguageServer(
TestContext,
services => services.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true)),
moduleRegistry.Object.AsArray()));
services => services
.WithModuleDispatcher(moduleDispatcher.Object)
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true))));
return defaultServer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;
using Bicep.Core.Tracing;
using Bicep.Core.Registry;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.FileSystem;
using Bicep.Core.UnitTests.Utils;
Expand Down Expand Up @@ -37,20 +36,17 @@ private LanguageServerHelper(Server server, ILanguageClient client)
/// <summary>
/// Creates and initializes a new language server/client pair without loading any files. This is recommended when you need to open multiple files using the language server.
/// </summary>
public static async Task<LanguageServerHelper> StartServer(TestContext testContext, Action<LanguageClientOptions>? onClientOptions = null, Action<IServiceCollection>? onRegisterServices = null, IArtifactRegistry[]? artifactRegistries = null)
public static async Task<LanguageServerHelper> StartServer(TestContext testContext, Action<LanguageClientOptions>? onClientOptions = null, Action<IServiceCollection>? onRegisterServices = null)
{
using var timer = new ExecutionTimer($"Starting language server", logInitial: false);
var clientPipe = new Pipe();
var serverPipe = new Pipe();

var (mockDispatcher, mockRestoreScheduler) = BicepTestConstants.CreateMockModuleDispatcherAndRestoreScheduler(artifactRegistries);

var server = new Server(
options => options
.WithInput(serverPipe.Reader)
.WithOutput(clientPipe.Writer)
.WithServices(services => services.AddSingleton(mockDispatcher))
.WithServices(services => services.AddSingleton(mockRestoreScheduler))
.WithServices(services => services.AddSingleton(BicepTestConstants.ModuleRestoreScheduler))
.WithServices(services => onRegisterServices?.Invoke(services)));
var _ = server.RunAsync(CancellationToken.None); // do not wait on this async method, or you'll be waiting a long time!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private MultiFileLanguageServerHelper(Server server, ILanguageClient client, Con
this.notificationRouter = notificationRouter;
}

public static async Task<MultiFileLanguageServerHelper> StartLanguageServer(TestContext testContext, Action<IServiceCollection>? onRegisterServices = null, IArtifactRegistry[]? artifactRegistries = null)
public static async Task<MultiFileLanguageServerHelper> StartLanguageServer(TestContext testContext, Action<IServiceCollection>? onRegisterServices = null)
{
var notificationRouter = new ConcurrentDictionary<DocumentUri, MultipleMessageListener<PublishDiagnosticsParams>>();
var helper = await LanguageServerHelper.StartServer(
Expand All @@ -54,8 +54,7 @@ public static async Task<MultiFileLanguageServerHelper> StartLanguageServer(Test
throw new AssertFailedException($"Task completion source was not registered for document uri '{p.Uri}'.");
});
},
onRegisterServices,
artifactRegistries);
onRegisterServices);

return new(helper.Server, helper.Client, notificationRouter);
}
Expand Down

0 comments on commit 273fd6c

Please sign in to comment.