-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathValidateAliases.cs
98 lines (84 loc) · 3.71 KB
/
ValidateAliases.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using NUnit.Framework;
namespace IntegrityTests
{
public class ValidateAliases
{
[Test]
public void ValidateAliasesVersionMatchesPackageReferenceVersion()
{
new TestRunner("*.csproj", "Found alias version not matching package reference version. i.e. Project in `Core_8` should reference NServiceBus 8.*, while `Core_9` should reference NServiceBus 9.*")
.Run(projPath =>
{
// The NServiceBus.MessageInterfaces sample needs to reference two different versions of NServiceBus
if (projPath.Contains("samples") && projPath.Contains("message-assembly-sharing"))
{
return true;
}
var (versionedPath, aliasName, aliasVersion) = GetVersionedAliasPath(projPath);
if (versionedPath == null)
{
return true;
}
if (aliasVersion == "All")
{
return true;
}
// Temporarily skipping over, this probably means incorrect component names, but that's another test
if (!TestSetup.NugetAliases.TryGetValue(aliasName, out var folderPackageId))
{
return true;
}
foreach (var packageRef in QueryPackageRefs(projPath))
{
var packageId = packageRef.Attribute("Include")!.Value;
var packageVersion = packageRef.Attribute("Version")!.Value;
if (packageId == folderPackageId)
{
if (aliasVersion == "1" && packageVersion.StartsWith("0.")) continue; // Valid for previews
if (!packageVersion.StartsWith(aliasVersion))
{
return false; // TODO: Would be nice if could return `aliasVersion` and `packageVersion`
}
}
}
return true;
});
}
static (string path, string componentName, string version) GetVersionedAliasPath(string path)
{
var dirPath = Path.GetDirectoryName(path);
while (dirPath!.Length >= TestSetup.DocsRootPath.Length)
{
var dirName = Path.GetFileName(dirPath);
if (Regex.IsMatch(dirName, @"_(All|\d+(\.\d+)?)$"))
{
var underScoreIndex = dirName.LastIndexOf('_');
var componentName = dirName[..underScoreIndex];
return (dirPath, componentName, dirName[(underScoreIndex + 1)..]);
}
dirPath = Path.GetDirectoryName(dirPath);
}
return (null, null, null);
}
static IEnumerable<XElement> QueryPackageRefs(string projectPath)
{
var xdoc = XDocument.Load(projectPath);
var nsMgr = new XmlNamespaceManager(new NameTable());
var query = "/Project/ItemGroup/PackageReference";
var xmlnsAtt = xdoc.Root.Attribute("xmlns");
if (xmlnsAtt != null)
{
nsMgr.AddNamespace("x", xmlnsAtt.Value);
query = "/x:Project/x:ItemGroup/x:PackageReference";
}
var packageRefs = xdoc.XPathSelectElements(query, nsMgr);
return packageRefs;
}
}
}