forked from allure-framework/allure-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllureLifecycle.cs
698 lines (618 loc) · 21.7 KB
/
AllureLifecycle.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading;
using Allure.Net.Commons.Configuration;
using Allure.Net.Commons.Functions;
using Allure.Net.Commons.TestPlan;
using Allure.Net.Commons.Writer;
using Newtonsoft.Json.Linq;
#nullable enable
namespace Allure.Net.Commons;
/// <summary>
/// A facade that allows to control the Allure context, set up allure model
/// objects and emit output files.
/// </summary>
/// <remarks>
/// This class is primarily intended to be used by a test framework
/// integration. We don't advice to use it from test code unless strictly
/// necessary.<br></br>
/// NOTE: Modifications of the Allure context persist until either some
/// method has affect them, or the execution context is restored to the
/// point beyond the call that had introduced them.
/// </remarks>
public class AllureLifecycle
{
private readonly Dictionary<Type, ITypeFormatter> typeFormatters = new();
private static readonly Lazy<AllureLifecycle> instance =
new(Initialize);
/// <summary>
/// The list of the currently registered formatters used by Allure to
/// convert test and step arguments to strings.
/// </summary>
public IReadOnlyDictionary<Type, ITypeFormatter> TypeFormatters =>
new ReadOnlyDictionary<Type, ITypeFormatter>(typeFormatters);
readonly AsyncLocal<AllureContext> context = new();
readonly Lazy<AllureTestPlan> lazyTestPlan;
readonly IAllureResultsWriter writer;
internal IAllureResultsWriter Writer => this.writer;
/// <summary>
/// Protects mutations of shared allure model objects against data
/// races that may otherwise occur because of multithreaded access.
/// </summary>
readonly object modelMonitor = new();
/// <summary>
/// Captures the current value of Allure context.
/// </summary>
public AllureContext Context
{
get => this.context.Value ??= new AllureContext();
private set => this.context.Value = value;
}
/// <summary>
/// The current testplan. If no testplan was specified, the default one is
/// used that doesn't filter any test.
/// </summary>
public AllureTestPlan TestPlan { get => this.lazyTestPlan.Value; }
internal AllureLifecycle() : this(GetConfiguration())
{
}
internal AllureLifecycle(
Func<AllureConfiguration, IAllureResultsWriter> writerFactory
) : this(
GetConfiguration(),
writerFactory,
AllureTestPlan.FromEnvironment
)
{
}
internal AllureLifecycle(JObject config) : this(
config,
c => new FileSystemResultsWriter(c),
AllureTestPlan.FromEnvironment
)
{
}
internal AllureLifecycle(
JObject config,
Func<AllureConfiguration, IAllureResultsWriter> writerFactory,
Func<AllureTestPlan> testPlanFactory
)
{
JsonConfiguration = config.ToString();
AllureConfiguration = AllureConfiguration.ReadFromJObject(config);
writer = writerFactory(AllureConfiguration);
lazyTestPlan = new(testPlanFactory);
}
/// <summary>
/// The JSON representation of the current Allure configuration.
/// </summary>
public string JsonConfiguration { get; private set; }
/// <summary>
/// The current Allure configuration.
/// </summary>
public AllureConfiguration AllureConfiguration { get; }
/// <summary>
/// The full path to the Allure results directory.
/// </summary>
public string ResultsDirectory => writer.ToString();
/// <summary>
/// The current instance of the Allure lifecycle.
/// </summary>
public static AllureLifecycle Instance { get => instance.Value; }
/// <summary>
/// Registers a type formatter to be used when converting a test's or
/// step's argument to the string that will be included in the Allure
/// report.
/// </summary>
/// <typeparam name="T">
/// The type that the formatter converts. The formatter will be used for
/// arguments of that exact type. Otherwise, the argument will be converted
/// using JSON serialization.
/// </typeparam>
/// <param name="typeFormatter">The formatter instance.</param>
public void AddTypeFormatter<T>(TypeFormatter<T> typeFormatter) =>
AddTypeFormatterImpl(typeof(T), typeFormatter);
private void AddTypeFormatterImpl(Type type, ITypeFormatter formatter) =>
typeFormatters[type] = formatter;
/// <summary>
/// Binds the provided value as the current Allure context and executes
/// the specified function. The context is then restored to the initial
/// value. That allows the Allure context to bypass .NET execution
/// context boundaries.
/// </summary>
/// <param name="context">
/// A context that was previously captured with <see cref="Context"/>.
/// If it is null, the code is executed in the current context.
/// </param>
/// <param name="action">A code to run.</param>
/// <returns>The context after the code is executed.</returns>
public AllureContext RunInContext(
AllureContext? context,
Action action
)
{
if (context is null)
{
action();
return this.Context;
}
var originalContext = this.Context;
try
{
this.Context = context;
action();
return this.Context;
}
finally
{
this.Context = originalContext;
}
}
/// <summary>
/// Binds the provided value as the current Allure context. This allows the
/// Allure context to bypass .NET execution context boundaries. Use this
/// function in a highly concurrent environments where framework hooks and
/// user's test functions might all be run in different execution contexts.
/// For other scenarios consider using <see cref="RunInContext"/> first.
/// </summary>
/// <param name="context">
/// A context that was previously captured with <see cref="Context"/>.
/// It can't be null.
/// </param>
/// <exception cref="ArgumentNullException"></exception>
public void RestoreContext(AllureContext context)
{
this.Context = context ?? throw new ArgumentNullException(
nameof(context)
);
}
#region TestContainer
/// <summary>
/// Starts a new test container and pushes it into the container
/// context making the container context active. The container becomes
/// the current one in the current execution context.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Can't be called if the fixture or the test context is active.
/// </remarks>
/// <param name="container">A new test container to start.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartTestContainer(
TestResultContainer container
)
{
container.start = DateTimeOffset.Now.ToUnixTimeMilliseconds();
this.UpdateContext(c => c.WithContainer(container));
return this;
}
/// <summary>
/// Applies the specified update function to the current test container.
/// </summary>
/// <remarks>
/// Requires the container context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle UpdateTestContainer(
Action<TestResultContainer> update
)
{
var container = this.Context.CurrentContainer;
lock (this.modelMonitor)
{
update.Invoke(container);
}
return this;
}
/// <summary>
/// Stops the current test container.
/// </summary>
/// <remarks>
/// Requires the container context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopTestContainer()
{
UpdateTestContainer(stopContainer);
return this;
}
/// <summary>
/// Writes the current test container and removes it from the context.
/// If there are another test containers in the context, the most
/// recently started one becomes the current container in the current
/// execution context. Otherwise the container context is deactivated.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the container context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle WriteTestContainer()
{
var container = this.Context.CurrentContainer;
this.UpdateContext(c => c.WithNoLastContainer());
this.writer.Write(container);
return this;
}
#endregion
#region Fixture
/// <summary>
/// Starts a new before fixture and activates the fixture context with
/// it. The fixture is set as the current one in the current execution
/// context. Does nothing if the fixture context is already active.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the container context to be active.
/// </remarks>
/// <param name="result">A new fixture.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartBeforeFixture(FixtureResult result)
{
this.UpdateTestContainer(c => c.befores.Add(result));
this.StartFixture(result);
return this;
}
/// <summary>
/// Starts a new after fixture and activates the fixture context with
/// it. The fixture is set as the current one in the current execution
/// context. Does nothing if the fixture context is already active.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the container context to be active.
/// </remarks>
/// <param name="result">A new fixture.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartAfterFixture(FixtureResult result)
{
this.UpdateTestContainer(c => c.afters.Add(result));
this.StartFixture(result);
return this;
}
/// <summary>
/// Applies the specified update function to the current fixture.
/// </summary>
/// <remarks>
/// Requires the fixture context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle UpdateFixture(
Action<FixtureResult> update
)
{
var fixture = this.Context.CurrentFixture;
lock (this.modelMonitor)
{
update.Invoke(fixture);
}
return this;
}
/// <summary>
/// Stops the current fixture and deactivates the fixture context.
/// </summary>
/// <param name="beforeStop">
/// A function applied to the fixture result before it is stopped.
/// </param>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Required the fixture context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopFixture(
Action<FixtureResult> beforeStop
)
{
this.UpdateFixture(beforeStop);
return this.StopFixture();
}
/// <summary>
/// Stops the current fixture and deactivates the fixture context.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Required the fixture context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopFixture()
{
this.UpdateFixture(stopAllureItem);
this.UpdateContext(c => c.WithNoFixtureContext());
return this;
}
#endregion
#region TestCase
/// <summary>
/// Prepares a new test and activates the test context with it. The test
/// should be then started separately with <see cref="StartTestCase()"/>
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the test context to be active.
/// </remarks>
/// <param name="testResult">A new test case.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle ScheduleTestCase(TestResult testResult)
{
var uuid = testResult.uuid;
testResult.stage = Stage.scheduled;
var containers = this.Context.ContainerContext;
lock (this.modelMonitor)
{
foreach (TestResultContainer container in containers)
{
container.children.Add(uuid);
}
}
this.UpdateContext(c => c.WithTestContext(testResult));
return this;
}
/// <summary>
/// Starts a previously scheduled test.
/// </summary>
/// <remarks>
/// Requires the test context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartTestCase() =>
this.UpdateTestCase(startAllureItem);
/// <summary>
/// Starts a new test and activates the test context with it. The test
/// becomes the current one in the current execution context.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the test context to be active.
/// </remarks>
/// <param name="testResult">A new test case.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartTestCase(TestResult testResult) =>
this.ScheduleTestCase(testResult)
.UpdateTestCase(startAllureItem);
/// <summary>
/// Applies the specified update function to the current test.
/// </summary>
/// <remarks>
/// Requires the test context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle UpdateTestCase(
Action<TestResult> update
)
{
var testResult = this.Context.CurrentTest;
lock (this.modelMonitor)
{
update(testResult);
}
return this;
}
/// <summary>
/// Stops the current test.
/// </summary>
/// <remarks>
/// Requires the test context to be active.
/// </remarks>
/// <param name="beforeStop">
/// A function applied to the test result before it is stopped.
/// </param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopTestCase(
Action<TestResult> beforeStop
) => this.UpdateTestCase(
Chain(beforeStop, stopTestCase)
);
/// <summary>
/// Stops the current test.
/// </summary>
/// <remarks>
/// Requires the test context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopTestCase() =>
this.UpdateTestCase(stopTestCase);
/// <summary>
/// Writes the current test and removes it from the context. The test
/// context is then deactivated.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the test context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle WriteTestCase()
{
var testResult = this.Context.CurrentTest;
string uuid;
lock (this.modelMonitor)
{
uuid = testResult.uuid;
}
this.UpdateContext(c => c.WithNoTestContext());
this.writer.Write(testResult);
return this;
}
#endregion
#region Step
/// <summary>
/// Starts a new step and pushes it into the step context making the
/// step context active. The step becomes the current one in the
/// current execution context.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires either the fixture or the test context to be active.
/// </remarks>
/// <param name="result">A new step.</param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StartStep(StepResult result)
{
var parent = this.Context.CurrentStepContainer;
lock (this.modelMonitor)
{
parent.steps.Add(result);
}
this.UpdateContext(c => c.WithStep(result));
this.UpdateStep(startAllureItem);
return this;
}
/// <summary>
/// Applies the specified update function to the current step.
/// </summary>
/// <remarks>
/// Requires the step context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle UpdateStep(Action<StepResult> update)
{
var stepResult = this.Context.CurrentStep;
lock (this.modelMonitor)
{
update.Invoke(stepResult);
}
return this;
}
/// <summary>
/// Stops the current step and removes it from the context. If there
/// are another steps in the context, the most recently started one
/// becomes the current step in the current execution context.
/// Otherwise the step context is deactivated.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the step context to be active.
/// </remarks>
/// <param name="beforeStop">
/// A function that is applied to the step result before it is stopped.
/// </param>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopStep(Action<StepResult> beforeStop)
{
this.UpdateStep(beforeStop);
return this.StopStep();
}
/// <summary>
/// Stops the current step and removes it from the context. If there
/// are another steps in the context, the most recently started one
/// becomes the current step in the current execution context.
/// Otherwise the step context is deactivated.
/// </summary>
/// <remarks>
/// This method modifies the Allure context.<br></br>
/// Requires the step context to be active.
/// </remarks>
/// <exception cref="InvalidOperationException"/>
public virtual AllureLifecycle StopStep()
{
this.UpdateStep(stopAllureItem);
this.UpdateContext(c => c.WithNoLastStep());
return this;
}
#endregion
#region Executable item
/// <summary>
/// If the step context is active, updates the current step.
/// Otherwise, if the fixture context is active, updates the current fixture.
/// Otherwise, updates the current test.
/// Fails if neither test, nor fixture, nor step context is active.
/// </summary>
/// <remarks>
/// The method is intended to be used by authors of integrations.
/// </remarks>
/// <param name="updateItem">The update callback.</param>
public virtual AllureLifecycle UpdateExecutableItem(
Action<ExecutableItem> updateItem
)
{
var item = this.Context.CurrentStepContainer;
lock (this.modelMonitor)
{
updateItem(item);
}
return this;
}
#endregion
#region Extensions
/// <summary>
/// Removes all files and folders in the current Allure results directory.
/// </summary>
public virtual void CleanupResultDirectory()
{
writer.CleanUp();
}
#endregion
#region Privates
static AllureLifecycle Initialize() => new();
private static JObject GetConfiguration()
{
var configEnvVarName = AllureConstants.ALLURE_CONFIG_ENV_VARIABLE;
var jsonConfigPath = Environment.GetEnvironmentVariable(
configEnvVarName
);
if (jsonConfigPath != null && !File.Exists(jsonConfigPath))
{
throw new FileNotFoundException(
$"Couldn't find '{jsonConfigPath}' specified " +
$"in {configEnvVarName} environment variable"
);
}
if (File.Exists(jsonConfigPath))
{
return JObject.Parse(File.ReadAllText(jsonConfigPath));
}
var defaultJsonConfigPath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
AllureConstants.CONFIG_FILENAME
);
if (File.Exists(defaultJsonConfigPath))
{
return JObject.Parse(File.ReadAllText(defaultJsonConfigPath));
}
return JObject.Parse("{}");
}
private void StartFixture(FixtureResult fixtureResult)
{
this.UpdateContext(c => c.WithFixtureContext(fixtureResult));
this.UpdateFixture(startAllureItem);
}
static readonly Action<TestResultContainer> stopContainer =
c => c.stop = DateTimeOffset.Now.ToUnixTimeMilliseconds();
static readonly Action<ExecutableItem> startAllureItem =
item =>
{
item.stage = Stage.running;
item.start = DateTimeOffset.Now.ToUnixTimeMilliseconds();
};
static readonly Action<ExecutableItem> stopAllureItem =
item =>
{
item.stage = Stage.finished;
item.stop = DateTimeOffset.Now.ToUnixTimeMilliseconds();
};
static readonly Action<TestResult> stopTestCase = Chain(
stopAllureItem,
(TestResult tr) => tr.historyId ??= IdFunctions.CreateHistoryId(
tr.fullName,
tr.parameters
),
(TestResult tr) => tr.testCaseId ??= IdFunctions.CreateTestCaseId(
tr.fullName
)
);
void UpdateContext(Func<AllureContext, AllureContext> updateFn)
{
this.Context = updateFn(this.Context);
}
static string CreateUuid() =>
Guid.NewGuid().ToString("N");
static Action<T> Chain<T>(params Action<T>[] actions) => v =>
{
foreach (var action in actions)
{
action(v);
}
};
#endregion
}