This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathStartup.cs
More file actions
105 lines (92 loc) · 4.07 KB
/
Copy pathStartup.cs
File metadata and controls
105 lines (92 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.IO;
using Microsoft.Build.Locator;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Jupyter.Core;
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.AzureClient;
using Microsoft.Quantum.IQSharp.Kernel;
namespace Tests.IQSharp
{
static class Startup
{
internal static ServiceProvider CreateServiceProvider(string workspaceFolder)
{
var dict = new Dictionary<string, string> { { "Workspace", Path.GetFullPath(workspaceFolder) } };
var config = new ConfigurationBuilder()
.AddInMemoryCollection(dict)
.AddJsonFile("appsettings.json")
.Build();
var services = new ServiceCollection();
if (Microsoft.Quantum.IQSharp.Startup.VisualStudioInstance is {} vsi)
{
services.AddSingleton<CompilerService.MSBuildMetadata>(new CompilerService.MSBuildMetadata(
Version: vsi.Version,
RootPath: vsi.VisualStudioRootPath,
Name: vsi.Name,
Path: vsi.MSBuildPath
));
}
services.AddSingleton<IPerformanceMonitor, PerformanceMonitor>();
services.AddSingleton<IConfiguration>(config);
services.Configure<Workspace.Settings>(config);
services.Configure<NugetPackages.Settings>(config);
services.AddLogging(builder =>
{
builder.AddProvider(
new UnitTestLoggerProvider(
new UnitTestLoggerConfiguration
{
LogLevel = LogLevel.Information
}
)
);
});
services.AddTelemetry();
services.AddIQSharp();
services.AddIQSharpKernel();
services.AddAzureClient();
services.AddMocks();
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetRequiredService<ITelemetryService>();
serviceProvider.GetRequiredService<IWorkspace>().Initialization.Wait();
serviceProvider.AddBuiltInMagicSymbols();
return serviceProvider;
}
internal static T Create<T>(string workspaceFolder, Action<IServiceProvider>? configure = null)
{
var serviceProvider = CreateServiceProvider(workspaceFolder);
configure?.Invoke(serviceProvider);
return ActivatorUtilities.CreateInstance<T>(serviceProvider);
}
internal async static Task<T> Create<T>(string workspaceFolder, Func<IServiceProvider, Task> configure)
{
var serviceProvider = CreateServiceProvider(workspaceFolder);
await configure.Invoke(serviceProvider);
return ActivatorUtilities.CreateInstance<T>(serviceProvider);
}
public static void AddMocks(this IServiceCollection services)
{
var shell = new MockShell();
services.AddSingleton<IShellServer>(shell);
services.AddSingleton<IShellRouter>(new MockShellRouter(shell));
services.AddSingleton<ICommsRouter>(new MockCommsRouter(shell));
services.AddSingleton<IOptions<KernelContext>>(new MockKernelOptions());
services.AddSingleton<INugetPackages>(new MockNugetPackages());
services.AddSingleton<IAzureFactory>(new MocksAzureFactory());
}
public static void AddTelemetry(this IServiceCollection services)
{
services.AddSingleton(typeof(ITelemetryService), TelemetryTests.TelemetryServiceType);
}
public static async Task<TOutput> Then<TInput, TOutput>(this Task<TInput> task, Func<TInput, Task<TOutput>> continuation)
{
var input = await task;
return await continuation(input);
}
}
}