Skip to content

Commit a0f176e

Browse files
committed
Update
1 parent 7032b04 commit a0f176e

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

src/Aspire.Hosting/Dashboard/CommandsConfigurationExtensions.cs renamed to src/Aspire.Hosting/ApplicationModel/CommandsConfigurationExtensions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Aspire.Hosting.ApplicationModel;
54
using Aspire.Hosting.Dcp;
65
using Microsoft.Extensions.DependencyInjection;
76

8-
namespace Aspire.Hosting.Dashboard;
7+
namespace Aspire.Hosting.ApplicationModel;
98

109
internal static class CommandsConfigurationExtensions
1110
{
11+
internal const string StartType = "start";
12+
internal const string StopType = "stop";
13+
internal const string RestartType = "restart";
14+
1215
internal static IResourceBuilder<T> WithLifeCycleCommands<T>(this IResourceBuilder<T> builder) where T : IResource
1316
{
1417
builder.WithCommand(
15-
"start",
18+
StartType,
1619
"Start",
1720
context =>
1821
{
@@ -39,7 +42,7 @@ internal static IResourceBuilder<T> WithLifeCycleCommands<T>(this IResourceBuild
3942
isHighlighted: true);
4043

4144
builder.WithCommand(
42-
"stop",
45+
StopType,
4346
"Stop",
4447
context =>
4548
{
@@ -66,7 +69,7 @@ internal static IResourceBuilder<T> WithLifeCycleCommands<T>(this IResourceBuild
6669
isHighlighted: true);
6770

6871
builder.WithCommand(
69-
"restart",
72+
RestartType,
7073
"Restart",
7174
context =>
7275
{

src/Aspire.Hosting/ContainerResourceBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using Aspire.Hosting.ApplicationModel;
6-
using Aspire.Hosting.Dashboard;
76
using Aspire.Hosting.Utils;
87

98
namespace Aspire.Hosting;

src/Aspire.Hosting/ExecutableResourceBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Aspire.Hosting.ApplicationModel;
5-
using Aspire.Hosting.Dashboard;
65
using Aspire.Hosting.Publishing;
76
using Aspire.Hosting.Utils;
87

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
6+
namespace Aspire.Hosting.Tests;
7+
8+
public class ResourceCommandAnnotationTests
9+
{
10+
[Fact]
11+
public void AddContainer_HasKnownCommandAnnotations()
12+
{
13+
HasKnownCommandAnnotationsCore(builder => builder.AddContainer("name", "image"));
14+
}
15+
16+
[Fact]
17+
public void AddProject_HasKnownCommandAnnotations()
18+
{
19+
HasKnownCommandAnnotationsCore(builder => builder.AddProject("name", "path", o => o.ExcludeLaunchProfile = true));
20+
}
21+
22+
[Fact]
23+
public void AddExecutable_HasKnownCommandAnnotations()
24+
{
25+
HasKnownCommandAnnotationsCore(builder => builder.AddExecutable("name", "command", "workingDirectory"));
26+
}
27+
28+
[Theory]
29+
[InlineData(CommandsConfigurationExtensions.StartType, "Starting", ResourceCommandState.Disabled)]
30+
[InlineData(CommandsConfigurationExtensions.StartType, "Stopping", ResourceCommandState.Hidden)]
31+
[InlineData(CommandsConfigurationExtensions.StartType, "Running", ResourceCommandState.Hidden)]
32+
[InlineData(CommandsConfigurationExtensions.StartType, "Exited", ResourceCommandState.Enabled)]
33+
[InlineData(CommandsConfigurationExtensions.StopType, "Starting", ResourceCommandState.Hidden)]
34+
[InlineData(CommandsConfigurationExtensions.StopType, "Stopping", ResourceCommandState.Disabled)]
35+
[InlineData(CommandsConfigurationExtensions.StopType, "Running", ResourceCommandState.Enabled)]
36+
[InlineData(CommandsConfigurationExtensions.StopType, "Exited", ResourceCommandState.Hidden)]
37+
[InlineData(CommandsConfigurationExtensions.RestartType, "Starting", ResourceCommandState.Disabled)]
38+
[InlineData(CommandsConfigurationExtensions.RestartType, "Stopping", ResourceCommandState.Disabled)]
39+
[InlineData(CommandsConfigurationExtensions.RestartType, "Running", ResourceCommandState.Enabled)]
40+
[InlineData(CommandsConfigurationExtensions.RestartType, "Exited", ResourceCommandState.Disabled)]
41+
public void LifeCycleCommands_CommandState(string commandType, string resourceState, ResourceCommandState commandState)
42+
{
43+
// Arrange
44+
var builder = DistributedApplication.CreateBuilder();
45+
var resourceBuilder = builder.AddContainer("name", "image");
46+
47+
var startCommand = resourceBuilder.Resource.Annotations.OfType<ResourceCommandAnnotation>().Single(a => a.Type == commandType);
48+
49+
// Act
50+
var state = startCommand.UpdateState(new UpdateCommandStateContext
51+
{
52+
ResourceSnapshot = new CustomResourceSnapshot
53+
{
54+
Properties = [],
55+
ResourceType = "test",
56+
State = resourceState
57+
}
58+
});
59+
60+
// Assert
61+
Assert.Equal(commandState, state);
62+
}
63+
64+
private static void HasKnownCommandAnnotationsCore<T>(Func<IDistributedApplicationBuilder, IResourceBuilder<T>> createResourceBuilder) where T : IResource
65+
{
66+
// Arrange
67+
var builder = DistributedApplication.CreateBuilder();
68+
69+
// Act
70+
var resourceBuilder = createResourceBuilder(builder);
71+
72+
// Assert
73+
var commandAnnotations = resourceBuilder.Resource.Annotations.OfType<ResourceCommandAnnotation>().ToList();
74+
Assert.Collection(commandAnnotations,
75+
a => Assert.Equal(CommandsConfigurationExtensions.StartType, a.Type),
76+
a => Assert.Equal(CommandsConfigurationExtensions.StopType, a.Type),
77+
a => Assert.Equal(CommandsConfigurationExtensions.RestartType, a.Type));
78+
}
79+
}

tests/Aspire.Hosting.Tests/ResourceExtensionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Xunit;
55

66
namespace Aspire.Hosting.Tests;
7+
78
public class ResourceExtensionsTests
89
{
910
[Fact]

0 commit comments

Comments
 (0)