-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathTUnitMessageBus.cs
More file actions
163 lines (130 loc) · 5.49 KB
/
Copy pathTUnitMessageBus.cs
File metadata and controls
163 lines (130 loc) · 5.49 KB
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
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.TestHost;
using TUnit.Core;
using TUnit.Engine.CommandLineProviders;
using TUnit.Engine.Exceptions;
using TUnit.Engine.Extensions;
using TUnit.Engine.Services;
#pragma warning disable TPEXP
namespace TUnit.Engine;
internal class TUnitMessageBus(IExtension extension, ICommandLineOptions commandLineOptions, VerbosityService verbosityService, IServiceProvider serviceProvider, ExecuteRequestContext context) : ITUnitMessageBus, IDataProducer
{
private readonly SessionUid _sessionSessionUid = context.Request.Session.SessionUid;
private bool? _isConsole;
private bool IsConsole => _isConsole ??= serviceProvider.GetClientInfo().Id.Contains("console", StringComparison.InvariantCultureIgnoreCase);
public async ValueTask Discovered(TestContext testContext)
{
if (testContext.IsNotDiscoverable)
{
return;
}
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testContext.ToTestNode(DiscoveredTestNodeStateProperty.CachedInstance)
));
}
public async ValueTask InProgress(TestContext testContext)
{
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testContext.ToTestNode(InProgressTestNodeStateProperty.CachedInstance)
));
}
public async ValueTask Passed(TestContext testContext, DateTimeOffset start)
{
if (!testContext.ReportResult)
{
return;
}
var testNode = testContext.ToTestNode(PassedTestNodeStateProperty.CachedInstance);
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testNode
));
}
public async ValueTask Failed(TestContext testContext, Exception exception, DateTimeOffset start)
{
if (!testContext.ReportResult)
{
return;
}
exception = SimplifyStacktrace(exception);
var duration = testContext.Execution.TestEnd - testContext.Execution.TestStart;
var updateType = GetFailureStateProperty(testContext, exception, duration ?? TimeSpan.Zero);
var testNode = testContext.ToTestNode(updateType);
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testNode
));
}
private Exception SimplifyStacktrace(Exception exception)
{
// Check both the legacy --detailed-stacktrace flag and the new verbosity system
if (commandLineOptions.IsOptionSet(DetailedStacktraceCommandProvider.DetailedStackTrace) ||
verbosityService?.ShowDetailedStackTrace == true)
{
return exception;
}
if (IsConsole)
{
// It's only really spammy in a console environment.
// In an IDE, every test has their own output window, so it's not as spammy.
return new TestFailedException(exception);
}
return exception;
}
public async ValueTask Skipped(TestContext testContext, string reason)
{
var testNode = testContext.ToTestNode(new SkippedTestNodeStateProperty(reason));
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testNode
));
}
public async ValueTask Cancelled(TestContext testContext, DateTimeOffset start)
{
var testNode = testContext.ToTestNode(new CancelledTestNodeStateProperty());
await context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(
sessionUid: _sessionSessionUid,
testNode: testNode
));
}
public async ValueTask SessionArtifact(Artifact artifact)
{
await context.MessageBus.PublishAsync(this,
new SessionFileArtifact(
context.Request.Session.SessionUid,
artifact.File,
artifact.DisplayName,
artifact.Description
)
);
}
private static TestNodeStateProperty GetFailureStateProperty(TestContext testContext, Exception e, TimeSpan duration)
{
if (testContext.Metadata.TestDetails.Timeout != null
&& e is TaskCanceledException or OperationCanceledException or TimeoutException
&& duration >= testContext.Metadata.TestDetails.Timeout.Value)
{
return new TimeoutTestNodeStateProperty($"Test timed out after {testContext.Metadata.TestDetails.Timeout.Value.TotalMilliseconds}ms");
}
if (e.GetType().Name.Contains("Assertion", StringComparison.InvariantCulture))
{
return new FailedTestNodeStateProperty(e);
}
return new ErrorTestNodeStateProperty(e);
}
public Task<bool> IsEnabledAsync()
{
return extension.IsEnabledAsync();
}
public string Uid => extension.Uid;
public string Version => extension.Version;
public string DisplayName => extension.DisplayName;
public string Description => extension.Description;
public Type[] DataTypesProduced => [typeof(TestNodeUpdateMessage), typeof(SessionFileArtifact)];
}