diff --git a/Directory.Packages.props b/Directory.Packages.props index f0ded72..d17afb4 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -11,7 +11,6 @@ - diff --git a/build.cake b/build.cake index ab02548..94d411b 100644 --- a/build.cake +++ b/build.cake @@ -20,8 +20,7 @@ BuildSettings.Packages.Add(new NuGetPackage( checks: new PackageCheck[] { HasFile("LICENSE.txt"), HasDirectory("lib/net462").WithFile("nunit.engine.api.dll"), - HasDirectory("lib/netstandard2.0").WithFile("nunit.engine.api.dll"), - HasDependency("NUnit.Extensibility.Api") + HasDirectory("lib/netstandard2.0").WithFile("nunit.engine.api.dll") }, symbols: new PackageCheck[] { HasDirectory("lib/net462").WithFile("nunit.engine.api.pdb"), diff --git a/src/NUnit.Engine.Api.Tests/ExtensionInfoApiTests.cs b/src/NUnit.Engine.Api.Tests/ExtensionInfoApiTests.cs new file mode 100644 index 0000000..b7b1586 --- /dev/null +++ b/src/NUnit.Engine.Api.Tests/ExtensionInfoApiTests.cs @@ -0,0 +1,238 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.Versioning; +using NUnit.Framework; + +namespace NUnit.Engine.Api +{ + /// + /// Runners list extensions through IExtensionService using plain data types + /// defined here, so the engine API doesn't depend on NUnit.Extensibility.Api + /// (nunit/nunit-console#1980). These tests pin that shape of the API. They use + /// reflection because the API is frozen at 4.0.0.0 through 4.x: its shape is + /// the contract. + /// + public class ExtensionInfoApiTests + { + private static readonly Assembly ApiAssembly = typeof(ITestEngine).Assembly; + + private const string ExtensibilityAssemblyName = "NUnit.Extensibility.Api"; + private const string ExtensibilityNamespace = "NUnit.Extensibility"; + private const string IsExternalInitTypeName = "System.Runtime.CompilerServices.IsExternalInit"; + + #region No dependency on NUnit.Extensibility.Api + + [Test] + public void ApiAssemblyDoesNotReferenceExtensibilityApi() + { + var references = ApiAssembly.GetReferencedAssemblies().Select(a => a.Name); + + Assert.That(references, Has.No.Member(ExtensibilityAssemblyName)); + } + + [Test] + public void NoPublicMemberUsesExtensibilityTypes() + { + var offenders = new List(); + + foreach (var type in ApiAssembly.GetExportedTypes()) + { + if (type.BaseType is not null && IsExtensibilityType(type.BaseType)) + offenders.Add($"{type.FullName} : {type.BaseType.FullName}"); + + foreach (var iface in type.GetInterfaces().Where(IsExtensibilityType)) + offenders.Add($"{type.FullName} : {iface.FullName}"); + + foreach (var member in type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)) + { + foreach (var used in TypesUsedBy(member).Where(IsExtensibilityType)) + offenders.Add($"{type.FullName}.{member.Name} uses {used.FullName}"); + } + } + + Assert.That(offenders, Is.Empty); + } + + #endregion + + #region IExtensionService + + [TestCase(nameof(IExtensionService.ExtensionPoints), "NUnit.Engine.ExtensionPointInfo")] + [TestCase(nameof(IExtensionService.Extensions), "NUnit.Engine.ExtensionInfo")] + public void ExtensionServiceListsInfoTypes(string propertyName, string elementTypeName) + { + var elementType = ApiAssembly.GetType(elementTypeName); + Assert.That(elementType, Is.Not.Null, $"{elementTypeName} is missing"); + + var property = typeof(IExtensionService).GetProperty(propertyName); + Assert.That(property, Is.Not.Null, $"IExtensionService.{propertyName} is missing"); + Assert.That(property.PropertyType, Is.EqualTo(typeof(IEnumerable<>).MakeGenericType(elementType))); + Assert.That(property.CanWrite, Is.False); + } + + [TestCase("GetExtensionPoint")] + [TestCase("GetExtensionNodes")] + public void ExtensionServiceHasNoLookupByPathOrType(string methodName) + { + var methods = typeof(IExtensionService).GetMethods().Where(m => m.Name == methodName); + + Assert.That(methods, Is.Empty, $"IExtensionService.{methodName} should be removed"); + } + + #endregion + + #region ExtensionInfoStatus + + private static readonly string[] ExpectedStatusNames = { "Unloaded", "Loaded", "Error", "Unknown" }; + + [Test] + public void ExtensionInfoStatusHasExpectedValues() + { + var type = ApiAssembly.GetType("NUnit.Engine.ExtensionInfoStatus"); + + Assert.That(type, Is.Not.Null, "NUnit.Engine.ExtensionInfoStatus is missing"); + Assert.That(type.IsEnum, Is.True); + Assert.That(Enum.GetNames(type), Is.EqualTo(ExpectedStatusNames)); + } + + #endregion + + #region ExtensionPointInfo and ExtensionInfo + + private static readonly object[] InfoProperties = + { + new object[] { "NUnit.Engine.ExtensionPointInfo", "Path", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionPointInfo", "Description", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionPointInfo", "TypeName", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionPointInfo", "Extensions", "IReadOnlyList" }, + new object[] { "NUnit.Engine.ExtensionInfo", "TypeName", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionInfo", "Path", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionInfo", "Description", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionInfo", "Enabled", typeof(bool) }, + new object[] { "NUnit.Engine.ExtensionInfo", "Status", "NUnit.Engine.ExtensionInfoStatus" }, + new object[] { "NUnit.Engine.ExtensionInfo", "Error", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionInfo", "AssemblyPath", typeof(string) }, + new object[] { "NUnit.Engine.ExtensionInfo", "AssemblyVersion", typeof(Version) }, + new object[] { "NUnit.Engine.ExtensionInfo", "AssemblyTargetFramework", typeof(FrameworkName) }, + new object[] { "NUnit.Engine.ExtensionInfo", "Properties", typeof(IReadOnlyDictionary>) }, + }; + + [TestCase("NUnit.Engine.ExtensionPointInfo")] + [TestCase("NUnit.Engine.ExtensionInfo")] + public void InfoTypeIsSealedClassWithDefaultConstructor(string typeName) + { + var type = ApiAssembly.GetType(typeName); + + Assert.That(type, Is.Not.Null, $"{typeName} is missing"); + Assert.That(type.IsPublic, Is.True); + Assert.That(type.IsClass, Is.True); + Assert.That(type.IsSealed, Is.True, "Sealed, so members can be added in 4.x"); + Assert.That(type.GetConstructor(Type.EmptyTypes), Is.Not.Null, "Built with object initializers"); + } + + [TestCaseSource(nameof(InfoProperties))] + public void InfoTypeHasInitOnlyProperty(string typeName, string propertyName, object expectedType) + { + var type = ApiAssembly.GetType(typeName); + Assert.That(type, Is.Not.Null, $"{typeName} is missing"); + + var property = type.GetProperty(propertyName); + Assert.That(property, Is.Not.Null, $"{typeName}.{propertyName} is missing"); + Assert.That(property.PropertyType, Is.EqualTo(ResolveType(expectedType))); + Assert.That(property.GetMethod?.IsPublic, Is.True, "Public getter"); + + var setter = property.SetMethod; + Assert.That(setter?.IsPublic, Is.True, "Public init accessor"); + var modifiers = setter.ReturnParameter.GetRequiredCustomModifiers().Select(m => m.FullName); + Assert.That(modifiers, Has.Member(IsExternalInitTypeName), "Init-only, not settable"); + } + + [TestCase("NUnit.Engine.ExtensionPointInfo", "Extensions")] + [TestCase("NUnit.Engine.ExtensionInfo", "Properties")] + public void InfoTypeCollectionsDefaultToEmpty(string typeName, string propertyName) + { + var type = ApiAssembly.GetType(typeName); + Assert.That(type, Is.Not.Null, $"{typeName} is missing"); + + var value = type.GetProperty(propertyName)?.GetValue(Activator.CreateInstance(type)); + + Assert.That(value, Is.Not.Null.And.InstanceOf().And.Empty); + } + + [Test] + public void ExtensionInfoDefaultsToNoError() + { + var type = ApiAssembly.GetType("NUnit.Engine.ExtensionInfo"); + Assert.That(type, Is.Not.Null, "NUnit.Engine.ExtensionInfo is missing"); + + var error = type.GetProperty("Error")?.GetValue(Activator.CreateInstance(type)); + + Assert.That(error, Is.Null); + } + + #endregion + + #region Helpers + + private static bool IsExtensibilityType(Type type) + { + if (type.IsGenericType) + if (type.GetGenericArguments().Any(IsExtensibilityType)) + return true; + + if (type.HasElementType) + return IsExtensibilityType(type.GetElementType()); + + return type.Namespace == ExtensibilityNamespace || + type.Assembly.GetName().Name == ExtensibilityAssemblyName; + } + + private static IEnumerable TypesUsedBy(MemberInfo member) + { + switch (member) + { + case PropertyInfo property: + yield return property.PropertyType; + break; + case FieldInfo field: + yield return field.FieldType; + break; + case EventInfo evt: + yield return evt.EventHandlerType; + break; + case MethodBase method: + if (method is MethodInfo info) + yield return info.ReturnType; + foreach (var parameter in method.GetParameters()) + yield return parameter.ParameterType; + break; + } + } + + // Expected types are given as a Type, or as a name for types that don't exist yet. + private static Type ResolveType(object expected) + { + if (expected is Type type) + return type; + + string name = (string)expected; + const string readOnlyListPrefix = "IReadOnlyList<"; + if (name.StartsWith(readOnlyListPrefix, StringComparison.Ordinal)) + { + var elementName = name.Substring(readOnlyListPrefix.Length, name.Length - readOnlyListPrefix.Length - 1); + return typeof(IReadOnlyList<>).MakeGenericType(ResolveType(elementName)); + } + + var resolved = ApiAssembly.GetType(name); + Assert.That(resolved, Is.Not.Null, $"{name} is missing"); + return resolved; + } + + #endregion + } +} diff --git a/src/NUnit.Engine.Api/ExtensionInfo.cs b/src/NUnit.Engine.Api/ExtensionInfo.cs new file mode 100644 index 0000000..2ace8a6 --- /dev/null +++ b/src/NUnit.Engine.Api/ExtensionInfo.cs @@ -0,0 +1,71 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.Collections.Generic; +using System.Runtime.Versioning; + +namespace NUnit.Engine +{ + /// + /// Information about an installed extension, as reported to runners + /// by . It is a snapshot taken when the + /// information was requested, not a live view of the extension. + /// + public sealed class ExtensionInfo + { + private static readonly IReadOnlyDictionary> NoProperties = + new Dictionary>(); + + /// + /// Gets the full name of the extension type. + /// + public string TypeName { get; init; } = string.Empty; + + /// + /// Gets the path of the extension point the extension is attached to. + /// + public string Path { get; init; } = string.Empty; + + /// + /// Gets the description of the extension, if it has one. + /// + public string? Description { get; init; } + + /// + /// Gets a value indicating whether the extension is enabled. + /// + public bool Enabled { get; init; } + + /// + /// Gets the status of the extension. + /// + public ExtensionInfoStatus Status { get; init; } + + /// + /// Gets a description of the error that occurred loading the extension, + /// or null if there was none. + /// + public string? Error { get; init; } + + /// + /// Gets the path of the assembly containing the extension. + /// + public string AssemblyPath { get; init; } = string.Empty; + + /// + /// Gets the version of the assembly containing the extension, if known. + /// + public Version? AssemblyVersion { get; init; } + + /// + /// Gets the target framework of the assembly containing the extension, if known. + /// + public FrameworkName? AssemblyTargetFramework { get; init; } + + /// + /// Gets the properties of the extension. Each property name maps to + /// one or more values. + /// + public IReadOnlyDictionary> Properties { get; init; } = NoProperties; + } +} diff --git a/src/NUnit.Engine.Api/ExtensionInfoStatus.cs b/src/NUnit.Engine.Api/ExtensionInfoStatus.cs new file mode 100644 index 0000000..12bc252 --- /dev/null +++ b/src/NUnit.Engine.Api/ExtensionInfoStatus.cs @@ -0,0 +1,30 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +namespace NUnit.Engine +{ + /// + /// The status of an extension, as reported to runners in an . + /// + public enum ExtensionInfoStatus + { + /// + /// The extension has not yet been loaded. + /// + Unloaded, + + /// + /// The extension has been loaded. + /// + Loaded, + + /// + /// An error occurred trying to load the extension. See . + /// + Error, + + /// + /// The extension doesn't match any known extension point. + /// + Unknown, + } +} diff --git a/src/NUnit.Engine.Api/ExtensionPointInfo.cs b/src/NUnit.Engine.Api/ExtensionPointInfo.cs new file mode 100644 index 0000000..c4ff3bf --- /dev/null +++ b/src/NUnit.Engine.Api/ExtensionPointInfo.cs @@ -0,0 +1,35 @@ +// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt + +using System; +using System.Collections.Generic; + +namespace NUnit.Engine +{ + /// + /// Information about an extension point and the extensions attached to it, + /// as reported to runners by . It is a snapshot + /// taken when the information was requested, not a live view. + /// + public sealed class ExtensionPointInfo + { + /// + /// Gets the unique path identifying the extension point. + /// + public string Path { get; init; } = string.Empty; + + /// + /// Gets the description of the extension point, if it has one. + /// + public string? Description { get; init; } + + /// + /// Gets the full name of the type that extensions of this point must implement. + /// + public string TypeName { get; init; } = string.Empty; + + /// + /// Gets the extensions attached to this extension point. + /// + public IReadOnlyList Extensions { get; init; } = Array.Empty(); + } +} diff --git a/src/NUnit.Engine.Api/IExtensionService.cs b/src/NUnit.Engine.Api/IExtensionService.cs index ee274de..b2023e8 100644 --- a/src/NUnit.Engine.Api/IExtensionService.cs +++ b/src/NUnit.Engine.Api/IExtensionService.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -using NUnit.Extensibility; namespace NUnit.Engine { @@ -12,16 +11,16 @@ namespace NUnit.Engine public interface IExtensionService { /// - /// Gets an enumeration of all extension points in the engine. + /// Gets information about all extension points in the engine, + /// each with the extensions attached to it. /// - /// An enumeration of IExtensionPoints. - IEnumerable ExtensionPoints { get; } + IEnumerable ExtensionPoints { get; } /// - /// Gets an enumeration of all installed Extensions. + /// Gets information about all installed extensions, including any + /// that don't match a known extension point. /// - /// An enumeration of IExtensionNodes - IEnumerable Extensions { get; } + IEnumerable Extensions { get; } /// /// Find candidate extension assemblies starting from a given base directory, @@ -36,18 +35,6 @@ public interface IExtensionService [Obsolete("Add the directory to ITestEngine.ExtensionDirectories before the engine is initialized. This method will be removed in 5.0.")] void FindExtensionAssemblies(string initialDirectory); - /// - /// Get an IExtensionPoint based on its unique identifying path. - /// - /// A single IExtensionPoint with the specified path if it exists. Otherwise null. - IExtensionPoint? GetExtensionPoint(string path); - - /// - /// Get an enumeration of IExtensionNodes based on their identifying path. - /// - /// An enumeration of IExtensionNodes. - IEnumerable GetExtensionNodes(string path); - /// /// Enable or disable an extension /// @@ -68,11 +55,5 @@ public interface IExtensionService /// /// An enumeration of T IEnumerable GetExtensions(); - - /// - /// Get all extension nodes of a given Type. - /// - /// An enumeration of IExtensionNodes for Type T. - IEnumerable GetExtensionNodes(); } } diff --git a/src/NUnit.Engine.Api/NUnit.Engine.Api.csproj b/src/NUnit.Engine.Api/NUnit.Engine.Api.csproj index 9faa687..805de46 100644 --- a/src/NUnit.Engine.Api/NUnit.Engine.Api.csproj +++ b/src/NUnit.Engine.Api/NUnit.Engine.Api.csproj @@ -16,8 +16,4 @@ Defines the interfaces used by runners and engine extensions to access the NUnit Engine. Not normally needed by direct users who simply want to run tests. - - - -