AutoForge/src/tests/AutoForge.IntegrationTests/ProjectGraphTests.cs

116 lines
4.6 KiB
C#

using System.Xml.Linq;
namespace AutoForge.IntegrationTests;
public sealed class ProjectGraphTests
{
[Fact]
public void Solution_ContainsOnlyTheExpectedProjectsAndDependencies()
{
var root = FindRepositoryRoot();
var solutionPath = Path.Combine(root, "src", "AutoForge.slnx");
var solution = XDocument.Load(solutionPath);
var projectPaths = solution.Descendants()
.Where(element => element.Name.LocalName == "Project")
.Select(element => (string?)element.Attribute("Path"))
.Where(path => path is not null)
.Select(path => Path.GetFullPath(Path.Combine(Path.GetDirectoryName(solutionPath)!, path!)))
.ToArray();
Assert.Equal(7, projectPaths.Length);
var projects = projectPaths.ToDictionary(
path => Path.GetFileNameWithoutExtension(path),
LoadProject,
StringComparer.Ordinal);
Assert.Equal(
new[]
{
"AutoForge.Application",
"AutoForge.Client",
"AutoForge.Components",
"AutoForge.Core",
"AutoForge.IntegrationTests",
"AutoForge.UI",
"AutoForge.UnitTests"
},
projects.Keys.Order(StringComparer.Ordinal));
AssertReferences(projects, "AutoForge.Core");
AssertReferences(projects, "AutoForge.Application", "AutoForge.Core");
AssertReferences(projects, "AutoForge.Components");
AssertReferences(projects, "AutoForge.UI", "AutoForge.Application", "AutoForge.Components");
AssertReferences(projects, "AutoForge.Client", "AutoForge.Application", "AutoForge.UI");
AssertReferences(
projects,
"AutoForge.UnitTests",
"AutoForge.Application",
"AutoForge.Components",
"AutoForge.Core");
AssertReferences(
projects,
"AutoForge.IntegrationTests",
"AutoForge.Application",
"AutoForge.Components",
"AutoForge.Core",
"AutoForge.UI");
var executables = projects
.Where(pair => GetProperty(pair.Value, "OutputType") == "Exe")
.Select(pair => pair.Key)
.ToArray();
Assert.Equal(["AutoForge.Client"], executables);
Assert.Equal("net10.0-windows10.0.26100.0", GetProperty(projects["AutoForge.Client"], "TargetFramework"));
Assert.Equal("true", GetProperty(projects["AutoForge.Client"], "UseMaui"));
Assert.Null(GetProperty(projects["AutoForge.Client"], "IsTestProject"));
Assert.Equal("net10.0", GetProperty(projects["AutoForge.UI"], "TargetFramework"));
Assert.Equal("net10.0", GetProperty(projects["AutoForge.Components"], "TargetFramework"));
}
private static string FindRepositoryRoot()
{
for (var directory = new DirectoryInfo(AppContext.BaseDirectory);
directory is not null;
directory = directory.Parent)
{
if (File.Exists(Path.Combine(directory.FullName, "src", "AutoForge.slnx")))
{
return directory.FullName;
}
}
throw new DirectoryNotFoundException("Could not locate the AutoForge solution from the test output directory.");
}
private static XDocument LoadProject(string projectPath) => XDocument.Load(projectPath);
private static void AssertReferences(
IReadOnlyDictionary<string, XDocument> projects,
string projectName,
params string[] expectedReferences)
{
var document = projects[projectName];
var projectPath = Path.Combine(
FindRepositoryRoot(),
"src",
projectName switch
{
"AutoForge.UnitTests" or "AutoForge.IntegrationTests" =>
Path.Combine("tests", projectName, $"{projectName}.csproj"),
_ => Path.Combine(projectName, $"{projectName}.csproj")
});
var actualReferences = document.Descendants()
.Where(element => element.Name.LocalName == "ProjectReference")
.Select(element => Path.GetFileNameWithoutExtension(
Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectPath)!, (string)element.Attribute("Include")!))))
.Order(StringComparer.Ordinal)
.ToArray();
Assert.Equal(expectedReferences.Order(StringComparer.Ordinal), actualReferences);
}
private static string? GetProperty(XDocument project, string propertyName) =>
project.Descendants().FirstOrDefault(element => element.Name.LocalName == propertyName)?.Value;
}