70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using System.Xml.Linq;
|
|
|
|
namespace FinalFactory.Rendezvous.Tests.Contracts;
|
|
|
|
internal static class ContractTestFiles
|
|
{
|
|
public static string Read(string fileName) => File
|
|
.ReadAllText(Path.Combine(DirectoryFor(fileName), fileName))
|
|
.TrimEnd('\r', '\n');
|
|
|
|
public static string Directory
|
|
{
|
|
get
|
|
{
|
|
return VersionedDirectory(Property("RendezvousMajorVersion"));
|
|
}
|
|
}
|
|
|
|
public static string OpenApiDocument
|
|
{
|
|
get
|
|
{
|
|
string httpVersion = Property("HttpContractVersion");
|
|
return Path.Combine(RepositoryRoot, $"docs/api/rendezvous-v{httpVersion}.json");
|
|
}
|
|
}
|
|
|
|
private static string DirectoryFor(string fileName)
|
|
{
|
|
string property = fileName switch
|
|
{
|
|
"client-public-api.txt" or "contracts-public-api.txt" => "RendezvousMajorVersion",
|
|
"connection-ticket.json" => "ConnectionTicketFormatVersion",
|
|
_ when fileName.EndsWith(".hex", StringComparison.Ordinal) => "UdpContractVersion",
|
|
_ when fileName.EndsWith(".json", StringComparison.Ordinal) => "HttpContractVersion",
|
|
_ => throw new InvalidOperationException($"No contract version dimension maps {fileName}."),
|
|
};
|
|
return VersionedDirectory(Property(property));
|
|
}
|
|
|
|
private static string VersionedDirectory(string version) => Path.Combine(
|
|
RepositoryRoot,
|
|
$"tests/FinalFactory.Rendezvous.Tests/TestData/Contracts/v{version}");
|
|
|
|
private static string Property(string name)
|
|
{
|
|
XDocument versions = XDocument.Load(Path.Combine(RepositoryRoot, "eng/Versions.props"));
|
|
return versions.Descendants(name).Single().Value;
|
|
}
|
|
|
|
private static string RepositoryRoot
|
|
{
|
|
get
|
|
{
|
|
DirectoryInfo? directory = new(AppContext.BaseDirectory);
|
|
while (directory is not null)
|
|
{
|
|
if (File.Exists(Path.Combine(directory.FullName, "Rendezvous.slnx")))
|
|
{
|
|
return directory.FullName;
|
|
}
|
|
|
|
directory = directory.Parent;
|
|
}
|
|
|
|
throw new DirectoryNotFoundException("Could not locate repository root.");
|
|
}
|
|
}
|
|
}
|