From 8faaa3a0c4eb15611f23a42cc808b04b0d4afa33 Mon Sep 17 00:00:00 2001
From: Mayank Bansal <maban@microsoft.com>
Date: Thu, 14 Dec 2017 16:07:32 +0530
Subject: [PATCH 1/3] Throwing exception only if it is of type
 TestPlatformFormatException

---
 src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs b/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
index 118f645648..a1c4836a64 100644
--- a/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
+++ b/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
@@ -119,10 +119,17 @@ private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscove
                 MethodInfo methodGetTestCaseFilter = context.GetType().GetRuntimeMethod("GetTestCaseFilter", new[] { typeof(IEnumerable<string>), typeof(Func<string, TestProperty>) });
                 return (ITestCaseFilterExpression)methodGetTestCaseFilter?.Invoke(context, new object[] { this.supportedProperties.Keys, (Func<string, TestProperty>)this.PropertyProvider });
             }
-            catch (TargetInvocationException ex)
+            catch (Exception ex)
             {
-                throw ex.InnerException;
+                // In case of UWP .Net Native Tool Chain compilation. Invoking methods via Reflection doesn't work, hence discovery always fails.
+                // Hence throwing exception only if it is of type TestPlatformFormatException
+                if (ex.InnerException is TestPlatformFormatException)
+                {
+                    throw ex.InnerException;
+                }
             }
+
+            return null;
         }
     }
 }
\ No newline at end of file

From 2bf3d47e4ec4981824053ffd4e104f46e7acfbe5 Mon Sep 17 00:00:00 2001
From: Mayank Bansal <maban@microsoft.com>
Date: Fri, 15 Dec 2017 13:13:30 +0530
Subject: [PATCH 2/3] test

---
 .../Discovery/UnitTestDiscovererTests.cs      | 20 +++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
index eae996e215..06c0ca7951 100644
--- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
+++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
@@ -339,6 +339,26 @@ public void SendTestCasesShouldNotSendAnyTestCasesIfFilterError()
             this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M2")), Times.Never);
         }
 
+        /// <summary>
+        /// Send test cases should not send any test cases if filter parsing error.
+        /// </summary>
+        [TestMethodV1]
+        public void SendTestCasesShouldSendAllTestCasesIfTestPlatformFormatExceptionNotThrown()
+        {
+            TestableDiscoveryContextWithGetTestCaseFilter discoveryContext = new TestableDiscoveryContextWithGetTestCaseFilter(() => { throw new NotImplementedException("DummyException"); });
+
+            var test1 = new UnitTestElement(new TestMethod("M1", "C", "A", false));
+            var test2 = new UnitTestElement(new TestMethod("M2", "C", "A", false));
+            var testElements = new List<UnitTestElement> { test1, test2 };
+
+            // Action
+            this.unitTestDiscoverer.SendTestCases(Source, testElements, this.mockTestCaseDiscoverySink.Object, discoveryContext, this.mockMessageLogger.Object);
+
+            // Assert.
+            this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M1")), Times.Once);
+            this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M2")), Times.Once);
+        }
+
         private void SetupNavigation(string source, UnitTestElement test, string className, string methodName)
         {
             var testNavigationData = new DummyNavigationData("DummyFileName.cs", 1, 10);

From b77c79a3fcb0fe145eacfc36f356104642b47c5c Mon Sep 17 00:00:00 2001
From: Mayank Bansal <maban@microsoft.com>
Date: Fri, 15 Dec 2017 14:25:16 +0530
Subject: [PATCH 3/3] Fail discovery if any exception thrown in
 IDiscoveryContext.GetTestCaseFilter

---
 .../MSTest.CoreAdapter/TestMethodFilter.cs    | 10 ++++++----
 .../Discovery/UnitTestDiscovererTests.cs      | 20 -------------------
 2 files changed, 6 insertions(+), 24 deletions(-)

diff --git a/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs b/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
index a1c4836a64..154be33e22 100644
--- a/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
+++ b/src/Adapter/MSTest.CoreAdapter/TestMethodFilter.cs
@@ -46,7 +46,7 @@ internal ITestCaseFilterExpression GetFilterExpression(IDiscoveryContext context
             {
                 try
                 {
-                    filter = (context is IRunContext) ? this.GetTestCaseFilterFromRunContext(context as IRunContext) : this.GetTestCaseFilterFromDiscoveryContext(context);
+                    filter = (context is IRunContext) ? this.GetTestCaseFilterFromRunContext(context as IRunContext) : this.GetTestCaseFilterFromDiscoveryContext(context, logger);
                 }
                 catch (TestPlatformFormatException ex)
                 {
@@ -111,7 +111,7 @@ private ITestCaseFilterExpression GetTestCaseFilterFromRunContext(IRunContext co
         /// </summary>
         /// <param name="context">Discovery context</param>
         /// <returns>Filter expression.</returns>
-        private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscoveryContext context)
+        private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscoveryContext context, IMessageLogger logger)
         {
             try
             {
@@ -122,11 +122,13 @@ private ITestCaseFilterExpression GetTestCaseFilterFromDiscoveryContext(IDiscove
             catch (Exception ex)
             {
                 // In case of UWP .Net Native Tool Chain compilation. Invoking methods via Reflection doesn't work, hence discovery always fails.
-                // Hence throwing exception only if it is of type TestPlatformFormatException
-                if (ex.InnerException is TestPlatformFormatException)
+                // Hence throwing exception only if it is of type TargetInvocationException(i.e. Method got invoked but something went wrong in GetTestCaseFilter Method)
+                if (ex is TargetInvocationException)
                 {
                     throw ex.InnerException;
                 }
+
+                logger.SendMessage(TestMessageLevel.Warning, ex.Message);
             }
 
             return null;
diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
index 06c0ca7951..eae996e215 100644
--- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
+++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Discovery/UnitTestDiscovererTests.cs
@@ -339,26 +339,6 @@ public void SendTestCasesShouldNotSendAnyTestCasesIfFilterError()
             this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M2")), Times.Never);
         }
 
-        /// <summary>
-        /// Send test cases should not send any test cases if filter parsing error.
-        /// </summary>
-        [TestMethodV1]
-        public void SendTestCasesShouldSendAllTestCasesIfTestPlatformFormatExceptionNotThrown()
-        {
-            TestableDiscoveryContextWithGetTestCaseFilter discoveryContext = new TestableDiscoveryContextWithGetTestCaseFilter(() => { throw new NotImplementedException("DummyException"); });
-
-            var test1 = new UnitTestElement(new TestMethod("M1", "C", "A", false));
-            var test2 = new UnitTestElement(new TestMethod("M2", "C", "A", false));
-            var testElements = new List<UnitTestElement> { test1, test2 };
-
-            // Action
-            this.unitTestDiscoverer.SendTestCases(Source, testElements, this.mockTestCaseDiscoverySink.Object, discoveryContext, this.mockMessageLogger.Object);
-
-            // Assert.
-            this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M1")), Times.Once);
-            this.mockTestCaseDiscoverySink.Verify(ds => ds.SendTestCase(It.Is<TestCase>(tc => tc.FullyQualifiedName == "C.M2")), Times.Once);
-        }
-
         private void SetupNavigation(string source, UnitTestElement test, string className, string methodName)
         {
             var testNavigationData = new DummyNavigationData("DummyFileName.cs", 1, 10);
