Skip to content

Commit 05b75fb

Browse files
committed
Borrowing more code from the aspire repo to try and make tests easier
1 parent a9b1561 commit 05b75fb

File tree

6 files changed

+74
-9
lines changed

6 files changed

+74
-9
lines changed

tests/Aspire.CommunityToolkit.Hosting.Java.Tests/JavaHostingComponentTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using Aspire.CommunityToolkit.Testing;
22
using FluentAssertions;
3-
using Microsoft.TestUtilities;
3+
using Aspire.Components.Common.Tests;
44

55
namespace Aspire.CommunityToolkit.Hosting.Java.Tests;
66

77
#pragma warning disable CTASPIRE001
8+
[RequiresDocker]
89
public class JavaHostingComponentTests(AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Java_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Java_AppHost>>
910
{
10-
[ConditionalTheory]
11-
[OSSkipCondition(OperatingSystems.Windows)]
11+
[Theory]
1212
[InlineData("containerapp")]
1313
[InlineData("executableapp")]
1414
public async Task AppResourceWillRespondWithOk(string resourceName)

tests/Aspire.CommunityToolkit.Hosting.Ollama.Tests/AppHostTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using Aspire.CommunityToolkit.Testing;
2+
using Aspire.Components.Common.Tests;
23
using FluentAssertions;
3-
using Microsoft.TestUtilities;
44

55
namespace Aspire.CommunityToolkit.Hosting.Ollama.Tests;
66

7+
[RequiresDocker]
78
public class AppHostTests(AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Ollama_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.Aspire_CommunityToolkit_Hosting_Ollama_AppHost>>
89
{
9-
[ConditionalTheory]
10-
[OSSkipCondition(OperatingSystems.Windows)]
10+
[Theory]
1111
[InlineData("ollama")]
1212
[InlineData("ollama-openwebui")]
1313
public async Task ResourceStartsAndRespondsOk(string resourceName)

tests/Aspire.CommunityToolkit.Testing/AspireIntegrationTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Aspire.Hosting;
1+
using Aspire.Components.Common.Tests;
2+
using Aspire.Hosting;
23
using Aspire.Hosting.ApplicationModel;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Logging;
@@ -32,7 +33,11 @@ protected override void OnBuilderCreated(DistributedApplicationBuilder applicati
3233
base.OnBuilderCreated(applicationBuilder);
3334
}
3435

35-
public async Task InitializeAsync() => await StartAsync();
36+
public async Task InitializeAsync()
37+
{
38+
if (RequiresDockerAttribute.IsSupported)
39+
await StartAsync();
40+
}
3641

3742
async Task IAsyncLifetime.DisposeAsync()
3843
{
@@ -42,7 +47,7 @@ async Task IAsyncLifetime.DisposeAsync()
4247
}
4348
catch (Exception)
4449
{
45-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI")))
50+
if (RequiresDockerAttribute.IsSupported)
4651
{
4752
// GitHub Actions Windows runners don't support Linux Docker containers, which can result in a bunch of false errors, even if we try to skip the test run, so we only really want to throw
4853
// if we're on a non-Windows runner or if we're on a Windows runner but not in a CI environment
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Aspire.Components.Common.Tests;
2+
3+
public static class PlatformDetection
4+
{
5+
public static bool IsRunningOnCI => Environment.GetEnvironmentVariable("CI") is not null;
6+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// Copied from: https://github.com/dotnet/aspire/blob/d31331d6132aeb22940dcd8834344956ba811373/tests/Aspire.Components.Common.Tests/RequiresDockerAttribute.cs
5+
6+
using Xunit.Sdk;
7+
8+
namespace Aspire.Components.Common.Tests;
9+
10+
[TraitDiscoverer("Aspire.Components.Common.Tests.RequiresDockerDiscoverer", "Aspire.Components.Common.Tests")]
11+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
12+
public class RequiresDockerAttribute : Attribute, ITraitAttribute
13+
{
14+
// This property is `true` when docker is *expected* to be available.
15+
//
16+
// A hard-coded *expected* value is used here to ensure that docker
17+
// dependent tests *fail* if docker is not available/usable in an environment
18+
// where it is expected to be available. A run-time check would allow tests
19+
// to fail silently, which is not desirable.
20+
//
21+
// scenarios:
22+
// - Windows: assume installed only for *local* runs as docker isn't supported on CI yet
23+
// - https://github.com/dotnet/aspire/issues/4291
24+
// - Linux - Local, or CI: always assume that docker is installed
25+
public static bool IsSupported =>
26+
!OperatingSystem.IsWindows() ||
27+
!PlatformDetection.IsRunningOnCI;
28+
29+
public string? Reason { get; init; }
30+
public RequiresDockerAttribute(string? reason = null)
31+
{
32+
Reason = reason;
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// Copied from: https://github.com/dotnet/aspire/blob/d31331d6132aeb22940dcd8834344956ba811373/tests/Aspire.Components.Common.Tests/RequiresDockerDiscoverer.cs
5+
6+
using Xunit.Abstractions;
7+
using Xunit.Sdk;
8+
9+
namespace Aspire.Components.Common.Tests;
10+
11+
public class RequiresDockerDiscoverer : ITraitDiscoverer
12+
{
13+
public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
14+
{
15+
if (!RequiresDockerAttribute.IsSupported)
16+
{
17+
yield return new KeyValuePair<string, string>("category", "failing");
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)