@@ -31,9 +31,11 @@ internal class TestContainerDiscoverer : ITestContainerDiscoverer
3131 private readonly IAppOptionsProvider appOptionsProvider ;
3232 private readonly IReportGeneratorUtil reportGeneratorUtil ;
3333 private readonly IMsCodeCoverageRunSettingsService msCodeCoverageRunSettingsService ;
34+ private readonly Dictionary < TestOperationStates , Func < IOperation , Task > > testOperationStateChangeHandlers ;
3435 private bool cancelling ;
3536 private MsCodeCoverageCollectionStatus msCodeCoverageCollectionStatus ;
3637 private bool runningInParallel ;
38+ private IAppOptions settings ;
3739 internal Task initializeTask ;
3840
3941 [ ExcludeFromCodeCoverage ]
@@ -65,6 +67,13 @@ IMsCodeCoverageRunSettingsService msCodeCoverageRunSettingsService
6567 this . fccEngine = fccEngine ;
6668 this . testOperationFactory = testOperationFactory ;
6769 this . logger = logger ;
70+ testOperationStateChangeHandlers = new Dictionary < TestOperationStates , Func < IOperation , Task > >
71+ {
72+ { TestOperationStates . TestExecutionCanceling , TestExecutionCancellingAsync } ,
73+ { TestOperationStates . TestExecutionStarting , TestExecutionStartingAsync } ,
74+ { TestOperationStates . TestExecutionFinished , TestExecutionFinishedAsync } ,
75+ { TestOperationStates . TestExecutionCancelAndFinished , TestExecutionCancelAndFinishedAsync } ,
76+ } ;
6877
6978 disposeAwareTaskRunner . RunAsync ( ( ) =>
7079 {
@@ -84,6 +93,7 @@ IMsCodeCoverageRunSettingsService msCodeCoverageRunSettingsService
8493
8594 private async Task TestExecutionStartingAsync ( IOperation operation )
8695 {
96+ cancelling = false ;
8797 runningInParallel = false ;
8898 StopCoverage ( ) ;
8999
@@ -122,19 +132,35 @@ private void CombinedLog(string message)
122132
123133 private async Task TestExecutionFinishedAsync ( IOperation operation )
124134 {
125- var settings = appOptionsProvider . Get ( ) ;
126- if ( ! settings . Enabled || runningInParallel || MsCodeCoverageErrored )
127- {
128- return ;
135+ var ( should , testOperation ) = ShouldConditionallyCollectWhenTestExecutionFinished ( operation ) ;
136+ if ( should ) {
137+ await TestExecutionFinishedCollectionAsync ( operation , testOperation ) ;
129138 }
139+
140+ }
130141
131- var testOperation = testOperationFactory . Create ( operation ) ;
132-
133- if ( ! CoverageConditionsMet ( testOperation , settings ) )
142+ private ( bool should , ITestOperation testOperation ) ShouldConditionallyCollectWhenTestExecutionFinished ( IOperation operation )
143+ {
144+ if ( ShouldNotCollectWhenTestExecutionFinished ( ) )
134145 {
135- return ;
146+ return ( false , null ) ;
136147 }
137148
149+ var testOperation = testOperationFactory . Create ( operation ) ;
150+
151+ var shouldCollect = CoverageConditionsMet ( testOperation ) ;
152+ return ( shouldCollect , testOperation ) ;
153+ }
154+
155+ private bool ShouldNotCollectWhenTestExecutionFinished ( )
156+ {
157+ settings = appOptionsProvider . Get ( ) ;
158+ return ! settings . Enabled || runningInParallel || MsCodeCoverageErrored ;
159+
160+ }
161+
162+ private async Task TestExecutionFinishedCollectionAsync ( IOperation operation , ITestOperation testOperation )
163+ {
138164 if ( msCodeCoverageCollectionStatus == MsCodeCoverageCollectionStatus . Collecting )
139165 {
140166 await msCodeCoverageRunSettingsService . CollectAsync ( operation , testOperation ) ;
@@ -145,7 +171,7 @@ private async Task TestExecutionFinishedAsync(IOperation operation)
145171 }
146172 }
147173
148- private bool CoverageConditionsMet ( ITestOperation testOperation , IAppOptions settings )
174+ private bool CoverageConditionsMet ( ITestOperation testOperation )
149175 {
150176 if ( ! settings . RunWhenTestsFail && testOperation . FailedTests > 0 )
151177 {
@@ -206,29 +232,24 @@ private void OperationState_StateChanged(object sender, OperationStateChangedEve
206232 } ) ;
207233 }
208234
209- private async Task OperationState_StateChangedAsync ( OperationStateChangedEventArgs e )
235+ private async Task TestExecutionCancellingAsync ( IOperation operation )
210236 {
211- if ( e . State == TestOperationStates . TestExecutionCanceling )
212- {
213- cancelling = true ;
214- await CoverageCancelledAsync ( "Test execution cancelling - running coverage will be cancelled." , e . Operation ) ;
215- }
216-
217-
218- if ( e . State == TestOperationStates . TestExecutionStarting )
219- {
220- await TestExecutionStartingAsync ( e . Operation ) ;
221- cancelling = false ;
222- }
237+ cancelling = true ;
238+ await CoverageCancelledAsync ( "Test execution cancelling - running coverage will be cancelled." , operation ) ;
239+ }
223240
224- if ( e . State == TestOperationStates . TestExecutionFinished )
241+ private async Task TestExecutionCancelAndFinishedAsync ( IOperation operation )
242+ {
243+ if ( ! cancelling )
225244 {
226- await TestExecutionFinishedAsync ( e . Operation ) ;
245+ await CoverageCancelledAsync ( "There has been an issue running tests. See the Tests output window pane." , operation ) ;
227246 }
247+ }
228248
229- if ( e . State == TestOperationStates . TestExecutionCancelAndFinished && ! cancelling )
230- {
231- await CoverageCancelledAsync ( "There has been an issue running tests. See the Tests output window pane." , e . Operation ) ;
249+ private async Task OperationState_StateChangedAsync ( OperationStateChangedEventArgs e )
250+ {
251+ if ( testOperationStateChangeHandlers . TryGetValue ( e . State , out var handler ) ) {
252+ await handler ( e . Operation ) ;
232253 }
233254 }
234255
0 commit comments