Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understanding category attribute from adapter #1528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -169,7 +170,7 @@ internal class TestDiscovererMetadata : ITestDiscovererCapabilities
/// </summary>
/// <param name="fileExtensions"> The file Extensions. </param>
/// <param name="defaultExecutorUri"> The default Executor Uri. </param>
public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string defaultExecutorUri)
public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string defaultExecutorUri, AssemblyType assemblyType = default(AssemblyType))
{
if (fileExtensions != null && fileExtensions.Count > 0)
{
Expand All @@ -180,6 +181,8 @@ public TestDiscovererMetadata(IReadOnlyCollection<string> fileExtensions, string
{
this.DefaultExecutorUri = new Uri(defaultExecutorUri);
}

this.AssemblyType = assemblyType;
}

/// <summary>
Expand All @@ -199,5 +202,14 @@ public Uri DefaultExecutorUri
get;
private set;
}

/// <summary>
/// Gets assembly type supported by the discoverer.
/// </summary>
public AssemblyType AssemblyType
{
get;
private set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -26,6 +28,7 @@ public TestDiscovererPluginInformation(Type testDiscovererType)
{
this.FileExtensions = GetFileExtensions(testDiscovererType);
this.DefaultExecutorUri = GetDefaultExecutorUri(testDiscovererType);
this.AssemblyType = GetAssemblyType(testDiscovererType);
}
}

Expand All @@ -36,7 +39,7 @@ public override ICollection<Object> Metadata
{
get
{
return new object[] { this.FileExtensions, this.DefaultExecutorUri };
return new object[] { this.FileExtensions, this.DefaultExecutorUri, this.AssemblyType };
}
}

Expand All @@ -58,6 +61,15 @@ public string DefaultExecutorUri
private set;
}

/// <summary>
/// Gets the assembly type supported by discoverer plugin.
/// </summary>
public AssemblyType AssemblyType
{
get;
private set;
}

/// <summary>
/// Helper to get file extensions from the FileExtensionAttribute on the discover plugin.
/// </summary>
Expand Down Expand Up @@ -105,5 +117,23 @@ private static string GetDefaultExecutorUri(Type testDiscovererType)

return result;
}

/// <summary>
/// Helper to get the supported assembly type from the CategoryAttribute on the discover plugin.
/// </summary>
/// <param name="testDiscovererType"> The test discoverer Type. </param>
/// <returns> Supported assembly type. </returns>
private AssemblyType GetAssemblyType(Type testDiscovererType)
{
var assemblyType = default(AssemblyType);

// Get Category
var attribute = testDiscovererType.GetTypeInfo().GetCustomAttribute(typeof(CategoryAttribute));
var category = (attribute as CategoryAttribute)?.Category;

// Get assembly type from category.
Enum.TryParse(category, true, out assemblyType);
return assemblyType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Microsoft.VisualStudio.TestPlatform.Common.Interfaces
using System;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestPlatform.Common.Utilities;

/// <summary>
/// Metadata that is available from Test Discoverers.
/// </summary>
Expand All @@ -20,5 +22,10 @@ public interface ITestDiscovererCapabilities
/// Default executor Uri for this discoverer
/// </summary>
Uri DefaultExecutorUri { get; }

/// <summary>
/// Assembly type that the test discoverer supports.
/// </summary>
AssemblyType AssemblyType { get; }
}
}
12 changes: 12 additions & 0 deletions src/Microsoft.TestPlatform.Common/Utilities/AssemblyType.cs
Original file line number Diff line number Diff line change
@@ -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
{
None,
Native,
Managed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -124,5 +125,13 @@ public void TestDiscovererMetadataCtorSetsDefaultUri()

Assert.AreEqual("executor://helloworld/", metadata.DefaultExecutorUri.AbsoluteUri);
}

[TestMethod]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for default case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum can't be null ro empty. Thus checking one test that whatever is passed is set enough.

public void TestDiscovererMetadataCtorSetsAssemblyType()
{
var metadata = new TestDiscovererMetadata(null, "executor://helloworld", AssemblyType.Native);

Assert.AreEqual(AssemblyType.Native, metadata.AssemblyType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,6 +98,7 @@ public void MetadataShouldCreateMetadataFromMetadataType()
Assert.AreEqual(typeof(DummyDiscovererCapability), metadata.GetType());
CollectionAssert.AreEqual(new List<string> { "csv" }, (metadata as ITestDiscovererCapabilities).FileExtension.ToArray());
Assert.AreEqual("executor://unittestexecutor/", (metadata as ITestDiscovererCapabilities).DefaultExecutorUri.AbsoluteUri);
Assert.AreEqual(AssemblyType.Native, (metadata as ITestDiscovererCapabilities).AssemblyType);
}

#endregion
Expand All @@ -116,16 +119,24 @@ public Uri DefaultExecutorUri
private set;
}

public DummyDiscovererCapability(List<string> fileExtensions, string executorURI)
public AssemblyType AssemblyType
{
get;
private set;
}

public DummyDiscovererCapability(List<string> 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<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -52,6 +54,55 @@ public void FileExtensionsShouldReturnSupportedFileExtensionsForADiscoverer()
CollectionAssert.AreEqual(new List<string> {"csv", "docx"}, this.testPluginInformation.FileExtensions);
}

[TestMethod]
public void AssemblyTypeShouldReturnNoneIfDiscovererHasNoCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithNoCategory));
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]
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 AssemblyTypeShouldReturnNoneIfDiscovererHasUnknownCategory()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithUnknownCategory));
Assert.AreEqual(AssemblyType.None, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void AssemblyTypeShouldReturnAssemblyTypeIfDiscovererHasCategoryInArbitCasing()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovereWithArbitCasedCategory));
Assert.AreEqual(AssemblyType.Native, this.testPluginInformation.AssemblyType);
}

[TestMethod]
public void DefaultExecutorUriShouldReturnEmptyListIfADiscovererDoesNotHaveOne()
{
Expand All @@ -68,7 +119,7 @@ public void DefaultExecutorUriShouldReturnDefaultExecutorUriOfADiscoverer()
}

[TestMethod]
public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri()
public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUriAndAssemblyType()
{
this.testPluginInformation = new TestDiscovererPluginInformation(typeof(DummyTestDiscovererWithTwoFileExtensions));

Expand All @@ -77,6 +128,7 @@ public void MetadataShouldReturnFileExtensionsAndDefaultExecutorUri()

CollectionAssert.AreEqual(expectedFileExtensions, (testPluginMetada[0] as List<string>).ToArray());
Assert.AreEqual("csvexecutor", testPluginMetada[1] as string);
Assert.AreEqual(AssemblyType.Managed, Enum.Parse(typeof(AssemblyType), testPluginMetada[2].ToString()));
}
}

Expand All @@ -86,6 +138,47 @@ public class DummyTestDiscovererWithNoFileExtensions
{
}

[FileExtension(".dll")]
public class DummyTestDiscovereWithNoCategory
{
}

[FileExtension(".dll")]
[Category]
public class DummyTestDiscovereWithCategoryHavingNoValue
{
}

[FileExtension(".dll")]
[Category]
public class DummyTestDiscovereWithCategoryHavingEmptyValue
{
}

[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
Expand All @@ -94,6 +187,7 @@ public class DummyTestDiscovererWithOneFileExtensions

[FileExtension("csv")]
[FileExtension("docx")]
[Category("managed")]
[DefaultExecutorUri("csvexecutor")]
public class DummyTestDiscovererWithTwoFileExtensions
{
Expand Down