From ec2ac0d2cf493ced03fb0bfe993be0c46f2df488 Mon Sep 17 00:00:00 2001
From: Evan Hampton <evan.hampton@seequent.com>
Date: Thu, 16 Sep 2021 08:40:30 -0600
Subject: [PATCH] Fixing incorrect logic when reducing to root test suite.
 Adding flags to allow for not reducing to root test suite.

---
 GoogleTestAdapter/Core/GoogleTestExecutor.cs   |  2 +-
 .../Core/Runners/CommandLineGenerator.cs       | 18 ++++++++----------
 .../Core/Runners/ParallelTestRunner.cs         |  2 +-
 .../Core/Runners/PreparingTestRunner.cs        |  8 ++++----
 .../Core/Runners/SequentialTestRunner.cs       |  6 ++++--
 5 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/GoogleTestAdapter/Core/GoogleTestExecutor.cs b/GoogleTestAdapter/Core/GoogleTestExecutor.cs
index 49c43f597..64d3335d7 100644
--- a/GoogleTestAdapter/Core/GoogleTestExecutor.cs
+++ b/GoogleTestAdapter/Core/GoogleTestExecutor.cs
@@ -73,7 +73,7 @@ private void ComputeTestRunner(ITestFrameworkReporter reporter, bool isBeingDebu
             }
             else
             {
-                _runner = new PreparingTestRunner(reporter, _logger, _settings, _schedulingAnalyzer);
+                _runner = new PreparingTestRunner(reporter, _logger, _settings, _schedulingAnalyzer, true);
                 if (_settings.ParallelTestExecution && isBeingDebugged)
                 {
                     _logger.DebugInfo(
diff --git a/GoogleTestAdapter/Core/Runners/CommandLineGenerator.cs b/GoogleTestAdapter/Core/Runners/CommandLineGenerator.cs
index 80078c5d2..03c54b589 100644
--- a/GoogleTestAdapter/Core/Runners/CommandLineGenerator.cs
+++ b/GoogleTestAdapter/Core/Runners/CommandLineGenerator.cs
@@ -31,16 +31,18 @@ internal Args(IList<TestCase> testCases, string commandLine)
         private readonly string _resultXmlFile;
         private readonly SettingsWrapper _settings;
         private readonly string _userParameters;
+        private readonly bool _reduceToRootTestSuite;
 
         public CommandLineGenerator(IEnumerable<TestCase> testCasesToRun,
             int lengthOfExecutableString, string userParameters, string resultXmlFile,
-            SettingsWrapper settings)
+            SettingsWrapper settings, bool reduceToRootTestSuite)
         {
             _lengthOfExecutableString = lengthOfExecutableString;
             _testCasesToRun = testCasesToRun.ToList();
             _resultXmlFile = resultXmlFile;
             _settings = settings;
             _userParameters = userParameters ?? throw new ArgumentNullException(nameof(userParameters));
+            _reduceToRootTestSuite = reduceToRootTestSuite;
         }
 
         public IEnumerable<Args> GetCommandLines()
@@ -78,9 +80,8 @@ private IEnumerable<Args> GetFinalCommandLines(string baseCommandLine)
                 return commandLines;
             }
 
-            List<string> suitesRunningAllTests = GetSuitesRunningAllTests();
+            List<string> suitesRunningAllTests = _reduceToRootTestSuite ? GetSuitesRunningAllTests() : new List<string>();
             int maxSuiteLength = MaxCommandLength - _lengthOfExecutableString - userParam.Length - 1;
-
             List<List<string>> suiteLists = GetSuiteListsForCommandLines(suitesRunningAllTests, maxSuiteLength);
             
             // lambda to return the base commandline string (including suite filters) and the list of testcases to execute
@@ -286,14 +287,11 @@ private List<string> GetSuitesRunningAllTests()
             foreach (string suite in GetAllSuitesOfTestCasesToRun())
             {
                 List<TestCase> allMatchingTestCasesToBeRun = GetAllMatchingTestCases(_testCasesToRun, suite);
-                TestCaseMetaDataProperty metaData = allMatchingTestCasesToBeRun.First().Properties
-                    .OfType<TestCaseMetaDataProperty>()
-                    .SingleOrDefault();
-                if (metaData == null)
-                    throw new Exception($"Test does not have meta data: {allMatchingTestCasesToBeRun.First()}");
-
-                if (allMatchingTestCasesToBeRun.Count == metaData.NrOfTestCasesInSuite)
+                if (allMatchingTestCasesToBeRun.All(_testCasesToRun.Contains)
+                    && allMatchingTestCasesToBeRun.Count == _testCasesToRun.Count)
+                {
                     suitesRunningAllTests.Add(suite);
+                }
             }
             return suitesRunningAllTests;
         }
diff --git a/GoogleTestAdapter/Core/Runners/ParallelTestRunner.cs b/GoogleTestAdapter/Core/Runners/ParallelTestRunner.cs
index 26cffe381..8bcd19ece 100644
--- a/GoogleTestAdapter/Core/Runners/ParallelTestRunner.cs
+++ b/GoogleTestAdapter/Core/Runners/ParallelTestRunner.cs
@@ -79,7 +79,7 @@ private void RunTests(IEnumerable<TestCase> testCasesToRun, List<Thread> threads
             int threadId = 0;
             foreach (List<TestCase> testcases in splittedTestCasesToRun)
             {
-                var runner = new PreparingTestRunner(threadId++, _frameworkReporter, _logger, _settings.Clone(), _schedulingAnalyzer);
+                var runner = new PreparingTestRunner(threadId++, _frameworkReporter, _logger, _settings.Clone(), _schedulingAnalyzer, false);
                 _testRunners.Add(runner);
 
                 var thread = new Thread(
diff --git a/GoogleTestAdapter/Core/Runners/PreparingTestRunner.cs b/GoogleTestAdapter/Core/Runners/PreparingTestRunner.cs
index 0844f81b1..404d3ea31 100644
--- a/GoogleTestAdapter/Core/Runners/PreparingTestRunner.cs
+++ b/GoogleTestAdapter/Core/Runners/PreparingTestRunner.cs
@@ -25,7 +25,7 @@ public class PreparingTestRunner : ITestRunner
         private readonly string _testDirectory;
 
 
-        public PreparingTestRunner(int threadId, ITestFrameworkReporter reporter, ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer)
+        public PreparingTestRunner(int threadId, ITestFrameworkReporter reporter, ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer, bool reduceToRootTestSuite)
         {
             _logger = logger;
             _settings = settings;
@@ -33,12 +33,12 @@ public PreparingTestRunner(int threadId, ITestFrameworkReporter reporter, ILogge
             _threadName = string.IsNullOrEmpty(threadName) ? "" : $"{threadName} ";
             _threadId = Math.Max(0, threadId);
             _testDirectory = Utils.GetTempDirectory();
-            _innerTestRunner = new SequentialTestRunner(_threadName, _threadId, _testDirectory, reporter, _logger, _settings, schedulingAnalyzer);
+            _innerTestRunner = new SequentialTestRunner(_threadName, _threadId, _testDirectory, reporter, _logger, _settings, schedulingAnalyzer, reduceToRootTestSuite);
         }
 
         public PreparingTestRunner(ITestFrameworkReporter reporter,
-            ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer)
-            : this(-1, reporter, logger, settings, schedulingAnalyzer){
+            ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer, bool reduceToRootTestSuite)
+            : this(-1, reporter, logger, settings, schedulingAnalyzer, reduceToRootTestSuite){
         }
 
 
diff --git a/GoogleTestAdapter/Core/Runners/SequentialTestRunner.cs b/GoogleTestAdapter/Core/Runners/SequentialTestRunner.cs
index d491f914b..7675f8c69 100644
--- a/GoogleTestAdapter/Core/Runners/SequentialTestRunner.cs
+++ b/GoogleTestAdapter/Core/Runners/SequentialTestRunner.cs
@@ -28,10 +28,11 @@ public class SequentialTestRunner : ITestRunner
         private readonly ILogger _logger;
         private readonly SettingsWrapper _settings;
         private readonly SchedulingAnalyzer _schedulingAnalyzer;
+        private readonly bool _reduceToRootTestSuite;
 
         private IProcessExecutor _processExecutor;
 
-        public SequentialTestRunner(string threadName, int threadId, string testDir, ITestFrameworkReporter reporter, ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer)
+        public SequentialTestRunner(string threadName, int threadId, string testDir, ITestFrameworkReporter reporter, ILogger logger, SettingsWrapper settings, SchedulingAnalyzer schedulingAnalyzer, bool reduceToRootTestSuite)
         {
             _threadName = threadName;
             _threadId = threadId;
@@ -40,6 +41,7 @@ public SequentialTestRunner(string threadName, int threadId, string testDir, ITe
             _logger = logger;
             _settings = settings;
             _schedulingAnalyzer = schedulingAnalyzer;
+            _reduceToRootTestSuite = reduceToRootTestSuite;
         }
 
 
@@ -90,7 +92,7 @@ private void RunTestsFromExecutable(string executable, string workingDir,
             string resultXmlFile = Path.GetTempFileName();
             var serializer = new TestDurationSerializer();
 
-            var generator = new CommandLineGenerator(testCasesToRun, executable.Length, userParameters, resultXmlFile, _settings);
+            var generator = new CommandLineGenerator(testCasesToRun, executable.Length, userParameters, resultXmlFile, _settings, _reduceToRootTestSuite);
             foreach (CommandLineGenerator.Args arguments in generator.GetCommandLines())
             {
                 if (_canceled)