-
Notifications
You must be signed in to change notification settings - Fork 301
/
UpgradeGuides.cs
81 lines (71 loc) · 2.96 KB
/
UpgradeGuides.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
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace IntegrityTests
{
public class UpgradeGuides
{
static readonly IDeserializer deserializer;
static readonly Regex yamlSeparatorRegex = new("---");
static readonly string upgradesPathPart = $"{Path.DirectorySeparatorChar}upgrades{Path.DirectorySeparatorChar}";
static UpgradeGuides()
{
var builder = new DeserializerBuilder();
builder.IgnoreUnmatchedProperties();
builder.WithNamingConvention(CamelCaseNamingConvention.Instance);
deserializer = builder.Build();
}
[Test]
public void VerifyUpgradeGuidesDontDoubleMentionCoreVersions()
{
new TestRunner("*.md", "Mentioning the same core version twice in upgrade guide metadata is invalid")
.IgnoreSnippets()
.IgnoreTutorials()
.IgnoreRegex(@".*\.include\.md")
.IgnoreRegex(@".*\.partial\.md")
.Run(path =>
{
// This test is important because if the same Core version is entered twice (i.e. versions 7 and 7) then that
// upgrade guide would erroneously be added to the Core7to8 upgrade guide.
// NOTE: It may be possible to add this test to the engine in the future and then remove this integrity test.
try
{
if (!path.Contains(upgradesPathPart))
{
return true;
}
var markdown = File.ReadAllText(path);
var separatorMatches = yamlSeparatorRegex.Matches(markdown);
var yaml = markdown.Substring(separatorMatches[0].Index + 3, separatorMatches[1].Index - 3).Trim();
var metadata = deserializer.Deserialize<Metadata>(yaml);
var coreVersions = metadata?.UpgradeGuideCoreVersions;
if (coreVersions == null)
{
return true;
}
if (coreVersions.Length == 1)
{
return true;
}
if (coreVersions.Length != coreVersions.Distinct().Count())
{
return false;
}
return true;
}
catch (System.Exception x)
{
System.Console.WriteLine(x);
return false;
}
});
}
class Metadata
{
public int[] UpgradeGuideCoreVersions { get; set; }
}
}
}