|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Xml.Linq;
using Aspire.Cli.Configuration;
using Aspire.Cli.Projects;
namespace Aspire.Cli.Tests.Projects;
public class PrebuiltAppHostServerTests
{
[Fact]
public void GenerateIntegrationProjectFile_WithPackagesOnly_ProducesPackageReferences()
{
var packageRefs = new List<IntegrationReference>
{
IntegrationReference.FromPackage("Aspire.Hosting", "13.2.0"),
IntegrationReference.FromPackage("Aspire.Hosting.Redis", "13.2.0")
};
var projectRefs = new List<IntegrationReference>();
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile(packageRefs, projectRefs, "/tmp/libs");
var doc = XDocument.Parse(xml);
var packageElements = doc.Descendants("PackageReference").ToList();
Assert.Equal(2, packageElements.Count);
Assert.Contains(packageElements, e => e.Attribute("Include")?.Value == "Aspire.Hosting" && e.Attribute("Version")?.Value == "13.2.0");
Assert.Contains(packageElements, e => e.Attribute("Include")?.Value == "Aspire.Hosting.Redis" && e.Attribute("Version")?.Value == "13.2.0");
Assert.Empty(doc.Descendants("ProjectReference"));
}
[Fact]
public void GenerateIntegrationProjectFile_WithProjectRefsOnly_ProducesProjectReferences()
{
var packageRefs = new List<IntegrationReference>();
var projectRefs = new List<IntegrationReference>
{
IntegrationReference.FromProject("MyIntegration", "/path/to/MyIntegration.csproj")
};
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile(packageRefs, projectRefs, "/tmp/libs");
var doc = XDocument.Parse(xml);
var projectElements = doc.Descendants("ProjectReference").ToList();
Assert.Single(projectElements);
Assert.Equal("/path/to/MyIntegration.csproj", projectElements[0].Attribute("Include")?.Value);
Assert.Empty(doc.Descendants("PackageReference"));
}
[Fact]
public void GenerateIntegrationProjectFile_WithMixed_ProducesBothReferenceTypes()
{
var packageRefs = new List<IntegrationReference>
{
IntegrationReference.FromPackage("Aspire.Hosting", "13.2.0"),
IntegrationReference.FromPackage("Aspire.Hosting.Redis", "13.2.0")
};
var projectRefs = new List<IntegrationReference>
{
IntegrationReference.FromProject("MyIntegration", "/path/to/MyIntegration.csproj")
};
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile(packageRefs, projectRefs, "/tmp/libs");
var doc = XDocument.Parse(xml);
Assert.Equal(2, doc.Descendants("PackageReference").Count());
Assert.Single(doc.Descendants("ProjectReference"));
}
[Fact]
public void GenerateIntegrationProjectFile_SetsOutDir()
{
var packageRefs = new List<IntegrationReference>
{
IntegrationReference.FromPackage("Aspire.Hosting", "13.2.0")
};
var projectRefs = new List<IntegrationReference>();
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile(packageRefs, projectRefs, "/custom/output/path");
var doc = XDocument.Parse(xml);
var ns = doc.Root!.GetDefaultNamespace();
var outDir = doc.Descendants(ns + "OutDir").FirstOrDefault()?.Value;
Assert.Equal("/custom/output/path", outDir);
}
[Fact]
public void GenerateIntegrationProjectFile_HasCopyLocalLockFileAssemblies()
{
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile([], [], "/tmp/libs");
var doc = XDocument.Parse(xml);
var ns = doc.Root!.GetDefaultNamespace();
var copyLocal = doc.Descendants(ns + "CopyLocalLockFileAssemblies").FirstOrDefault()?.Value;
Assert.Equal("true", copyLocal);
}
[Fact]
public void GenerateIntegrationProjectFile_DisablesAnalyzersAndDocGen()
{
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile([], [], "/tmp/libs");
var doc = XDocument.Parse(xml);
var ns = doc.Root!.GetDefaultNamespace();
Assert.Equal("false", doc.Descendants(ns + "EnableNETAnalyzers").FirstOrDefault()?.Value);
Assert.Equal("false", doc.Descendants(ns + "GenerateDocumentationFile").FirstOrDefault()?.Value);
Assert.Equal("false", doc.Descendants(ns + "ProduceReferenceAssembly").FirstOrDefault()?.Value);
}
[Fact]
public void GenerateIntegrationProjectFile_TargetsNet10()
{
var xml = PrebuiltAppHostServer.GenerateIntegrationProjectFile([], [], "/tmp/libs");
var doc = XDocument.Parse(xml);
var ns = doc.Root!.GetDefaultNamespace();
Assert.Equal("net10.0", doc.Descendants(ns + "TargetFramework").FirstOrDefault()?.Value);
}
}
|