forked from Particular/docs.particular.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferenceVersions.cs
34 lines (31 loc) · 1.2 KB
/
ReferenceVersions.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
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using NUnit.Framework;
namespace IntegrityTests
{
public class ReferenceVersions
{
[Test]
public void MustScopeReferencesToAMajorVersion()
{
new TestRunner("*.csproj", "Package References cannot have Version=\"*\" or restore will sometimes fail and yield old, incorrect, or mismatched versions.")
.Run(projectFilePath =>
{
var xdoc = XDocument.Load(projectFilePath);
var nsMgr = new XmlNamespaceManager(new NameTable());
var query = "/Project/ItemGroup/PackageReference[@Version='*']";
var xmlnsAtt = xdoc.Root.Attribute("xmlns");
if (xmlnsAtt != null)
{
query = "/x:Project/x:ItemGroup/x:PackageReference[@Version='*']";
var xmlns = xmlnsAtt.Value;
nsMgr.AddNamespace("x", xmlns);
}
var badPackageRefs = xdoc.XPathSelectElements(query, nsMgr);
return !badPackageRefs.Any();
});
}
}
}