-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathAllureXunitPatcher.cs
108 lines (93 loc) · 2.84 KB
/
AllureXunitPatcher.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
using System;
using Allure.Net.Commons.TestPlan;
using HarmonyLib;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Allure.Xunit;
internal static class AllureXunitPatcher
{
private const string ALLURE_ID = "io.qameta.allure.xunit";
private static bool _isPatched;
private static IRunnerLogger _logger;
private static AllureMessageSink CurrentSink
{
get
{
var sink = AllureMessageSink.CurrentSink;
if (sink is null)
{
_logger.LogWarning("Unable to get current message sink.");
}
return sink;
}
}
internal static void PatchXunit(IRunnerLogger runnerLogger)
{
if (_isPatched)
{
_logger.LogMessage(
"Patching is skipped: Xunit is already patched"
);
return;
}
_logger = runnerLogger;
var patcher = new Harmony(ALLURE_ID);
PatchXunitTestRunnerCtors(patcher);
_isPatched = true;
}
private static void PatchXunitTestRunnerCtors(Harmony patcher)
{
var testRunnerType = typeof(XunitTestRunner);
var wasPatched = false;
foreach (var ctor in testRunnerType.GetConstructors())
{
try
{
patcher.Patch(
ctor,
prefix: new HarmonyMethod(
typeof(AllureXunitPatcher),
nameof(OnTestRunnerCreating)
),
postfix: new HarmonyMethod(
typeof(AllureXunitPatcher),
nameof(OnTestRunnerCreated)
)
);
wasPatched = true;
_logger.LogImportantMessage(
"{0}'s {1} has been patched",
testRunnerType.Name,
ctor.ToString()
);
}
catch (Exception e)
{
_logger.LogWarning(
"Unable to patch {0}'s {1}: {2}",
testRunnerType.Name,
ctor.ToString(),
e.ToString()
);
}
}
if (!wasPatched)
{
_logger.LogWarning(
"No constructors of {0} were patched. Some theories may " +
"miss their parameters in the report",
testRunnerType.Name
);
}
}
private static void OnTestRunnerCreating(ITest test, ref string skipReason)
{
if (!CurrentSink.SelectByTestPlan(test))
{
skipReason = AllureTestPlan.SkipReason;
}
}
private static void OnTestRunnerCreated(ITest test, object[] testMethodArguments) =>
CurrentSink.OnTestArgumentsCreated(test, testMethodArguments);
}