@@ -49,9 +49,9 @@ public sealed class PublishTestResultsCommand : IWorkerCommand
4949 private bool _publishRunLevelAttachments ;
5050 private TestCaseResult [ ] _testCaseResults ;
5151 private string _testPlanId ;
52- private bool enableAzureTestPlanFeatureState ;
53- private bool publishTestResultsLibFeatureState ;
54- private bool triggerCoverageMergeJobFeatureState ;
52+ private bool _enableAzureTestPlanFeatureState ;
53+ private bool _publishTestResultsLibFeatureState ;
54+ private bool _triggerCoverageMergeJobFeatureState ;
5555
5656 private bool _failTaskOnFailedTests ;
5757
@@ -161,7 +161,7 @@ private void LoadPublishTestResultsInputs(IExecutionContext context, Dictionary<
161161 _publishRunLevelAttachments = true ;
162162 }
163163
164- if ( enableAzureTestPlanFeatureState )
164+ if ( _enableAzureTestPlanFeatureState )
165165 {
166166 string jsonString ;
167167 eventProperties . TryGetValue ( PublishTestResultsEventProperties . ListOfAutomatedTestPoints , out jsonString ) ;
@@ -257,7 +257,7 @@ private TestRunContext CreateTestRunContext()
257257
258258 TestRunContext testRunContext ;
259259
260- if ( enableAzureTestPlanFeatureState && ! string . IsNullOrEmpty ( _testPlanId ) )
260+ if ( _enableAzureTestPlanFeatureState && ! string . IsNullOrEmpty ( _testPlanId ) )
261261 {
262262 ShallowReference testPlanObject = new ( ) { Id = _testPlanId } ;
263263
@@ -308,84 +308,86 @@ private PublishOptions GetPublishOptions()
308308 return publishOptions ;
309309 }
310310
311- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Maintainability" , "CA2000:Dispose objects before losing scope" , MessageId = "connection" ) ]
312311 private async Task PublishTestRunDataAsync ( string teamProject , TestRunContext testRunContext )
313312 {
314313 bool isTestRunOutcomeFailed = false ;
315314
316- _telemetryProperties . Add ( "UsePublishTestResultsLib" , publishTestResultsLibFeatureState ) ;
317- var connection = WorkerUtilities . GetVssConnection ( _executionContext ) ;
318-
319- //This check is to determine to use "Microsoft.TeamFoundation.PublishTestResults" Library or the agent code to parse and publish the test results.
320- if ( publishTestResultsLibFeatureState )
315+ _telemetryProperties . Add ( "UsePublishTestResultsLib" , _publishTestResultsLibFeatureState ) ;
316+ using ( var connection = WorkerUtilities . GetVssConnection ( _executionContext ) )
321317 {
322- var publisher = _executionContext . GetHostContext ( ) . GetService < ITestDataPublisher > ( ) ;
323- publisher . InitializePublisher ( _executionContext , teamProject , connection , _testRunner ) ;
324318
325- if ( enableAzureTestPlanFeatureState && ! _testCaseResults . IsNullOrEmpty ( ) && ! _testPlanId . IsNullOrEmpty ( ) )
319+ //This check is to determine to use "Microsoft.TeamFoundation.PublishTestResults" Library or the agent code to parse and publish the test results.
320+ if ( _publishTestResultsLibFeatureState )
326321 {
327- isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , _testCaseResults , GetPublishOptions ( ) , _executionContext . CancellationToken ) ;
322+ var publisher = _executionContext . GetHostContext ( ) . GetService < ITestDataPublisher > ( ) ;
323+ publisher . InitializePublisher ( _executionContext , teamProject , connection , _testRunner ) ;
324+
325+ if ( _enableAzureTestPlanFeatureState && ! _testCaseResults . IsNullOrEmpty ( ) && ! _testPlanId . IsNullOrEmpty ( ) )
326+ {
327+ isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , _testCaseResults , GetPublishOptions ( ) , _executionContext . CancellationToken ) ;
328+ }
329+ else
330+ {
331+ isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , GetPublishOptions ( ) , _executionContext . CancellationToken ) ;
332+ }
328333 }
329334 else
330335 {
331- isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , GetPublishOptions ( ) , _executionContext . CancellationToken ) ;
332- }
333- }
334- else
335- {
336- var publisher = _executionContext . GetHostContext ( ) . GetService < ILegacyTestRunDataPublisher > ( ) ;
337- publisher . InitializePublisher ( _executionContext , teamProject , connection , _testRunner , _publishRunLevelAttachments ) ;
336+ var publisher = _executionContext . GetHostContext ( ) . GetService < ILegacyTestRunDataPublisher > ( ) ;
337+ publisher . InitializePublisher ( _executionContext , teamProject , connection , _testRunner , _publishRunLevelAttachments ) ;
338338
339- isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , _runTitle , _executionContext . Variables . Build_BuildId , _mergeResults ) ;
340- }
339+ isTestRunOutcomeFailed = await publisher . PublishAsync ( testRunContext , _testResultFiles , _runTitle , _executionContext . Variables . Build_BuildId , _mergeResults ) ;
340+ }
341341
342- if ( isTestRunOutcomeFailed && _failTaskOnFailedTests )
343- {
344- _executionContext . Result = TaskResult . Failed ;
345- _executionContext . Error ( StringUtil . Loc ( "FailedTestsInResults" ) ) ;
346- }
342+ if ( isTestRunOutcomeFailed && _failTaskOnFailedTests )
343+ {
344+ _executionContext . Result = TaskResult . Failed ;
345+ _executionContext . Error ( StringUtil . Loc ( "FailedTestsInResults" ) ) ;
346+ }
347347
348- await PublishEventsAsync ( connection ) ;
349- if ( triggerCoverageMergeJobFeatureState )
350- {
351- TriggerCoverageMergeJob ( _testResultFiles , _executionContext ) ;
348+ await PublishEventsAsync ( connection ) ;
349+ if ( _triggerCoverageMergeJobFeatureState )
350+ {
351+ TriggerCoverageMergeJob ( _testResultFiles , _executionContext ) ;
352+ }
352353 }
353354 }
354355
355356 // Queue code coverage merge job if code coverage attachments are published to avoid BQC timeout.
356- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Maintainability" , "CA2000:Dispose objects before losing scope" , MessageId = "connection" ) ]
357357 private void TriggerCoverageMergeJob ( List < string > resultFilesInput , IExecutionContext context )
358358 {
359359 try
360360 {
361361 ITestResultsServer _testResultsServer = context . GetHostContext ( ) . GetService < ITestResultsServer > ( ) ;
362- foreach ( var resultFile in resultFilesInput )
362+ using ( var connection = WorkerUtilities . GetVssConnection ( _executionContext ) )
363363 {
364- string text = File . ReadAllText ( resultFile ) ;
365- XmlDocument xdoc = new XmlDocument ( ) ;
366- xdoc . LoadXml ( text ) ;
367- XmlNodeList nodes = xdoc . GetElementsByTagName ( "A" ) ;
368-
369- var connection = WorkerUtilities . GetVssConnection ( _executionContext ) ;
370- foreach ( XmlNode attachmentNode in nodes )
364+ foreach ( var resultFile in resultFilesInput )
371365 {
372- var file = attachmentNode . Attributes ? [ "href" ] ? . Value ;
373- if ( ! string . IsNullOrEmpty ( file ) )
366+ string text = File . ReadAllText ( resultFile ) ;
367+ XmlDocument xdoc = new XmlDocument ( ) ;
368+ xdoc . LoadXml ( text ) ;
369+ XmlNodeList nodes = xdoc . GetElementsByTagName ( "A" ) ;
370+
371+ foreach ( XmlNode attachmentNode in nodes )
374372 {
375- if (
376- Path . GetExtension ( file ) . Equals ( ".covx" , StringComparison . OrdinalIgnoreCase ) ||
377- Path . GetExtension ( file ) . Equals ( ".covb" , StringComparison . OrdinalIgnoreCase ) ||
378- Path . GetExtension ( file ) . Equals ( ".coverage" , StringComparison . OrdinalIgnoreCase )
379- )
373+ var file = attachmentNode . Attributes ? [ "href" ] ? . Value ;
374+ if ( ! string . IsNullOrEmpty ( file ) )
380375 {
381- _testResultsServer . InitializeServer ( connection , _executionContext ) ;
382- try
383- {
384- var codeCoverageResults = _testResultsServer . UpdateCodeCoverageSummaryAsync ( connection , _executionContext . Variables . System_TeamProjectId . ToString ( ) , _executionContext . Variables . Build_BuildId . GetValueOrDefault ( ) ) ;
385- }
386- catch ( Exception e )
376+ if (
377+ Path . GetExtension ( file ) . Equals ( ".covx" , StringComparison . OrdinalIgnoreCase ) ||
378+ Path . GetExtension ( file ) . Equals ( ".covb" , StringComparison . OrdinalIgnoreCase ) ||
379+ Path . GetExtension ( file ) . Equals ( ".coverage" , StringComparison . OrdinalIgnoreCase )
380+ )
387381 {
388- _executionContext . Section ( $ "Could not queue code coverage merge:{ e } ") ;
382+ _testResultsServer . InitializeServer ( connection , _executionContext ) ;
383+ try
384+ {
385+ var codeCoverageResults = _testResultsServer . UpdateCodeCoverageSummaryAsync ( connection , _executionContext . Variables . System_TeamProjectId . ToString ( ) , _executionContext . Variables . Build_BuildId . GetValueOrDefault ( ) ) ;
386+ }
387+ catch ( Exception e )
388+ {
389+ _executionContext . Section ( $ "Could not queue code coverage merge:{ e } ") ;
390+ }
389391 }
390392 }
391393 }
@@ -435,15 +437,16 @@ private void PopulateTelemetryData()
435437 }
436438 }
437439
438- [ System . Diagnostics . CodeAnalysis . SuppressMessage ( "Microsoft.Maintainability" , "CA2000:Dispose objects before losing scope" , MessageId = "connection" ) ]
439440 private void LoadFeatureFlagState ( )
440441 {
441- var connection = WorkerUtilities . GetVssConnection ( _executionContext ) ;
442- var featureFlagService = _executionContext . GetHostContext ( ) . GetService < IFeatureFlagService > ( ) ;
443- featureFlagService . InitializeFeatureService ( _executionContext , connection ) ;
444- publishTestResultsLibFeatureState = featureFlagService . GetFeatureFlagState ( TestResultsConstants . UsePublishTestResultsLibFeatureFlag , TestResultsConstants . TFSServiceInstanceGuid ) ;
445- enableAzureTestPlanFeatureState = featureFlagService . GetFeatureFlagState ( TestResultsConstants . EnableAzureTestPlanTaskFeatureFlag , TestResultsConstants . TFSServiceInstanceGuid ) ;
446- triggerCoverageMergeJobFeatureState = featureFlagService . GetFeatureFlagState ( CodeCoverageConstants . TriggerCoverageMergeJobFF , TestResultsConstants . TFSServiceInstanceGuid ) ;
442+ using ( var connection = WorkerUtilities . GetVssConnection ( _executionContext ) )
443+ {
444+ var featureFlagService = _executionContext . GetHostContext ( ) . GetService < IFeatureFlagService > ( ) ;
445+ featureFlagService . InitializeFeatureService ( _executionContext , connection ) ;
446+ _publishTestResultsLibFeatureState = featureFlagService . GetFeatureFlagState ( TestResultsConstants . UsePublishTestResultsLibFeatureFlag , TestResultsConstants . TFSServiceInstanceGuid ) ;
447+ _enableAzureTestPlanFeatureState = featureFlagService . GetFeatureFlagState ( TestResultsConstants . EnableAzureTestPlanTaskFeatureFlag , TestResultsConstants . TFSServiceInstanceGuid ) ;
448+ _triggerCoverageMergeJobFeatureState = featureFlagService . GetFeatureFlagState ( CodeCoverageConstants . TriggerCoverageMergeJobFF , TestResultsConstants . TFSServiceInstanceGuid ) ;
449+ }
447450 }
448451 }
449452
0 commit comments