Skip to content

Commit 32ab060

Browse files
committed
Add support for --treenode-filter (Fixes #6)
1 parent e72b8fe commit 32ab060

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ There are known limitations on the current support of MTP for xunit 2 which is p
2727
- Tracking issue: https://github.com/Youssef1313/YTest.MTP.XUnit2/issues/4
2828
- `TestMethodIdentifierProperty` is missing the parameter types for parameterized tests.
2929
- Tracking issue: https://github.com/Youssef1313/YTest.MTP.XUnit2/issues/5
30-
- MTP's `--treenode-filter` is not yet supported.
31-
- Tracking issue: https://github.com/Youssef1313/YTest.MTP.XUnit2/issues/6
3230
- MTP's `--maximum-failed-tests` is not yet supported.
3331
- Tracking issue: https://github.com/Youssef1313/YTest.MTP.XUnit2/issues/7
3432

src/YTest.MTP.XUnit2/MTPFramework/TestingPlatformBuilderHook.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Testing.Platform.Builder;
22
using Microsoft.Testing.Platform.Capabilities.TestFramework;
3+
using Microsoft.Testing.Platform.Helpers;
34
using Microsoft.Testing.Platform.Services;
45

56
namespace YTest.MTP.XUnit2;
@@ -17,6 +18,7 @@ public static class TestingPlatformBuilderHook
1718
public static void AddExtensions(ITestApplicationBuilder testApplicationBuilder, string[] arguments)
1819
{
1920
testApplicationBuilder.CommandLine.AddProvider(() => new XUnit2MTPCommandLineProvider());
21+
testApplicationBuilder.AddTreeNodeFilterService(XUnit2MTPExtension.Instance);
2022
var trxReportCapability = new XUnit2MTPTestTrxCapability();
2123
testApplicationBuilder.RegisterTestFramework(
2224
capabilitiesFactory: _ => new TestFrameworkCapabilities(trxReportCapability),
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Testing.Platform.Extensions;
3+
4+
namespace YTest.MTP.XUnit2;
5+
6+
internal sealed class XUnit2MTPExtension : IExtension
7+
{
8+
private XUnit2MTPExtension()
9+
{
10+
}
11+
12+
public static IExtension Instance { get; } = new XUnit2MTPExtension();
13+
14+
public string Uid => nameof(XUnit2MTPTestFramework);
15+
16+
public string Version => "1.0.0";
17+
18+
public string DisplayName => "XUnit 2 Microsoft.Testing.Platform adapter";
19+
20+
public string Description => DisplayName;
21+
22+
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
23+
}

src/YTest.MTP.XUnit2/MTPFramework/XUnit2MTPTestFramework.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public XUnit2MTPTestFramework(
4343
_logger = loggerFactory.CreateLogger<XUnit2MTPTestFramework>();
4444
}
4545

46-
public string Uid => nameof(XUnit2MTPTestFramework);
46+
public string Uid => XUnit2MTPExtension.Instance.Uid;
4747

48-
public string Version => "1.0.0";
48+
public string Version => XUnit2MTPExtension.Instance.Version;
4949

50-
public string DisplayName => "XUnit 2 Microsoft.Testing.Platform adapter";
50+
public string DisplayName => XUnit2MTPExtension.Instance.DisplayName;
5151

52-
public string Description => DisplayName;
52+
public string Description => XUnit2MTPExtension.Instance.Description;
5353

5454
public Type[] DataTypesProduced { get; } =
5555
[
@@ -241,10 +241,36 @@ private static bool MatchesFilter(ITestExecutionFilter mtpFilter, TestCaseFilter
241241
{
242242
#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
243243
null or NopFilter => true,
244+
TreeNodeFilter treeNodeFilter => treeNodeFilter.MatchesFilter(CreateNodePath(test), CreateFilterablePropertyBag(test)),
244245
#pragma warning restore TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
245-
TestNodeUidListFilter testNodeUidListFilter => testNodeUidListFilter.TestNodeUids.Contains(new TestNodeUid(test.UniqueID)),
246+
TestNodeUidListFilter testNodeUidListFilter => testNodeUidListFilter.TestNodeUids.Contains(new TestNodeUid(test.UniqueID)),
246247
_ => throw new NotSupportedException($"Filter type '{mtpFilter.GetType().FullName}' is not supported by XUnit2 MTP adapter."),
247248
};
249+
250+
static string CreateNodePath(ITestCase testCase)
251+
{
252+
var assemblyName = testCase.TestMethod.TestClass.Class.Assembly.SimpleAssemblyName();
253+
var fqn = testCase.TestMethod.TestClass.Class.Name;
254+
var lastDotIndex = fqn.LastIndexOf('.');
255+
var namespaceName = lastDotIndex >= 0 ? fqn.Substring(0, lastDotIndex) : string.Empty;
256+
var classTypeName = lastDotIndex >= 0 ? fqn.Substring(lastDotIndex + 1) : fqn;
257+
var methodName = testCase.TestMethod.Method.Name;
258+
return $"/{assemblyName}/{namespaceName}/{classTypeName}/{methodName}";
259+
}
260+
261+
static PropertyBag CreateFilterablePropertyBag(ITestCase testCase)
262+
{
263+
var propertyBag = new PropertyBag();
264+
foreach (var trait in testCase.Traits)
265+
{
266+
foreach (var traitValue in trait.Value)
267+
{
268+
propertyBag.Add(new TestMetadataProperty(trait.Key, traitValue));
269+
}
270+
}
271+
272+
return propertyBag;
273+
}
248274
}
249275

250276
private async Task<TestAssemblyConfiguration> GetConfigurationAsync(string assemblyPath, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)