Skip to content
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
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageVersion Include="Nullable" Version="1.3.1" />
<PackageVersion Include="NUnit" Version="4.6.1" />
<PackageVersion Include="NUnitLite" Version="4.6.1" />
<PackageVersion Include="NUnit.Extensibility.Api" Version="4.0.0-beta.6" />
<PackageVersion Include="StyleCop.Analyzers.Unstable" Version="1.2.0.556" />
<PackageVersion Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
238 changes: 238 additions & 0 deletions src/NUnit.Engine.Api.Tests/ExtensionInfoApiTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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<string>();

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<NUnit.Engine.ExtensionInfo>" },
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<string, IReadOnlyList<string>>) },
};

[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<IEnumerable>().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<Type> 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
}
}
71 changes: 71 additions & 0 deletions src/NUnit.Engine.Api/ExtensionInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Information about an installed extension, as reported to runners
/// by <see cref="IExtensionService"/>. It is a snapshot taken when the
/// information was requested, not a live view of the extension.
/// </summary>
public sealed class ExtensionInfo
{
private static readonly IReadOnlyDictionary<string, IReadOnlyList<string>> NoProperties =
new Dictionary<string, IReadOnlyList<string>>();

/// <summary>
/// Gets the full name of the extension type.
/// </summary>
public string TypeName { get; init; } = string.Empty;

/// <summary>
/// Gets the path of the extension point the extension is attached to.
/// </summary>
public string Path { get; init; } = string.Empty;

/// <summary>
/// Gets the description of the extension, if it has one.
/// </summary>
public string? Description { get; init; }

/// <summary>
/// Gets a value indicating whether the extension is enabled.
/// </summary>
public bool Enabled { get; init; }

/// <summary>
/// Gets the status of the extension.
/// </summary>
public ExtensionInfoStatus Status { get; init; }

/// <summary>
/// Gets a description of the error that occurred loading the extension,
/// or null if there was none.
/// </summary>
public string? Error { get; init; }

/// <summary>
/// Gets the path of the assembly containing the extension.
/// </summary>
public string AssemblyPath { get; init; } = string.Empty;

/// <summary>
/// Gets the version of the assembly containing the extension, if known.
/// </summary>
public Version? AssemblyVersion { get; init; }

/// <summary>
/// Gets the target framework of the assembly containing the extension, if known.
/// </summary>
public FrameworkName? AssemblyTargetFramework { get; init; }

/// <summary>
/// Gets the properties of the extension. Each property name maps to
/// one or more values.
/// </summary>
public IReadOnlyDictionary<string, IReadOnlyList<string>> Properties { get; init; } = NoProperties;
}
}
30 changes: 30 additions & 0 deletions src/NUnit.Engine.Api/ExtensionInfoStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

namespace NUnit.Engine
{
/// <summary>
/// The status of an extension, as reported to runners in an <see cref="ExtensionInfo"/>.
/// </summary>
public enum ExtensionInfoStatus
{
/// <summary>
/// The extension has not yet been loaded.
/// </summary>
Unloaded,

/// <summary>
/// The extension has been loaded.
/// </summary>
Loaded,

/// <summary>
/// An error occurred trying to load the extension. See <see cref="ExtensionInfo.Error"/>.
/// </summary>
Error,

/// <summary>
/// The extension doesn't match any known extension point.
/// </summary>
Unknown,
}
}
35 changes: 35 additions & 0 deletions src/NUnit.Engine.Api/ExtensionPointInfo.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Information about an extension point and the extensions attached to it,
/// as reported to runners by <see cref="IExtensionService"/>. It is a snapshot
/// taken when the information was requested, not a live view.
/// </summary>
public sealed class ExtensionPointInfo
{
/// <summary>
/// Gets the unique path identifying the extension point.
/// </summary>
public string Path { get; init; } = string.Empty;

/// <summary>
/// Gets the description of the extension point, if it has one.
/// </summary>
public string? Description { get; init; }

/// <summary>
/// Gets the full name of the type that extensions of this point must implement.
/// </summary>
public string TypeName { get; init; } = string.Empty;

/// <summary>
/// Gets the extensions attached to this extension point.
/// </summary>
public IReadOnlyList<ExtensionInfo> Extensions { get; init; } = Array.Empty<ExtensionInfo>();
}
}
Loading