-
Notifications
You must be signed in to change notification settings - Fork 1k
/
GivenThatWeHaveErrorCodes.cs
125 lines (110 loc) · 4.18 KB
/
GivenThatWeHaveErrorCodes.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
122
123
124
125
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;
using FluentAssertions;
using Xunit;
namespace Microsoft.NET.Build.Tasks.UnitTests
{
public class GivenThatWeHaveErrorCodes
{
private const int _firstCode = 1001;
private static readonly IReadOnlyList<int> _deletedCodes = new int[]
{
1026,
1027,
1033,
1034,
1035,
1036,
1037,
1038,
1039,
1040,
1041,
1062,
1066,
1101,
1108,
1180,
1182,
1183,
1190,
1192
};
//ILLink lives in other repos and violated the _info requirement for no error code
//Adding them to an exclusion list as it's difficult and not worth it to unwind
private static readonly IReadOnlyList<string> _infoExceptions = new string[]
{
"ILLinkRunning",
"ILLinkOptimizedAssemblies"
};
[Fact]
public void ThereAreNoGapsDuplicatesOrIncorrectlyFormattedCodes()
{
var codes = new HashSet<int>(_deletedCodes);
foreach (var (key, message) in GetMessages())
{
var match = Regex.Match(message, "^NETSDK([0-9]{4}): ");
if (key.EndsWith("_Info"))
{
match.Success
.Should()
.BeFalse(because: "informational messages should not have error codes.");
}
else
{
if (!_infoExceptions.Contains(key))
{
match.Success
.Should()
.BeTrue(because: $"all non-informational should have correctly formatted error codes ({key} does not).");
int code = int.Parse(match.Groups[1].Value);
codes.Add(code)
.Should()
.BeTrue(because: $"error codes should not be duplicated (NETSDK{code} is used more than once)");
}
}
}
for (int i = 0; i < codes.Count; i++)
{
int code = _firstCode + i;
codes.Contains(code)
.Should()
.BeTrue(because: $"error codes should not be skipped (NETSDK{code} was not found; add to the deleted codes list if intentionally deleted)");
}
}
[Fact]
public void ResxIsCommentedWithCorrectStrBegin()
{
var doc = XDocument.Load("Strings.resx");
var ns = doc.Root.Name.Namespace;
foreach (var data in doc.Root.Elements(ns + "data"))
{
var name = data.Attribute("name").Value;
var value = data.Element(ns + "value").Value;
var comment = data.Element(ns + "comment")?.Value ?? "";
var prefix = value.Substring(0, value.IndexOf(' '));
if (name.EndsWith("_Info"))
{
comment.Should().NotContain("StrBegin",
because: "informational messages should not have error codes.");
}
else if (!_infoExceptions.Contains(name))
{
comment.Should().StartWith($@"{{StrBegin=""{prefix} ""}}",
because: $"localization instructions should indicate invariant error code as preceding translatable message.");
}
}
}
private static IEnumerable<(string key, string message)> GetMessages()
{
var set = Strings.ResourceManager.GetResourceSet(
CultureInfo.InvariantCulture,
createIfNotExists: true,
tryParents: false);
return set.Cast<DictionaryEntry>().Select(e => ((string)e.Key, (string)e.Value));
}
}
}