Files

73 lines
2.5 KiB
C#

using System.Xml.Linq;
namespace ItWorx.KnowledgeHub.Architecture.Tests;
public sealed class ProjectReferenceTests
{
[Fact]
public void Domain_and_contracts_have_no_project_dependencies()
{
Assert.Empty(ReadProjectReferences("src/backend/ItWorx.KnowledgeHub.Domain/ItWorx.KnowledgeHub.Domain.csproj"));
Assert.Empty(ReadProjectReferences("src/backend/ItWorx.KnowledgeHub.Contracts/ItWorx.KnowledgeHub.Contracts.csproj"));
}
[Fact]
public void Application_depends_only_on_domain_and_contracts()
{
var references = ReadProjectReferences(
"src/backend/ItWorx.KnowledgeHub.Application/ItWorx.KnowledgeHub.Application.csproj");
Assert.Equal(
new[]
{
"ItWorx.KnowledgeHub.Contracts",
"ItWorx.KnowledgeHub.Domain"
},
references.Order(StringComparer.Ordinal));
}
[Fact]
public void Infrastructure_does_not_depend_on_api()
{
var references = ReadProjectReferences(
"src/backend/ItWorx.KnowledgeHub.Infrastructure/ItWorx.KnowledgeHub.Infrastructure.csproj");
Assert.DoesNotContain("ItWorx.KnowledgeHub.Api", references);
}
private static IReadOnlyCollection<string> ReadProjectReferences(string relativeProjectPath)
{
var repositoryRoot = FindRepositoryRoot();
var projectPath = Path.Combine(
repositoryRoot.FullName,
relativeProjectPath.Replace('/', Path.DirectorySeparatorChar));
var document = XDocument.Load(projectPath);
return document
.Descendants("ProjectReference")
.Select(element => element.Attribute("Include")?.Value)
.Where(value => !string.IsNullOrWhiteSpace(value))
.Select(value => Path.GetFileNameWithoutExtension(value!))
.ToArray();
}
private static DirectoryInfo FindRepositoryRoot()
{
DirectoryInfo? current = new DirectoryInfo(AppContext.BaseDirectory);
while (current is not null)
{
if (File.Exists(Path.Combine(current.FullName, "global.json"))
&& (File.Exists(Path.Combine(current.FullName, "CLAUDE.md"))
|| File.Exists(Path.Combine(current.FullName, "PUBLIC_SOURCE_EXPORT.md"))))
{
return current;
}
current = current.Parent;
}
throw new DirectoryNotFoundException("Could not locate the repository root.");
}
}