-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathPluginHelper.cs
240 lines (215 loc) · 9.08 KB
/
PluginHelper.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using Allure.Commons;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Bindings;
namespace Allure.SpecFlowPlugin
{
public static class PluginHelper
{
public static string IGNORE_EXCEPTION = "IgnoreException";
private static readonly ScenarioInfo emptyScenarioInfo = new ScenarioInfo(string.Empty, string.Empty);
private static FeatureInfo emptyFeatureInfo = new FeatureInfo(
CultureInfo.CurrentCulture, string.Empty, string.Empty);
internal static PluginConfiguration PluginConfiguration = GetConfiguration(AllureLifecycle.Instance.JsonConfiguration);
public static PluginConfiguration GetConfiguration(string allureConfiguration)
{
var config = new PluginConfiguration();
var specflowSection = JObject.Parse(allureConfiguration)["specflow"];
if (specflowSection != null)
config = specflowSection.ToObject<PluginConfiguration>();
return config;
}
internal static string GetFeatureContainerId(FeatureInfo featureInfo)
{
var id = (featureInfo != null)
? featureInfo.GetHashCode().ToString()
: emptyFeatureInfo.GetHashCode().ToString();
return id;
}
internal static string NewId()
{
return Guid.NewGuid().ToString("N");
}
internal static FixtureResult GetFixtureResult(HookBinding hook)
{
return new FixtureResult
{
name = $"{hook.Method.Name} [{hook.HookOrder}]"
};
}
internal static TestResult StartTestCase(string containerId, FeatureContext featureContext,
ScenarioContext scenarioContext)
{
var featureInfo = featureContext?.FeatureInfo ?? emptyFeatureInfo;
var scenarioInfo = scenarioContext?.ScenarioInfo ?? emptyScenarioInfo;
var tags = GetTags(featureInfo, scenarioInfo);
var testResult = new TestResult
{
uuid = NewId(),
historyId = scenarioInfo.Title,
name = scenarioInfo.Title,
fullName = scenarioInfo.Title,
labels = new List<Label>
{
Label.Thread(),
string.IsNullOrWhiteSpace(AllureLifecycle.Instance.AllureConfiguration.Title) ? Label.Host() : Label.Host(AllureLifecycle.Instance.AllureConfiguration.Title),
Label.Feature(featureInfo.Title)
}
.Union(tags.Item1).ToList(),
links = tags.Item2
};
AllureLifecycle.Instance.StartTestCase(containerId, testResult);
scenarioContext?.Set(testResult);
featureContext?.Get<HashSet<TestResult>>().Add(testResult);
return testResult;
}
internal static TestResult GetCurrentTestCase(ScenarioContext context)
{
context.TryGetValue(out TestResult testresult);
return testresult;
}
internal static TestResultContainer StartTestContainer(FeatureContext featureContext,
ScenarioContext scenarioContext)
{
var containerId = GetFeatureContainerId(featureContext?.FeatureInfo);
var scenarioContainer = new TestResultContainer
{
uuid = NewId()
};
AllureLifecycle.Instance.StartTestContainer(containerId, scenarioContainer);
scenarioContext?.Set(scenarioContainer);
featureContext?.Get<HashSet<TestResultContainer>>().Add(scenarioContainer);
return scenarioContainer;
}
internal static TestResultContainer GetCurrentTestConainer(ScenarioContext context)
{
context.TryGetValue(out TestResultContainer testresultContainer);
return testresultContainer;
}
internal static StatusDetails GetStatusDetails(Exception ex)
{
return new StatusDetails
{
message = GetFullExceptionMessage(ex),
trace = ex.ToString()
};
}
private static string GetFullExceptionMessage(Exception ex)
{
return ex.Message +
(!string.IsNullOrWhiteSpace(ex.InnerException?.Message) ?
$" -> {GetFullExceptionMessage(ex.InnerException)}" : string.Empty);
}
private static Tuple<List<Label>, List<Link>> GetTags(FeatureInfo featureInfo, ScenarioInfo scenarioInfo)
{
var result = Tuple.Create(new List<Label>(), new List<Link>());
var tags = scenarioInfo.Tags
.Union(featureInfo.Tags)
.Distinct(StringComparer.CurrentCultureIgnoreCase);
foreach (var tag in tags)
{
var tagValue = tag;
// link
if (TryUpdateValueByMatch(PluginConfiguration.links.link, ref tagValue))
{
result.Item2.Add(new Link() { name = tagValue, url = tagValue }); continue;
}
// issue
if (TryUpdateValueByMatch(PluginConfiguration.links.issue, ref tagValue))
{
result.Item2.Add(Link.Issue(tagValue, tagValue)); continue;
}
// tms
if (TryUpdateValueByMatch(PluginConfiguration.links.tms, ref tagValue))
{
result.Item2.Add(Link.Tms(tagValue, tagValue)); continue;
}
// parent suite
if (TryUpdateValueByMatch(PluginConfiguration.grouping.suites.parentSuite, ref tagValue))
{
result.Item1.Add(Label.ParentSuite(tagValue)); continue;
}
// suite
if (TryUpdateValueByMatch(PluginConfiguration.grouping.suites.suite, ref tagValue))
{
result.Item1.Add(Label.Suite(tagValue)); continue;
}
// sub suite
if (TryUpdateValueByMatch(PluginConfiguration.grouping.suites.subSuite, ref tagValue))
{
result.Item1.Add(Label.SubSuite(tagValue)); continue;
}
// epic
if (TryUpdateValueByMatch(PluginConfiguration.grouping.behaviors.epic, ref tagValue))
{
result.Item1.Add(Label.Epic(tagValue)); continue;
}
// story
if (TryUpdateValueByMatch(PluginConfiguration.grouping.behaviors.story, ref tagValue))
{
result.Item1.Add(Label.Story(tagValue)); continue;
}
// package
if (TryUpdateValueByMatch(PluginConfiguration.grouping.packages.package, ref tagValue))
{
result.Item1.Add(Label.Package(tagValue)); continue;
}
// test class
if (TryUpdateValueByMatch(PluginConfiguration.grouping.packages.testClass, ref tagValue))
{
result.Item1.Add(Label.TestClass(tagValue)); continue;
}
// test method
if (TryUpdateValueByMatch(PluginConfiguration.grouping.packages.testMethod, ref tagValue))
{
result.Item1.Add(Label.TestMethod(tagValue)); continue;
}
// owner
if (TryUpdateValueByMatch(PluginConfiguration.labels.owner, ref tagValue))
{
result.Item1.Add(Label.Owner(tagValue)); continue;
}
// severity
if (TryUpdateValueByMatch(PluginConfiguration.labels.severity, ref tagValue) && Enum.TryParse(tagValue, out SeverityLevel level))
{
result.Item1.Add(Label.Severity(level)); continue;
}
// tag
result.Item1.Add(Label.Tag(tagValue));
}
return result;
}
private static bool TryUpdateValueByMatch(string expression, ref string value)
{
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(expression))
return false;
Regex regex = null;
try
{
regex = new Regex(expression, RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
}
catch (Exception)
{
return false;
}
if (regex == null)
return false;
if (regex.IsMatch(value))
{
var groups = regex.Match(value).Groups;
if (groups?.Count == 1)
value = groups[0].Value;
else
value = groups[1].Value;
return true;
}
else
return false;
}
}
}