File: TestServices\FakeFailingAppHostServerProject.cs
Web Access
Project: src\tests\Aspire.Cli.Tests\Aspire.Cli.Tests.csproj (Aspire.Cli.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Aspire.Cli.Configuration;
using Aspire.Cli.Projects;
 
namespace Aspire.Cli.Tests.TestServices;
 
/// <summary>
/// <see cref="IAppHostServerProject"/> whose <see cref="PrepareAsync"/> returns failure so
/// callers (e.g. <see cref="Aspire.Cli.Scaffolding.ScaffoldingService"/>) take the early-out
/// path without touching the network, the dotnet CLI, or template restore. Useful for tests
/// that exercise the channel/config write side-effects that happen BEFORE
/// <c>AppHostServerProject.PrepareAsync</c>.
/// </summary>
internal sealed class FakeFailingAppHostServerProject(string appDirectoryPath) : IAppHostServerProject, IDisposable
{
    public string AppDirectoryPath { get; } = appDirectoryPath;
 
    public bool Disposed { get; private set; }
 
    public string GetInstanceIdentifier() => AppDirectoryPath;
 
    public Task<AppHostServerPrepareResult> PrepareAsync(
        string sdkVersion,
        IEnumerable<IntegrationReference> integrations,
        string? requestedChannel = null,
        string? packageSourceOverride = null,
        CancellationToken cancellationToken = default) =>
        Task.FromResult(new AppHostServerPrepareResult(Success: false, Output: null));
 
    public Task<AppHostServerRunResult> RunAsync(
        int hostPid,
        IReadOnlyDictionary<string, string>? environmentVariables,
        string[]? additionalArgs,
        bool debug,
        AppHostServerRunControl? runControl) =>
        throw new NotSupportedException("Run should not be invoked when PrepareAsync fails.");
 
    public void Dispose()
    {
        Disposed = true;
    }
}