From 730f6baadb0913fc986d7b7a9a119c1f26add9f7 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Thu, 5 Apr 2018 14:18:28 +0530 Subject: [PATCH 1/2] Understanding category attribute from adapter --- .../TestDiscoveryExtensionManager.cs | 14 +++- .../TestDiscovererPluginInformation.cs | 34 ++++++++- .../Interfaces/ITestDiscovererCapabilities.cs | 7 ++ .../Utilities/AssemblyType.cs | 12 +++ .../TestDiscoveryExtensionManagerTests.cs | 9 +++ .../Utilities/LazyExtensionTests.cs | 21 +++-- .../TestDiscovererPluginInformationTests.cs | 76 ++++++++++++++++++- 7 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs diff --git a/src/Microsoft.TestPlatform.Common/ExtensionFramework/TestDiscoveryExtensionManager.cs b/src/Microsoft.TestPlatform.Common/ExtensionFramework/TestDiscoveryExtensionManager.cs index 727ed5dab2..e5fa418ea9 100644 --- a/src/Microsoft.TestPlatform.Common/ExtensionFramework/TestDiscoveryExtensionManager.cs +++ b/src/Microsoft.TestPlatform.Common/ExtensionFramework/TestDiscoveryExtensionManager.cs @@ -8,6 +8,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities; using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; @@ -169,7 +170,7 @@ internal class TestDiscovererMetadata : ITestDiscovererCapabilities /// /// The file Extensions. /// The default Executor Uri. - public TestDiscovererMetadata(IReadOnlyCollection fileExtensions, string defaultExecutorUri) + public TestDiscovererMetadata(IReadOnlyCollection fileExtensions, string defaultExecutorUri, AssemblyType assemblyType = default(AssemblyType)) { if (fileExtensions != null && fileExtensions.Count > 0) { @@ -180,6 +181,8 @@ public TestDiscovererMetadata(IReadOnlyCollection fileExtensions, string { this.DefaultExecutorUri = new Uri(defaultExecutorUri); } + + this.AssemblyType = assemblyType; } /// @@ -199,5 +202,14 @@ public Uri DefaultExecutorUri get; private set; } + + /// + /// Gets assembly type supported by the discoverer. + /// + public AssemblyType AssemblyType + { + get; + private set; + } } } diff --git a/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs b/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs index c1d78f3e97..371172ffd9 100644 --- a/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs +++ b/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs @@ -5,9 +5,11 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilitie { using System; using System.Collections.Generic; + using System.ComponentModel; using System.Linq; using System.Reflection; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using ObjectModel; /// @@ -26,6 +28,7 @@ public TestDiscovererPluginInformation(Type testDiscovererType) { this.FileExtensions = GetFileExtensions(testDiscovererType); this.DefaultExecutorUri = GetDefaultExecutorUri(testDiscovererType); + this.AssemblyType = GetAssemblyType(testDiscovererType); } } @@ -36,7 +39,7 @@ public override ICollection Metadata { get { - return new object[] { this.FileExtensions, this.DefaultExecutorUri }; + return new object[] { this.FileExtensions, this.DefaultExecutorUri, this.AssemblyType }; } } @@ -58,6 +61,15 @@ public string DefaultExecutorUri private set; } + /// + /// Gets the assembly type supported by discoverer plugin. + /// + public AssemblyType AssemblyType + { + get; + private set; + } + /// /// Helper to get file extensions from the FileExtensionAttribute on the discover plugin. /// @@ -105,5 +117,25 @@ private static string GetDefaultExecutorUri(Type testDiscovererType) return result; } + + /// + /// Helper to get the supported assembly type from the CategoryAttribute on the discover plugin. + /// + /// The test discoverer Type. + /// Supported assembly type. + private AssemblyType GetAssemblyType(Type testDiscovererType) + { + var assemblyType = default(AssemblyType); + + // Get Category + var attributes = testDiscovererType.GetTypeInfo().GetCustomAttributes(typeof(CategoryAttribute), false).ToArray(); + var category = attributes != null && attributes.Length > 0 ? + ((CategoryAttribute)attributes[0]).Category : + default(string); + + // Get assembly type from category. + Enum.TryParse(category, true, out assemblyType); + return assemblyType; + } } } diff --git a/src/Microsoft.TestPlatform.Common/Interfaces/ITestDiscovererCapabilities.cs b/src/Microsoft.TestPlatform.Common/Interfaces/ITestDiscovererCapabilities.cs index a528b7317e..d7f7ec2a7a 100644 --- a/src/Microsoft.TestPlatform.Common/Interfaces/ITestDiscovererCapabilities.cs +++ b/src/Microsoft.TestPlatform.Common/Interfaces/ITestDiscovererCapabilities.cs @@ -6,6 +6,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Interfaces using System; using System.Collections.Generic; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; + /// /// Metadata that is available from Test Discoverers. /// @@ -20,5 +22,10 @@ public interface ITestDiscovererCapabilities /// Default executor Uri for this discoverer /// Uri DefaultExecutorUri { get; } + + /// + /// Assembly type that the test discoverer supports. + /// + AssemblyType AssemblyType { get; } } } diff --git a/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs b/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs new file mode 100644 index 0000000000..e9b77323c3 --- /dev/null +++ b/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities +{ + public enum AssemblyType + { + NativeAndManaged, + Native, + Managed + } +} diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/TestDiscoveryExtensionManagerTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/TestDiscoveryExtensionManagerTests.cs index e78abff7e3..b7495ad485 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/TestDiscoveryExtensionManagerTests.cs +++ b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/TestDiscoveryExtensionManagerTests.cs @@ -8,6 +8,7 @@ namespace TestPlatform.Common.UnitTests.ExtensionFramework using System.Reflection; using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] @@ -124,5 +125,13 @@ public void TestDiscovererMetadataCtorSetsDefaultUri() Assert.AreEqual("executor://helloworld/", metadata.DefaultExecutorUri.AbsoluteUri); } + + [TestMethod] + public void TestDiscovererMetadataCtorSetsAssemblyType() + { + var metadata = new TestDiscovererMetadata(null, "executor://helloworld", AssemblyType.Native); + + Assert.AreEqual(AssemblyType.Native, metadata.AssemblyType); + } } } diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/LazyExtensionTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/LazyExtensionTests.cs index b493ac1b79..e4a3dc41a7 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/LazyExtensionTests.cs +++ b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/LazyExtensionTests.cs @@ -4,16 +4,18 @@ namespace TestPlatform.Common.UnitTests.ExtensionFramework.Utilities { using System; + using System.Collections.Generic; + using System.ComponentModel; using System.Linq; using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities; + using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using Microsoft.VisualStudio.TestPlatform.ObjectModel; - using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; - using Microsoft.VisualStudio.TestPlatform.Common.Interfaces; - using Moq; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; - using System.Collections.Generic; + using Microsoft.VisualStudio.TestTools.UnitTesting; + using Moq; [TestClass] public class LazyExtensionTests @@ -96,6 +98,7 @@ public void MetadataShouldCreateMetadataFromMetadataType() Assert.AreEqual(typeof(DummyDiscovererCapability), metadata.GetType()); CollectionAssert.AreEqual(new List { "csv" }, (metadata as ITestDiscovererCapabilities).FileExtension.ToArray()); Assert.AreEqual("executor://unittestexecutor/", (metadata as ITestDiscovererCapabilities).DefaultExecutorUri.AbsoluteUri); + Assert.AreEqual(AssemblyType.Native, (metadata as ITestDiscovererCapabilities).AssemblyType); } #endregion @@ -116,16 +119,24 @@ public Uri DefaultExecutorUri private set; } - public DummyDiscovererCapability(List fileExtensions, string executorURI) + public AssemblyType AssemblyType + { + get; + private set; + } + + public DummyDiscovererCapability(List fileExtensions, string executorURI, AssemblyType assemblyType) { this.FileExtension = fileExtensions; this.DefaultExecutorUri = new Uri(executorURI); + this.AssemblyType = assemblyType; } } [FileExtension("csv")] [DefaultExecutorUri("executor://unittestexecutor")] + [Category("native")] private class DummyExtension : ITestDiscoverer { public void DiscoverTests(IEnumerable sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink) diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs index de7e59bce6..b100009085 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs +++ b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs @@ -4,13 +4,15 @@ namespace TestPlatform.Common.UnitTests.ExtensionFramework.Utilities { using System; - using System.Linq; using System.Collections.Generic; + using System.ComponentModel; + using System.Linq; using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework.Utilities; - using Microsoft.VisualStudio.TestTools.UnitTesting; + using Microsoft.VisualStudio.TestPlatform.Common.Utilities; using Microsoft.VisualStudio.TestPlatform.ObjectModel; - + using Microsoft.VisualStudio.TestTools.UnitTesting; + [TestClass] public class TestDiscovererPluginInformationTests { @@ -52,6 +54,41 @@ public void FileExtensionsShouldReturnSupportedFileExtensionsForADiscoverer() CollectionAssert.AreEqual(new List {"csv", "docx"}, this.testPluginInformation.FileExtensions); } + [TestMethod] + public void AssemblyTypeShouldReturnNativeAndManagedIfDiscovererHasNoCategory() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNoCategory)); + Assert.AreEqual(AssemblyType.NativeAndManaged, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnNativeIfDiscovererHasNativeCategory() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNativeCategory)); + Assert.AreEqual(AssemblyType.Native, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnManagedIfDiscovererHasManagedCategory() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithManagedCategory)); + Assert.AreEqual(AssemblyType.Managed, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnNativeAndManagedIfDiscovererHasUnknownCategory() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithUnknownCategory)); + Assert.AreEqual(AssemblyType.NativeAndManaged, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnAssemblyTypeIfDiscovererHasCategoryInArbitCasing() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithArbitCasedCategory)); + Assert.AreEqual(AssemblyType.Native, this.testPluginInformation.AssemblyType); + } + [TestMethod] public void DefaultExecutorUriShouldReturnEmptyListIfADiscovererDoesNotHaveOne() { @@ -68,7 +105,7 @@ public void DefaultExecutorUriShouldReturnDefaultExecutorUriOfADiscoverer() } [TestMethod] - public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri() + public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUriAndAssemblyType() { this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovererWithTwoFileExtensions)); @@ -77,6 +114,7 @@ public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri() CollectionAssert.AreEqual(expectedFileExtensions, (testPluginMetada[0] as List).ToArray()); Assert.AreEqual("csvexecutor", testPluginMetada[1] as string); + Assert.AreEqual(AssemblyType.Managed, Enum.Parse(typeof(AssemblyType), testPluginMetada[2].ToString())); } } @@ -86,6 +124,35 @@ public class DummyTestDiscovererWithNoFileExtensions { } + [FileExtension(".dll")] + public class DummyTestDiscovereWithNoCategory + { + } + + [FileExtension(".js")] + [Category("native")] + public class DummyTestDiscovereWithNativeCategory + { + } + + [FileExtension(".dll")] + [Category("managed")] + public class DummyTestDiscovereWithManagedCategory + { + } + + [FileExtension(".dll")] + [Category("arbitValue")] + public class DummyTestDiscovereWithUnknownCategory + { + } + + [FileExtension(".dll")] + [Category("NatIVe")] + public class DummyTestDiscovereWithArbitCasedCategory + { + } + [FileExtension("csv")] [DefaultExecutorUri("csvexecutor")] public class DummyTestDiscovererWithOneFileExtensions @@ -94,6 +161,7 @@ public class DummyTestDiscovererWithOneFileExtensions [FileExtension("csv")] [FileExtension("docx")] + [Category("managed")] [DefaultExecutorUri("csvexecutor")] public class DummyTestDiscovererWithTwoFileExtensions { From 6a6719dcb3cabf5ed7e123b18a8e817b809b8405 Mon Sep 17 00:00:00 2001 From: abhishkk Date: Thu, 5 Apr 2018 15:54:03 +0530 Subject: [PATCH 2/2] review comments --- .../TestDiscovererPluginInformation.cs | 6 ++-- .../Utilities/AssemblyType.cs | 2 +- .../TestDiscovererPluginInformationTests.cs | 34 ++++++++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs b/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs index 371172ffd9..60209d116f 100644 --- a/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs +++ b/src/Microsoft.TestPlatform.Common/ExtensionFramework/Utilities/TestDiscovererPluginInformation.cs @@ -128,10 +128,8 @@ private AssemblyType GetAssemblyType(Type testDiscovererType) var assemblyType = default(AssemblyType); // Get Category - var attributes = testDiscovererType.GetTypeInfo().GetCustomAttributes(typeof(CategoryAttribute), false).ToArray(); - var category = attributes != null && attributes.Length > 0 ? - ((CategoryAttribute)attributes[0]).Category : - default(string); + var attribute = testDiscovererType.GetTypeInfo().GetCustomAttribute(typeof(CategoryAttribute)); + var category = (attribute as CategoryAttribute)?.Category; // Get assembly type from category. Enum.TryParse(category, true, out assemblyType); diff --git a/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs b/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs index e9b77323c3..045c6177bb 100644 --- a/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs +++ b/src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs @@ -5,7 +5,7 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Utilities { public enum AssemblyType { - NativeAndManaged, + None, Native, Managed } diff --git a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs index b100009085..1895ca3d4c 100644 --- a/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs +++ b/test/Microsoft.TestPlatform.Common.UnitTests/ExtensionFramework/Utilities/TestDiscovererPluginInformationTests.cs @@ -55,10 +55,24 @@ public void FileExtensionsShouldReturnSupportedFileExtensionsForADiscoverer() } [TestMethod] - public void AssemblyTypeShouldReturnNativeAndManagedIfDiscovererHasNoCategory() + public void AssemblyTypeShouldReturnNoneIfDiscovererHasNoCategory() { this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNoCategory)); - Assert.AreEqual(AssemblyType.NativeAndManaged, this.testPluginInformation.AssemblyType); + Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnNoneIfDiscovererHasCategoryWithNoValue() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithCategoryHavingNoValue)); + Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType); + } + + [TestMethod] + public void AssemblyTypeShouldReturnNoneIfDiscovererHasCategoryWithEmptyValue() + { + this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithCategoryHavingEmptyValue)); + Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType); } [TestMethod] @@ -76,10 +90,10 @@ public void AssemblyTypeShouldReturnManagedIfDiscovererHasManagedCategory() } [TestMethod] - public void AssemblyTypeShouldReturnNativeAndManagedIfDiscovererHasUnknownCategory() + public void AssemblyTypeShouldReturnNoneIfDiscovererHasUnknownCategory() { this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithUnknownCategory)); - Assert.AreEqual(AssemblyType.NativeAndManaged, this.testPluginInformation.AssemblyType); + Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType); } [TestMethod] @@ -129,6 +143,18 @@ public class DummyTestDiscovereWithNoCategory { } + [FileExtension(".dll")] + [Category] + public class DummyTestDiscovereWithCategoryHavingNoValue + { + } + + [FileExtension(".dll")] + [Category] + public class DummyTestDiscovereWithCategoryHavingEmptyValue + { + } + [FileExtension(".js")] [Category("native")] public class DummyTestDiscovereWithNativeCategory