-
Notifications
You must be signed in to change notification settings - Fork 301
/
ProjectFrameworks.cs
88 lines (79 loc) · 3.32 KB
/
ProjectFrameworks.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
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using NUnit.Framework;
namespace IntegrityTests
{
public class ProjectFrameworks
{
[Test]
public void TargetFrameworkElementShouldAgreeWithFrameworkCount()
{
new TestRunner("*.csproj", "Project files with <TargetFrameworks> element should list multiple frameworks")
.IgnoreSnippets()
.Run(projectFilePath =>
{
var xdoc = XDocument.Load(projectFilePath);
if (xdoc.Root.Attribute("xmlns") != null)
{
return true;
}
var firstTargetFrameworksElement = xdoc.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks");
if (firstTargetFrameworksElement == null)
{
return true;
}
var tfmList = firstTargetFrameworksElement.Value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
return tfmList.Length > 1;
});
}
static readonly string[] sdkProjectAllowedTfmList = new[] { "net6.0", "netcoreapp3.1", "net48", "netstandard2.0" };
static readonly string[] nonSdkProjectAllowedFrameworkList = new[] { "v4.8" };
[Test]
public void RestrictTargetFrameworks()
{
var tfmString = string.Join(", ", sdkProjectAllowedTfmList);
new TestRunner("*.csproj", "Allowed target frameworks are: " + tfmString)
.Run(projectFilePath =>
{
var xdoc = XDocument.Load(projectFilePath);
var xmlnsNode = xdoc.Root.Attribute("xmlns");
if (xmlnsNode != null)
{
var mgr = new XmlNamespaceManager(new NameTable());
mgr.AddNamespace("x", xmlnsNode.Value);
foreach (var node in xdoc.XPathSelectElements("/x:Project/x:PropertyGroup/x:TargetFramework", mgr))
{
if (!nonSdkProjectAllowedFrameworkList.Contains(node.Value))
{
return false;
}
}
}
else
{
foreach (var node in xdoc.XPathSelectElements("/Project/PropertyGroup/TargetFramework"))
{
if (!sdkProjectAllowedTfmList.Contains(node.Value))
{
return false;
}
}
foreach (var node in xdoc.XPathSelectElements("/Project/PropertyGroup/TargetFrameworks"))
{
foreach (var tfm in node.Value.Split(';'))
{
if (!sdkProjectAllowedTfmList.Contains(tfm))
{
return false;
}
}
}
}
return true;
});
}
}
}