// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Aspire.TestUtilities;
using Microsoft.Playwright;
using Xunit;
namespace Aspire.Templates.Tests;
public partial class TemplateTestsBase
{
[GeneratedRegex(@"^\s*//")]
private static partial Regex CommentLineRegex();
// Regex is from src/Aspire.Hosting.AppHost/build/Aspire.Hosting.AppHost.in.targets - _GeneratedClassNameFixupRegex
[GeneratedRegex(@"(((?<=\.)|^)(?=\d)|\W)")]
private static partial Regex GeneratedClassNameFixupRegex();
private static Lazy<IBrowser> Browser => new(CreateBrowser);
private static readonly XmlWriterSettings s_xmlWriterSettings = new() { ConformanceLevel = ConformanceLevel.Fragment };
protected readonly TestOutputWrapper _testOutput;
public static readonly string[] TestFrameworkTypes = ["none", "mstest", "nunit", "xunit.net"];
public TemplateTestsBase(ITestOutputHelper testOutput)
{
_testOutput = new TestOutputWrapper(testOutput);
}
private static IBrowser CreateBrowser()
{
var t = Task.Run(async () => await PlaywrightProvider.CreateBrowserAsync());
// default timeout for playwright.Chromium.LaunchAsync is 30secs,
// so using a timeout here as a fallback
if (!t.Wait(45 * 1000))
{
throw new TimeoutException("Browser creation timed out");
}
return t.Result;
}
public async Task<string> CreateAndAddTestTemplateProjectAsync(
string id,
string testTemplateName,
AspireProject project,
TestTargetFramework? tfm = null,
BuildEnvironment? buildEnvironment = null,
string? extraArgs = null,
Func<AspireProject, Task>? onBuildAspireProject = null,
string? overrideRootDir = null)
{
buildEnvironment ??= BuildEnvironment.ForDefaultFramework;
var tmfArg = tfm is not null ? $"-f {tfm.Value.ToTFMString()}" : "";
string rootDirToUse = overrideRootDir ?? project.RootDir;
// Add test project
var testProjectName = $"{id}.{FixupSymbolName(testTemplateName)}Tests";
using var newTestCmd = new DotNetNewCommand(
_testOutput,
label: $"new-test-{testTemplateName}",
buildEnv: buildEnvironment)
.WithWorkingDirectory(rootDirToUse);
var res = await newTestCmd.ExecuteAsync($"{testTemplateName} {tmfArg} -o \"{testProjectName}\" {extraArgs}");
res.EnsureSuccessful();
var testProjectDir = Path.Combine(rootDirToUse, testProjectName);
Assert.True(Directory.Exists(testProjectDir), $"Expected tests project at {testProjectDir}");
var testProjectPath = Path.Combine(testProjectDir, testProjectName + ".csproj");
Assert.True(File.Exists(testProjectPath), $"Expected tests project file at {testProjectPath}");
var appHostProjectName = Path.GetFileName(project.AppHostProjectDirectory)!;
PrepareTestCsFile(
id: project.Id,
projectDir: testProjectDir,
appHostProjectName: appHostProjectName,
testTemplateName: testTemplateName);
PrepareTestProject(
project: project,
projectPath: testProjectPath,
appHostProjectName: appHostProjectName);
return testProjectDir;
static void PrepareTestProject(AspireProject project, string projectPath, string appHostProjectName)
{
// Insert <ProjectReference Include="$(MSBuildThisFileDirectory)..\aspire-starter0.AppHost\aspire-starter0.AppHost.csproj" /> in the project file
// taken from https://raw.githubusercontent.com/dotnet/templating/a325ffa18edd1590f9b340cf83d51d8eb567ebdc/src/Microsoft.TemplateEngine.Orchestrator.RunnableProjects/ValueForms/XmlEncodeValueFormFactory.cs
StringBuilder output = new();
using (var w = XmlWriter.Create(output, s_xmlWriterSettings))
{
w.WriteString(appHostProjectName);
}
var xmlEncodedId = output.ToString();
var projectReference = $@"<ProjectReference Include=""$(MSBuildThisFileDirectory)..\{xmlEncodedId}\{xmlEncodedId}.csproj"" />";
var newContents = File.ReadAllText(projectPath)
.Replace("</Project>", $"<ItemGroup>{projectReference}</ItemGroup>\n</Project>");
File.WriteAllText(projectPath, newContents);
}
static void PrepareTestCsFile(string id, string projectDir, string appHostProjectName, string testTemplateName)
{
var testCsPath = Path.Combine(projectDir, "IntegrationTest1.cs");
var sb = new StringBuilder();
// Uncomment everything after the marker line
var inTest = false;
var marker = testTemplateName switch
{
"aspire-nunit" or "aspire-nunit-9" => "// [Test]",
"aspire-mstest" or "aspire-mstest-9" => "// [TestMethod]",
"aspire-xunit" or "aspire-xunit-9" => "// [Fact]",
_ => throw new NotImplementedException($"Unknown test template: {testTemplateName}")
};
foreach (var line in File.ReadAllLines(testCsPath))
{
if (!inTest && line.Contains(marker))
{
inTest = true;
}
if (inTest && CommentLineRegex().IsMatch(line))
{
sb.AppendLine(CommentLineRegex().Replace(line, " "));
continue;
}
sb.AppendLine(line);
}
var classNameFromId = GeneratedClassNameFixupRegex().Replace(appHostProjectName, "_");
sb.Replace("Projects.MyAspireApp_AppHost", $"Projects.{classNameFromId}");
File.WriteAllText(testCsPath, sb.ToString());
}
}
public static Task<IBrowserContext> CreateNewBrowserContextAsync()
=> PlaywrightProvider.HasPlaywrightSupport
? Browser.Value.NewContextAsync(new BrowserNewContextOptions { IgnoreHTTPSErrors = true })
: throw new InvalidOperationException("Playwright is not available");
protected Task<ResourceRow[]> CheckDashboardHasResourcesAsync(WrapperForIPage dashboardPageWrapper, IEnumerable<ResourceRow> expectedResources, string logPath, int timeoutSecs = 120)
=> CheckDashboardHasResourcesAsync(dashboardPageWrapper, expectedResources, _testOutput, logPath, timeoutSecs);
protected static async Task<ResourceRow[]> CheckDashboardHasResourcesAsync(WrapperForIPage dashboardPageWrapper,
IEnumerable<ResourceRow> expectedResources,
ITestOutputHelper testOutput,
string logPath,
int timeoutSecs = 120)
{
try
{
return await DashboardResourceAssertions.CheckResourcesAsync(dashboardPageWrapper, expectedResources, testOutput, timeoutSecs);
}
catch
{
string screenshotPath = Path.Combine(logPath, $"dashboard-fail-{Guid.NewGuid().ToString()[..8]}.png");
await dashboardPageWrapper.Page.ScreenshotAsync(new PageScreenshotOptions { Path = screenshotPath });
testOutput.WriteLine($"Dashboard screenshot saved to {screenshotPath}");
throw;
}
}
// Don't fixup the prefix so it can have characters meant for testing, like spaces
public static string GetNewProjectId(string? prefix = null)
=> (prefix is null ? "" : $"{prefix}_") + FixupSymbolName(Path.GetRandomFileName());
public static IEnumerable<string> GetProjectNamesForTest()
{
if (!PlatformDetection.IsRunningPRValidation && !EnvironmentVariables.RunOnlyBasicBuildTemplatesTests)
{
// Avoid running these cases on PR validation
yield return "aspire_龦唉丂荳_㐁ᠭ_ᠤསྲིདخەلꌠ_1ᥕ项目1"; // sln should have UTF-8 byte order mark
yield return "aspire_starter.1period then.34letters";
yield return "aspire-starter & with.1";
// ActiveIssue: https://github.com/dotnet/aspnetcore/issues/56277
// yield return "aspire_😀";
}
// basic case
yield return "aspire";
}
public static async Task AssertStarterTemplateRunAsync(IBrowserContext? context, AspireProject project, string config, ITestOutputHelper _testOutput)
{
await project.StartAppHostAsync(extraArgs: [$"-c {config}"], noBuild: false);
if (context is not null)
{
var page = await project.OpenDashboardPageAsync(context);
await CheckDashboardHasResourcesAsync(
page,
StarterTemplateRunTestsBase<StarterTemplateFixture>.GetExpectedResources(project, hasRedisCache: false),
_testOutput,
project.LogPath).ConfigureAwait(false);
string apiServiceUrl = project.InfoTable["apiservice"].Endpoints[0].Uri;
await StarterTemplateRunTestsBase<StarterTemplateFixture>.CheckApiServiceWorksAsync(apiServiceUrl, _testOutput, project.LogPath);
string webFrontEnd = project.InfoTable["webfrontend"].Endpoints[0].Uri;
await StarterTemplateRunTestsBase<StarterTemplateFixture>.CheckWebFrontendWorksAsync(context, webFrontEnd, _testOutput, project.LogPath);
}
else
{
_testOutput.WriteLine($"Skipping playwright part of the test");
}
await project.StopAppHostAsync();
}
public static async Task<CommandResult?> AssertTestProjectRunAsync(string testProjectDirectory, string testType, ITestOutputHelper testOutput, string config = "Debug", int testRunTimeoutSecs = 3 * 60)
{
if (testType == "none")
{
Assert.False(Directory.Exists(testProjectDirectory), "Expected no tests project to be created");
return null;
}
else
{
Assert.True(Directory.Exists(testProjectDirectory), $"Expected tests project at {testProjectDirectory}");
// Build first, because `dotnet test` does not show test results if all the tests pass
using var buildCmd = new DotNetCommand(testOutput, label: $"test-{testType}")
.WithWorkingDirectory(testProjectDirectory)
.WithTimeout(TimeSpan.FromSeconds(testRunTimeoutSecs));
(await buildCmd.ExecuteAsync($"build -c {config}")).EnsureSuccessful();
// .. then test with --no-build
using var testCmd = new DotNetCommand(testOutput, label: $"test-{testType}")
.WithWorkingDirectory(testProjectDirectory)
.WithTimeout(TimeSpan.FromSeconds(testRunTimeoutSecs));
// MSTest uses an MTP executable. Run it directly to support SDKs 8 and 9 as well as
// SDK 10+, where MTP 2 no longer supports the VSTest-based dotnet test entry point.
// This also avoids imposing a global.json runner selection on generated projects.
// See https://learn.microsoft.com/dotnet/core/testing/microsoft-testing-platform-integration-dotnet-test.
var command = testType == "mstest" ? "run" : "test";
var testRes = (await testCmd.ExecuteAsync($"{command} -c {config} --no-build"))
.EnsureSuccessful();
if (testType == "mstest")
{
TestRunOutput.AssertSinglePassedMtpTest(testRes.Output);
}
else
{
Assert.Matches("Passed! * - Failed: *0, Passed: *1, Skipped: *0, Total: *1", testRes.Output);
}
return testRes;
}
}
public static TheoryData<string, string, TestSdk, TestTargetFramework, string?> TestDataForNewAndBuildTemplateTests(string templateName, string extraArgs) => new()
{
{ templateName, extraArgs, TestSdk.Net10, TestTargetFramework.Net10, null },
{ templateName, extraArgs, TestSdk.Net10, TestTargetFramework.Net11, "The current .NET SDK does not support targeting .NET 11.0" },
{ templateName, extraArgs, TestSdk.Net11, TestTargetFramework.Net10, null },
{ templateName, extraArgs, TestSdk.Net11, TestTargetFramework.Net11, null },
{ templateName, extraArgs, TestSdk.Net11WithAllSupportedRuntimes, TestTargetFramework.Net10, null },
{ templateName, extraArgs, TestSdk.Net11WithAllSupportedRuntimes, TestTargetFramework.Net11, null },
};
// Taken from dotnet/runtime src/tasks/Common/Utils.cs
private static readonly char[] s_charsToReplace = ['.', '-', '+', '<', '>'];
public static string FixupSymbolName(string name)
{
UTF8Encoding utf8 = new();
byte[] bytes = utf8.GetBytes(name);
StringBuilder sb = new();
foreach (byte b in bytes)
{
if ((b >= (byte)'0' && b <= (byte)'9') ||
(b >= (byte)'a' && b <= (byte)'z') ||
(b >= (byte)'A' && b <= (byte)'Z') ||
(b == (byte)'_'))
{
sb.Append((char)b);
}
else if (s_charsToReplace.Contains((char)b))
{
sb.Append('_');
}
else
{
sb.Append($"_{b:X}_");
}
}
return sb.ToString();
}
}