-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigurableProgressNotifier.cs
275 lines (229 loc) · 10.6 KB
/
ConfigurableProgressNotifier.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using LightBDD.Core.ExecutionContext;
using LightBDD.Core.Formatting;
using LightBDD.Core.Metadata;
using LightBDD.Core.Notification;
using LightBDD.Core.Notification.Events;
using LightBDD.Core.Results;
using LightBDD.Core.Results.Parameters.Tabular;
using LightBDD.Core.Results.Parameters.Trees;
using LightBDD.XUnit2;
namespace LightBDD.Contrib.ProgressNotifierEnhancements;
/// <summary>
/// ProgressNotifier that avoids having the output of the tests repeat the step name each time
/// </summary>
public class ConfigurableProgressNotifier : IProgressNotifier
{
private readonly Action<string> _onNotify;
private readonly Action<string> _defaultOnNotify = x =>
ScenarioExecutionContext.GetCurrentScenarioFixtureIfPresent<ITestOutputProvider>()?.TestOutput.WriteLine(x);
/// <summary>
/// When set to false it will not write an additional line saying "Passed after XXXms" but will continue to write a message if it fails etc
/// </summary>
public bool WriteSuccessMessageForBasicSteps { get; set; }
/// <summary>
/// Include the final step number in each step notification. eg It will say "STEP 1.3:" instead of "STEP 1.3/1.5" (where 1.5 is the final step)
/// </summary>
public bool ShowFinalStepWithEachStep { get; set; }
/// <summary>
/// The number of spaces to indent each Sub-Step.
/// Default is '4'.
/// </summary>
public int IndentLength { get; set; } = 4;
/// <summary>
/// IncludeAsPrefix: Includes the word 'STEP' and step number as a prefix with a colon to each notification eg 'Step 1.1: Given this thing'
/// IncludeAsSuffix: Includes the word 'STEP' and step number as a suffix in brackets to each notification eg 'Given this thing (Step 1.1)'
/// Exclude: Don't include the word 'STEP' and step number in each notification
/// </summary>
public StepWordAndStepNumberBehaviour StepWordAndStepNumberOnStart { get; set; } = StepWordAndStepNumberBehaviour.IncludeAsSuffix;
/// <summary>
/// IncludeAsPrefix: Includes the word 'STEP' and step number as a prefix with a colon to each notification eg 'Step 1.1: Given this thing'
/// IncludeAsSuffix: Includes the word 'STEP' and step number as a suffix in brackets to each notification eg 'Given this thing (Step 1.1)'
/// Exclude: Don't include the word 'STEP' and step number in each notification
/// </summary>
public StepWordAndStepNumberBehaviour StepWordAndStepNumberOnFinish { get; set; } = StepWordAndStepNumberBehaviour.IncludeAsSuffix;
/// <summary>
/// Includes the step name in the notification when the step finishes
/// </summary>
public bool IncludeStepNameOnFinish { get; set; } = false;
/// <summary>
/// Include '...' after each step name in the step start notification.
/// Defaults to false.
/// </summary>
public bool IncludeEllipsisAfterStep { get; set; } = false;
/// <summary>
/// Initializes the notifier with <paramref name="onNotify"/> actions that will be used to delegate the rendered notification text.
/// </summary>
/// <param name="onNotify"></param>
public ConfigurableProgressNotifier(params Action<string>[] onNotify)
{
_onNotify = onNotify.Any() ? onNotify.Aggregate((current, next) => current + next) : _defaultOnNotify;
}
/// <summary>
/// Notifies that scenario has started.
/// </summary>
/// <param name="scenario">Scenario info.</param>
public virtual void NotifyScenarioStart(IScenarioInfo scenario)
{
_onNotify($"SCENARIO: {FormatLabels(scenario.Labels)}{scenario.Name}");
}
/// <summary>
/// Notifies that scenario has finished.
/// </summary>
/// <param name="scenario">Scenario result.</param>
public virtual void NotifyScenarioFinished(IScenarioResult scenario)
{
var scenarioText = scenario.ExecutionTime != null
? $" SCENARIO RESULT: {scenario.Status} after {scenario.ExecutionTime.Duration.FormatPretty()}"
: $" SCENARIO RESULT: {scenario.Status}";
var scenarioDetails = !string.IsNullOrWhiteSpace(scenario.StatusDetails)
? $"{Environment.NewLine} {scenario.StatusDetails.Replace(Environment.NewLine, Environment.NewLine + " ")}"
: string.Empty;
_onNotify(scenarioText + scenarioDetails);
}
/// <summary>
/// Notifies that step has started.
/// </summary>
/// <param name="step">Step info.</param>
public virtual void NotifyStepStart(IStepInfo step)
{
var indentPrefix = GetIndentPrefix(step);
var notification = $"{indentPrefix}";
var stepWordAndStepNumber = GetStepWordAndStepNumber(step);
if (StepWordAndStepNumberOnStart == StepWordAndStepNumberBehaviour.IncludeAsPrefix)
notification += stepWordAndStepNumber + ": ";
notification += step.Name;
if (IncludeEllipsisAfterStep)
notification += "...";
if (StepWordAndStepNumberOnStart == StepWordAndStepNumberBehaviour.IncludeAsSuffix)
notification += $" ({stepWordAndStepNumber})";
_onNotify(notification);
}
private string GetStepWordAndStepNumber(IStepInfo step) => $"STEP {step.GroupPrefix}{step.Number}{(ShowFinalStepWithEachStep ? $"/{step.GroupPrefix}{step.Total}" : "")}";
/// <summary>
/// Notifies that step has finished.
/// </summary>
/// <param name="step">Step result.</param>
public virtual void NotifyStepFinished(IStepResult step)
{
var indentPrefix = GetIndentPrefix(step.Info);
var report = new List<string>();
if (WriteSuccessMessageForBasicSteps || step.Status != ExecutionStatus.Passed)
{
var notification = $"{indentPrefix}";
var stepWordAndStepNumber = GetStepWordAndStepNumber(step.Info);
if (StepWordAndStepNumberOnFinish == StepWordAndStepNumberBehaviour.IncludeAsPrefix)
notification += stepWordAndStepNumber + ": ";
if (IncludeStepNameOnFinish)
notification += step.Info.Name;
if (StepWordAndStepNumberOnFinish != StepWordAndStepNumberBehaviour.IncludeAsPrefix && indentPrefix != "")
notification += " =>";
notification += $" ({step.Status} after {step.ExecutionTime.Duration.FormatPretty()})";
if (StepWordAndStepNumberOnStart == StepWordAndStepNumberBehaviour.IncludeAsSuffix)
notification += $" ({stepWordAndStepNumber})";
report.Add(notification);
}
foreach (var parameter in step.Parameters)
{
switch (parameter.Details)
{
case ITabularParameterDetails table:
report.Add($"{indentPrefix} {parameter.Name}:");
report.Add(new TextTableRenderer(table).Render($"{indentPrefix} "));
break;
case ITreeParameterDetails tree:
report.Add($"{indentPrefix} {parameter.Name}:");
report.Add(TextTreeRenderer.Render($"{indentPrefix} ", tree));
break;
}
}
var message = string.Join(Environment.NewLine, report).TrimEnd();
if (!string.IsNullOrWhiteSpace(message))
_onNotify(string.Join(Environment.NewLine, report).TrimEnd());
}
/// <summary>
/// Notifies that step has been commented.
/// </summary>
/// <param name="step">Step info.</param>
/// <param name="comment">Comment.</param>
public virtual void NotifyStepComment(IStepInfo step, string comment)
{
_onNotify($"{GetIndentPrefix(step)} => /* {comment} */");
}
/// <summary>
/// Notifies that feature has started.
/// </summary>
/// <param name="feature">Feature info.</param>
public virtual void NotifyFeatureStart(IFeatureInfo feature)
{
_onNotify($"FEATURE: {FormatLabels(feature.Labels)}{feature.Name}{FormatDescription(feature.Description)}");
}
/// <summary>
/// Notifies that feature has finished.
/// </summary>
/// <param name="feature">Feature result.</param>
public virtual void NotifyFeatureFinished(IFeatureResult feature)
{
_onNotify($"FEATURE FINISHED: {feature.Info.Name}");
}
/// <inheritdoc />
public virtual void Notify(ProgressEvent e)
{
switch (e)
{
case FeatureFinished featureFinished:
NotifyFeatureFinished(featureFinished.Result);
break;
case FeatureStarting featureStarting:
NotifyFeatureStart(featureStarting.Feature);
break;
case ScenarioFinished scenarioFinished:
NotifyScenarioFinished(scenarioFinished.Result);
break;
case ScenarioStarting scenarioStarting:
NotifyScenarioStart(scenarioStarting.Scenario);
break;
case StepCommented stepCommented:
NotifyStepComment(stepCommented.Step, stepCommented.Comment);
break;
case StepFinished stepFinished:
NotifyStepFinished(stepFinished.Result);
break;
case StepStarting stepStarting:
NotifyStepStart(stepStarting.Step);
break;
case StepFileAttached stepFileAttached:
NotifyStepFileAttached(stepFileAttached.Step, stepFileAttached.Attachment);
break;
}
}
protected virtual void NotifyStepFileAttached(IStepInfo step, FileAttachment attachment)
{
_onNotify($" => 🔗{attachment.Name}: {attachment.FilePath}");
}
protected virtual string FormatDescription(string description)
{
return string.IsNullOrWhiteSpace(description)
? string.Empty
: $"{Environment.NewLine} {description.Replace(Environment.NewLine, Environment.NewLine + " ")}";
}
protected virtual string FormatLabels(IEnumerable<string> labels)
{
var joinedLabels = string.Join("][", labels);
if (joinedLabels != string.Empty)
joinedLabels = "[" + joinedLabels + "] ";
return joinedLabels;
}
protected virtual string GetIndentPrefix(IStepInfo step)
{
var parent = step.Parent;
var prefix = IndentLength < 2 ? " " : "";
while (parent is IStepInfo parentStep)
{
prefix += string.Concat(Enumerable.Repeat(" ", IndentLength));
parent = parentStep.Parent;
}
prefix += string.Concat(Enumerable.Repeat(" ", IndentLength));
return prefix;
}
private static readonly string[] GivenWhenThenAndBut = { "GIVEN", "WHEN", "THEN", "AND", "BUT" };
}