-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathValidateReferencedPartials.cs
66 lines (59 loc) · 2.65 KB
/
ValidateReferencedPartials.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using NUnit.Framework.Internal;
namespace IntegrityTests
{
internal class ValidateReferencedPartials
{
[Test]
public void ReferencedPartialsAreUsed()
{
var allErrors = new Dictionary<string, List<string>>();
var allPartials = Directory.GetFiles(TestSetup.DocsRootPath, "*.partial.md", SearchOption.AllDirectories);
var allArticles = Directory.GetFiles(TestSetup.DocsRootPath, "*.md", SearchOption.AllDirectories).Except(allPartials);
foreach (var articlePath in allArticles)
{
//Skips the readme file because it references a partial as instructions
if (articlePath.Contains("README.md"))
{
continue;
}
var fileName = Path.GetFileNameWithoutExtension(articlePath);
string text = File.ReadAllText(articlePath);
string searchPattern = @"^(?i)partial:\s*[a-zA-Z0-9-]*";
string replacePattern = @"(?i)partial:\s*";
var matches = Regex.Matches(text, searchPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
var errors = new List<string>();
foreach (var match in matches.Cast<Match>())
{
var partialName = Regex.Replace(match.Value, replacePattern, "").Trim();
if (!allPartials.Any(x => x.Contains(fileName + "_" + partialName, StringComparison.OrdinalIgnoreCase)))
{
errors.Add($" - '{match.Value}' at position ({match.Index})");
}
}
if (errors.Count > 0)
{
allErrors.TryAdd(articlePath, errors);
}
}
if (allErrors.Count > 0)
{
StringBuilder stringBuilder = new();
stringBuilder.Append($"Found ({allErrors.Count}) markdown file(s) that had references to partials that do not have a corresponding partial file to be used. Here are the file paths and partial references that were not used.\r\n");
foreach (var error in allErrors)
{
stringBuilder.AppendLine()
.AppendLine($" > {error.Key.ToString()}")
.AppendLine($"{string.Join(Environment.NewLine, error.Value)}");
}
Assert.Fail(stringBuilder.ToString());
}
}
}
}