|
| 1 | +using Microsoft.VisualStudio.TestPlatform.ObjectModel; |
| 2 | +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace BenchmarkDotNet.TestAdapter; |
| 10 | + |
| 11 | +internal class TestCaseFilter |
| 12 | +{ |
| 13 | + private const string DisplayNameString = "DisplayName"; |
| 14 | + private const string FullyQualifiedNameString = "FullyQualifiedName"; |
| 15 | + |
| 16 | + private readonly HashSet<string> knownTraits; |
| 17 | + private List<string> supportedPropertyNames; |
| 18 | + private readonly ITestCaseFilterExpression? filterExpression; |
| 19 | + private readonly bool successfullyGotFilter; |
| 20 | + private readonly bool isDiscovery; |
| 21 | + |
| 22 | + public TestCaseFilter(IDiscoveryContext discoveryContext, LoggerHelper logger) |
| 23 | + { |
| 24 | + // Traits are not known at discovery time because we load them from benchmarks |
| 25 | + isDiscovery = true; |
| 26 | + knownTraits = []; |
| 27 | + supportedPropertyNames = GetSupportedPropertyNames(); |
| 28 | + successfullyGotFilter = GetTestCaseFilterExpressionFromDiscoveryContext(discoveryContext, logger, out filterExpression); |
| 29 | + } |
| 30 | + |
| 31 | + public TestCaseFilter(IRunContext runContext, LoggerHelper logger, string assemblyFileName, HashSet<string> knownTraits) |
| 32 | + { |
| 33 | + this.knownTraits = knownTraits; |
| 34 | + supportedPropertyNames = GetSupportedPropertyNames(); |
| 35 | + successfullyGotFilter = GetTestCaseFilterExpression(runContext, logger, assemblyFileName, out filterExpression); |
| 36 | + } |
| 37 | + |
| 38 | + public string GetTestCaseFilterValue() |
| 39 | + { |
| 40 | + return successfullyGotFilter |
| 41 | + ? filterExpression?.TestCaseFilterValue ?? "" |
| 42 | + : ""; |
| 43 | + } |
| 44 | + |
| 45 | + public bool MatchTestCase(TestCase testCase) |
| 46 | + { |
| 47 | + if (!successfullyGotFilter) |
| 48 | + { |
| 49 | + // Had an error while getting filter, match no testcase to ensure discovered test list is empty |
| 50 | + return false; |
| 51 | + } |
| 52 | + else if (filterExpression == null) |
| 53 | + { |
| 54 | + // No filter specified, keep every testcase |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + return filterExpression.MatchTestCase(testCase, p => PropertyProvider(testCase, p)); |
| 59 | + } |
| 60 | + |
| 61 | + public object? PropertyProvider(TestCase testCase, string name) |
| 62 | + { |
| 63 | + // Traits filtering |
| 64 | + if (isDiscovery || knownTraits.Contains(name)) |
| 65 | + { |
| 66 | + var result = new List<string>(); |
| 67 | + |
| 68 | + foreach (var trait in GetTraits(testCase)) |
| 69 | + if (string.Equals(trait.Key, name, StringComparison.OrdinalIgnoreCase)) |
| 70 | + result.Add(trait.Value); |
| 71 | + |
| 72 | + if (result.Count > 0) |
| 73 | + return result.ToArray(); |
| 74 | + } |
| 75 | + |
| 76 | + // Property filtering |
| 77 | + switch (name.ToLowerInvariant()) |
| 78 | + { |
| 79 | + // FullyQualifiedName |
| 80 | + case "fullyqualifiedname": |
| 81 | + return testCase.FullyQualifiedName; |
| 82 | + // DisplayName |
| 83 | + case "displayname": |
| 84 | + return testCase.DisplayName; |
| 85 | + default: |
| 86 | + return null; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private bool GetTestCaseFilterExpression(IRunContext runContext, LoggerHelper logger, string assemblyFileName, out ITestCaseFilterExpression? filter) |
| 91 | + { |
| 92 | + filter = null; |
| 93 | + |
| 94 | + try |
| 95 | + { |
| 96 | + filter = runContext.GetTestCaseFilter(supportedPropertyNames, null!); |
| 97 | + return true; |
| 98 | + } |
| 99 | + catch (TestPlatformFormatException e) |
| 100 | + { |
| 101 | + logger.LogWarning("{0}: Exception filtering tests: {1}", Path.GetFileNameWithoutExtension(assemblyFileName), e.Message); |
| 102 | + return false; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private bool GetTestCaseFilterExpressionFromDiscoveryContext(IDiscoveryContext discoveryContext, LoggerHelper logger, out ITestCaseFilterExpression? filter) |
| 107 | + { |
| 108 | + filter = null; |
| 109 | + |
| 110 | + if (discoveryContext is IRunContext runContext) |
| 111 | + { |
| 112 | + try |
| 113 | + { |
| 114 | + filter = runContext.GetTestCaseFilter(supportedPropertyNames, null!); |
| 115 | + return true; |
| 116 | + } |
| 117 | + catch (TestPlatformException e) |
| 118 | + { |
| 119 | + logger.LogWarning("Exception filtering tests: {0}", e.Message); |
| 120 | + return false; |
| 121 | + } |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + try |
| 126 | + { |
| 127 | + // GetTestCaseFilter is present on DiscoveryContext but not in IDiscoveryContext interface |
| 128 | + var method = discoveryContext.GetType().GetRuntimeMethod("GetTestCaseFilter", [typeof(IEnumerable<string>), typeof(Func<string, TestProperty>)]); |
| 129 | + filter = (ITestCaseFilterExpression)method?.Invoke(discoveryContext, [supportedPropertyNames, null])!; |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + catch (TargetInvocationException e) |
| 134 | + { |
| 135 | + if (e?.InnerException is TestPlatformException ex) |
| 136 | + { |
| 137 | + logger.LogWarning("Exception filtering tests: {0}", ex.InnerException.Message ?? ""); |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + throw e!.InnerException; |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private List<string> GetSupportedPropertyNames() |
| 147 | + { |
| 148 | + // Returns the set of well-known property names usually used with the Test Plugins (Used Test Traits + DisplayName + FullyQualifiedName) |
| 149 | + if (supportedPropertyNames == null) |
| 150 | + { |
| 151 | + supportedPropertyNames = knownTraits.ToList(); |
| 152 | + supportedPropertyNames.Add(DisplayNameString); |
| 153 | + supportedPropertyNames.Add(FullyQualifiedNameString); |
| 154 | + } |
| 155 | + |
| 156 | + return supportedPropertyNames; |
| 157 | + } |
| 158 | + |
| 159 | + private static IEnumerable<KeyValuePair<string, string>> GetTraits(TestCase testCase) |
| 160 | + { |
| 161 | + var traitProperty = TestProperty.Find("TestObject.Traits"); |
| 162 | + return traitProperty != null |
| 163 | + ? testCase.GetPropertyValue(traitProperty, Array.Empty<KeyValuePair<string, string>>()) |
| 164 | + : []; |
| 165 | + } |
| 166 | +} |
0 commit comments