88using System . Threading . Tasks ;
99using GoogleTestAdapter . Common ;
1010using GoogleTestAdapter . DiaResolver ;
11+ using GoogleTestAdapter . Framework ;
1112using GoogleTestAdapter . Helpers ;
1213using GoogleTestAdapter . Model ;
1314using GoogleTestAdapter . Runners ;
@@ -33,68 +34,20 @@ public TestCaseFactory(string executable, ILogger logger, SettingsWrapper settin
3334 _diaResolverFactory = diaResolverFactory ;
3435 }
3536
36- public IList < TestCase > CreateTestCases ( Action < TestCase > reportTestCase = null )
37+ public IList < TestCase > CreateTestCases ( Action < TestCase > reportTestCase = null , ITestFrameworkReporter reporter = null )
3738 {
3839 List < string > standardOutput = new List < string > ( ) ;
3940 if ( _settings . UseNewTestExecutionFramework )
4041 {
41- return NewCreateTestcases ( reportTestCase , standardOutput ) ;
42+ return NewCreateTestcases ( reportTestCase , standardOutput , reporter ) ;
4243 }
43-
44- try
45- {
46- var launcher = new ProcessLauncher ( _logger , _settings . GetPathExtension ( _executable ) , null ) ;
47- int processExitCode ;
48- string workingDir = new FileInfo ( _executable ) . DirectoryName ;
49-
50- string cmdLine = GoogleTestConstants . ListTestsOption ;
51- if ( ! string . IsNullOrEmpty ( _settings . AdditionalTestDiscoveryParam ) )
52- {
53- cmdLine = string . Format ( "{0} {1}" , _settings . AdditionalTestDiscoveryParam , cmdLine ) ;
54- }
55-
56- standardOutput = launcher . GetOutputOfCommand ( workingDir , null , _executable , cmdLine ,
57- false , false , out processExitCode ) ;
58-
59- if ( ! CheckProcessExitCode ( processExitCode , standardOutput ) )
60- return new List < TestCase > ( ) ;
61- }
62- catch ( Exception e )
63- {
64- SequentialTestRunner . LogExecutionError ( _logger , _executable , Path . GetFullPath ( "" ) ,
65- GoogleTestConstants . ListTestsOption , e ) ;
66- return new List < TestCase > ( ) ;
67- }
68-
69- IList < TestCaseDescriptor > testCaseDescriptors = new ListTestsParser ( _settings . TestNameSeparator ) . ParseListTestsOutput ( standardOutput ) ;
70- var testCaseLocations = GetTestCaseLocations ( testCaseDescriptors , _settings . GetPathExtension ( _executable ) ) ;
71-
72- IList < TestCase > testCases = new List < TestCase > ( ) ;
73- IDictionary < string , ISet < TestCase > > suite2TestCases = new Dictionary < string , ISet < TestCase > > ( ) ;
74- foreach ( var descriptor in testCaseDescriptors )
75- {
76- var testCase = _settings . ParseSymbolInformation
77- ? CreateTestCase ( descriptor , testCaseLocations )
78- : CreateTestCase ( descriptor ) ;
79- ISet < TestCase > testCasesInSuite ;
80- if ( ! suite2TestCases . TryGetValue ( descriptor . Suite , out testCasesInSuite ) )
81- suite2TestCases . Add ( descriptor . Suite , testCasesInSuite = new HashSet < TestCase > ( ) ) ;
82- testCasesInSuite . Add ( testCase ) ;
83- testCases . Add ( testCase ) ;
84- }
85-
86- foreach ( var suiteTestCasesPair in suite2TestCases )
44+ else
8745 {
88- foreach ( var testCase in suiteTestCasesPair . Value )
89- {
90- testCase . Properties . Add ( new TestCaseMetaDataProperty ( suiteTestCasesPair . Value . Count , testCases . Count , testCase . FullyQualifiedName ) ) ;
91- }
46+ return OldCreateTestCases ( standardOutput ) ;
9247 }
93-
94- return testCases ;
9548 }
9649
97- private IList < TestCase > NewCreateTestcases ( Action < TestCase > reportTestCase , List < string > standardOutput )
50+ private IList < TestCase > NewCreateTestcases ( Action < TestCase > reportTestCase , List < string > standardOutput , ITestFrameworkReporter reporter = null )
9851 {
9952 var testCases = new List < TestCase > ( ) ;
10053
@@ -195,12 +148,76 @@ private IList<TestCase> NewCreateTestcases(Action<TestCase> reportTestCase, List
195148 if ( ! CheckProcessExitCode ( processExitCode , standardOutput ) )
196149 return new List < TestCase > ( ) ;
197150 }
151+
152+ catch ( Exception e )
153+ {
154+ // If a crash happened with new framework, then try using old framework before reporting empty results.
155+ standardOutput = new List < string > ( ) ;
156+ return OldCreateTestCases ( standardOutput , reporter ) ;
157+ }
158+
159+ return testCases ;
160+ }
161+
162+ private IList < TestCase > OldCreateTestCases ( List < string > standardOutput , ITestFrameworkReporter reporter = null )
163+ {
164+ try
165+ {
166+ var launcher = new ProcessLauncher ( _logger , _settings . GetPathExtension ( _executable ) , null ) ;
167+ int processExitCode ;
168+ string workingDir = new FileInfo ( _executable ) . DirectoryName ;
169+
170+ string cmdLine = GoogleTestConstants . ListTestsOption ;
171+ if ( ! string . IsNullOrEmpty ( _settings . AdditionalTestDiscoveryParam ) )
172+ {
173+ cmdLine = string . Format ( "{0} {1}" , _settings . AdditionalTestDiscoveryParam , cmdLine ) ;
174+ }
175+
176+ standardOutput = launcher . GetOutputOfCommand ( workingDir , null , _executable , cmdLine ,
177+ false , false , out processExitCode ) ;
178+
179+ if ( ! CheckProcessExitCode ( processExitCode , standardOutput ) )
180+ return new List < TestCase > ( ) ;
181+ }
198182 catch ( Exception e )
199183 {
200184 SequentialTestRunner . LogExecutionError ( _logger , _executable , Path . GetFullPath ( "" ) ,
201- cmdLine , e ) ;
185+ GoogleTestConstants . ListTestsOption , e ) ;
202186 return new List < TestCase > ( ) ;
203187 }
188+
189+ IList < TestCaseDescriptor > testCaseDescriptors = new ListTestsParser ( _settings . TestNameSeparator ) . ParseListTestsOutput ( standardOutput ) ;
190+ var testCaseLocations = GetTestCaseLocations ( testCaseDescriptors , _settings . GetPathExtension ( _executable ) ) ;
191+
192+ IList < TestCase > testCases = new List < TestCase > ( ) ;
193+ IDictionary < string , ISet < TestCase > > suite2TestCases = new Dictionary < string , ISet < TestCase > > ( ) ;
194+ foreach ( var descriptor in testCaseDescriptors )
195+ {
196+ var testCase = _settings . ParseSymbolInformation
197+ ? CreateTestCase ( descriptor , testCaseLocations )
198+ : CreateTestCase ( descriptor ) ;
199+ ISet < TestCase > testCasesInSuite ;
200+ if ( ! suite2TestCases . TryGetValue ( descriptor . Suite , out testCasesInSuite ) )
201+ suite2TestCases . Add ( descriptor . Suite , testCasesInSuite = new HashSet < TestCase > ( ) ) ;
202+ testCasesInSuite . Add ( testCase ) ;
203+ testCases . Add ( testCase ) ;
204+ }
205+
206+ foreach ( var suiteTestCasesPair in suite2TestCases )
207+ {
208+ foreach ( var testCase in suiteTestCasesPair . Value )
209+ {
210+ testCase . Properties . Add ( new TestCaseMetaDataProperty ( suiteTestCasesPair . Value . Count , testCases . Count , testCase . FullyQualifiedName ) ) ;
211+ }
212+ }
213+
214+ // Only report the test cases if we are manually passing the reporter.
215+ // This can be due to a previous crash with the new framework so we manually invoke the old framework to report results.
216+ if ( reporter != null )
217+ {
218+ reporter . ReportTestsFound ( testCases ) ;
219+ }
220+
204221 return testCases ;
205222 }
206223
0 commit comments