Skip to content

Commit 899ad53

Browse files
committed
feat(execution): Refactor OverrideResult method for improved validation and exception handling
1 parent 5cac489 commit 899ad53

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

TUnit.Core/TestContext.Execution.cs

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -68,58 +68,56 @@ bool ITestExecution.ReportResult
6868
// Internal implementation methods
6969
internal void OverrideResult(TestState state, string reason)
7070
{
71-
// Validation: Reason must not be empty
72-
if (string.IsNullOrWhiteSpace(reason))
71+
lock (Lock)
7372
{
74-
throw new ArgumentException("Override reason cannot be empty or whitespace.", nameof(reason));
75-
}
73+
if (string.IsNullOrWhiteSpace(reason))
74+
{
75+
throw new ArgumentException("Override reason cannot be empty or whitespace.", nameof(reason));
76+
}
7677

77-
// Validation: Prevent double-override
78-
if (Result?.IsOverridden == true)
79-
{
80-
throw new InvalidOperationException(
81-
$"Result has already been overridden to {Result.State} with reason: '{Result.OverrideReason}'. " +
82-
"Cannot override a result multiple times.");
83-
}
78+
if (Result?.IsOverridden == true)
79+
{
80+
throw new InvalidOperationException(
81+
$"Result has already been overridden to {Result.State} with reason: '{Result.OverrideReason}'. " +
82+
"Cannot override a result multiple times. Check Result.IsOverridden before calling OverrideResult().");
83+
}
8484

85-
// Validation: Only allow final states
86-
if (state is TestState.NotStarted or TestState.WaitingForDependencies or TestState.Queued or TestState.Running)
87-
{
88-
throw new ArgumentException(
89-
$"Cannot override to intermediate state '{state}'. " +
90-
"Only final states (Passed, Failed, Skipped, Timeout, Cancelled) are allowed.",
91-
nameof(state));
92-
}
85+
if (state is TestState.NotStarted or TestState.WaitingForDependencies or TestState.Queued or TestState.Running)
86+
{
87+
throw new ArgumentException(
88+
$"Cannot override to intermediate state '{state}'. " +
89+
"Only final states (Passed, Failed, Skipped, Timeout, Cancelled) are allowed.",
90+
nameof(state));
91+
}
9392

94-
// Preserve the original exception if one exists
95-
var originalException = Result?.Exception;
93+
var originalException = Result?.Exception;
9694

97-
// When overriding to Failed without an original exception, create a synthetic one
98-
Exception? exceptionForResult;
99-
if (state == TestState.Failed)
100-
{
101-
exceptionForResult = originalException ?? new InvalidOperationException($"Test overridden to failed: {reason}");
102-
}
103-
else
104-
{
105-
exceptionForResult = null;
106-
}
95+
Exception? exceptionForResult;
96+
if (state == TestState.Failed)
97+
{
98+
exceptionForResult = originalException ?? new InvalidOperationException($"Test overridden to failed: {reason}");
99+
}
100+
else
101+
{
102+
exceptionForResult = null;
103+
}
107104

108-
Result = new TestResult
109-
{
110-
State = state,
111-
OverrideReason = reason,
112-
IsOverridden = true,
113-
OriginalException = originalException,
114-
Start = TestStart ?? DateTimeOffset.UtcNow,
115-
End = DateTimeOffset.UtcNow,
116-
Duration = DateTimeOffset.UtcNow - (TestStart ?? DateTimeOffset.UtcNow),
117-
Exception = exceptionForResult,
118-
ComputerName = Environment.MachineName,
119-
TestContext = this
120-
};
121-
122-
InternalExecutableTest.State = state;
105+
Result = new TestResult
106+
{
107+
State = state,
108+
OverrideReason = reason,
109+
IsOverridden = true,
110+
OriginalException = originalException,
111+
Start = TestStart ?? DateTimeOffset.UtcNow,
112+
End = DateTimeOffset.UtcNow,
113+
Duration = DateTimeOffset.UtcNow - (TestStart ?? DateTimeOffset.UtcNow),
114+
Exception = exceptionForResult,
115+
ComputerName = Environment.MachineName,
116+
TestContext = this
117+
};
118+
119+
InternalExecutableTest.State = state;
120+
}
123121
}
124122

125123
internal void AddLinkedCancellationToken(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)