-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathTestDistributedApplicationBuilder.cs
More file actions
74 lines (60 loc) · 3.47 KB
/
Copy pathTestDistributedApplicationBuilder.cs
File metadata and controls
74 lines (60 loc) · 3.47 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
// Copied from aspire
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Components.Common.TestUtilities;
using Microsoft.Extensions.Logging;
namespace Aspire.Hosting.Utils;
/// <summary>
/// DistributedApplication.CreateBuilder() creates a builder that includes configuration to read from appsettings.json.
/// The builder has a FileSystemWatcher, which can't be cleaned up unless a DistributedApplication is built and disposed.
/// This class wraps the builder and provides a way to automatically dispose it to prevent test failures from excessive
/// FileSystemWatcher instances from many tests.
/// </summary>
public static class TestDistributedApplicationBuilder
{
public static IDistributedApplicationTestingBuilder Create(DistributedApplicationOperation operation, string publisher = "manifest", string outputPath = "./", bool isDeploy = false)
{
var args = operation switch
{
DistributedApplicationOperation.Run => (string[])[],
DistributedApplicationOperation.Publish => [$"Publishing:Publisher={publisher}", $"Publishing:OutputPath={outputPath}", $"Publishing:Deploy={isDeploy}"],
_ => throw new ArgumentOutOfRangeException(nameof(operation))
};
return Create(args);
}
public static IDistributedApplicationTestingBuilder Create(params string[] args)
{
return CreateCore(args, (_) => { });
}
public static IDistributedApplicationTestingBuilder Create(ITestOutputHelper testOutputHelper, params string[] args)
{
return CreateCore(args, (_) => { }, testOutputHelper);
}
public static IDistributedApplicationTestingBuilder Create(Action<DistributedApplicationOptions>? configureOptions, ITestOutputHelper? testOutputHelper = null)
{
return CreateCore([], configureOptions, testOutputHelper);
}
public static IDistributedApplicationTestingBuilder CreateWithTestContainerRegistry(ITestOutputHelper testOutputHelper) =>
Create(o => o.ContainerRegistryOverride = ComponentTestConstants.AspireTestContainerRegistry, testOutputHelper);
private static IDistributedApplicationTestingBuilder CreateCore(string[] args, Action<DistributedApplicationOptions>? configureOptions, ITestOutputHelper? testOutputHelper = null)
{
var builder = DistributedApplicationTestingBuilder.Create(args, (applicationOptions, hostBuilderOptions) => configureOptions?.Invoke(applicationOptions));
// TODO: consider centralizing this to DistributedApplicationFactory by default once consumers have a way to opt-out
// E.g., once https://github.com/dotnet/extensions/pull/5801 is released.
// Discussion: https://github.com/dotnet/aspire/pull/7335/files#r1936799460
builder.Services.ConfigureHttpClientDefaults(http => http.AddStandardResilienceHandler());
builder.Services.AddLogging(builder =>
{
if (testOutputHelper is not null)
builder.AddXUnit(testOutputHelper);
else
builder.AddXUnit();
if (Environment.GetEnvironmentVariable("RUNNER_DEBUG") is not null && Environment.GetEnvironmentVariable("RUNNER_DEBUG") == "1")
builder.SetMinimumLevel(LogLevel.Trace);
else
builder.SetMinimumLevel(LogLevel.Information);
});
builder.WithTempAspireStore();
return builder;
}
}