| 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 { private const string BuildCall = " := 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); var buildCallIndex = content.IndexOf(BuildCall, StringComparison.Ordinal); Assert.True(buildCallIndex >= 0, $"Expected a builder.Build() assignment in {appHostPath}"); var lineStart = content.LastIndexOf('\n', buildCallIndex) + 1; File.WriteAllText(appHostPath, content.Insert(lineStart, $"{statements}\n\n")); } }