-
Notifications
You must be signed in to change notification settings - Fork 301
/
ProjectFrameworks.cs
121 lines (105 loc) · 4.31 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using NUnit.Framework;
namespace IntegrityTests
{
public class ProjectFrameworks
{
static readonly char[] separator = [';'];
[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(separator, StringSplitOptions.RemoveEmptyEntries);
return tfmList.Length > 1;
});
}
public static readonly string[] sdkProjectAllowedTfmList = ["net8.0", "net8.0-windows", "net6.0", "net6.0-windows", "net48", "netstandard2.0"];
static readonly string[] nonSdkProjectAllowedFrameworkList = ["v4.8"];
[Test]
public void RestrictTargetFrameworks()
{
var tfmString = string.Join(", ", sdkProjectAllowedTfmList);
new TestRunner("*.csproj", "Allowed target frameworks are: " + tfmString)
.Run(projectFilePath =>
{
XDocument xdoc = default;
try
{
xdoc = XDocument.Load(projectFilePath);
}
catch
{
return false;
}
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;
});
}
[Test]
public void SnippetsShouldNotBeMultiTargeted()
{
new TestRunner("*.csproj", "Snippets projects should not be multi-targeted")
.IgnoreSamples()
.IgnoreTutorials()
.Run(projectFilePath =>
{
var xdoc = XDocument.Load(projectFilePath);
if (xdoc.Root.Attribute("xmlns") != null)
{
return true;
}
var firstTargetFrameworksElement = xdoc.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks");
return firstTargetFrameworksElement is null;
});
}
}
}