File: Helpers\GoAppHostTestHelpers.cs
Web Access
Project: src\tests\Aspire.Cli.EndToEnd.Tests\Aspire.Cli.EndToEnd.Tests.csproj (Aspire.Cli.EndToEnd.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using Xunit;
 
namespace Aspire.Cli.EndToEnd.Tests.Helpers;
 
/// <summary>
/// Edits the <c>apphost.go</c> generated by <c>aspire init</c> and the Go starter template.
/// </summary>
internal static class GoAppHostTestHelpers
{
    // Both templates build the AppHost on this tab-indented line, after the resources are declared.
    private const string BuildCall = "\tapp, err := builder.Build()";
 
    /// <summary>
    /// Inserts Go statements right before the AppHost is built. Builder calls record their errors on the
    /// builder instead of returning them, so any failure in the inserted statements is reported by the
    /// template's own error handling around <c>Build()</c>. Go ignores indentation, so the statements
    /// can be written unindented.
    /// </summary>
    internal static void InsertBeforeBuild(string appHostPath, string statements)
    {
        var content = File.ReadAllText(appHostPath);
        Assert.True(content.Contains(BuildCall, StringComparison.Ordinal), $"Expected '{BuildCall.Trim()}' in {appHostPath}");
 
        File.WriteAllText(appHostPath, content.Replace(BuildCall, $"{statements}\n\n{BuildCall}", StringComparison.Ordinal));
    }
}