Skip to content

Commit 957d47c

Browse files
Merge pull request #287 from microsoft/dev/davidraygoza/TAfGTGB18Crash
If new framework crashes then use old framework to report results
2 parents 90b3dc8 + ca87efe commit 957d47c

2 files changed

Lines changed: 72 additions & 55 deletions

File tree

GoogleTestAdapter/Core/GoogleTestDiscoverer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void DiscoverTests(string executable, ITestFrameworkReporter repo
7474
nrOfTestCases++;
7575
};
7676
var factory = new TestCaseFactory(executable, logger, settings, diaResolverFactory);
77-
factory.CreateTestCases(reportTestCases);
77+
factory.CreateTestCases(reportTestCases, reporter);
7878
logger.LogInfo(String.Format(Resources.NumberOfTestsMessage, nrOfTestCases, executable));
7979
}, logger);
8080
}

GoogleTestAdapter/Core/TestCases/TestCaseFactory.cs

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using GoogleTestAdapter.Common;
1010
using GoogleTestAdapter.DiaResolver;
11+
using GoogleTestAdapter.Framework;
1112
using GoogleTestAdapter.Helpers;
1213
using GoogleTestAdapter.Model;
1314
using 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

Comments
 (0)