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

Multi-Targeting of engine extensions (.NET Framework only) #452

Merged
merged 8 commits into from
Aug 25, 2018
2 changes: 1 addition & 1 deletion src/NUnitConsole/nunit3-console/ConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void DisplayExtensionList()
foreach (var node in ep.Extensions)
{
_outWriter.Write(" Extension: ");
_outWriter.Write(ColorStyle.Value, node.TypeName);
_outWriter.Write(ColorStyle.Value, $"{node.TypeName} (.NET {node.TargetFramework.FrameworkVersion})");
_outWriter.WriteLine(node.Enabled ? "" : " (Disabled)");
foreach (var prop in node.PropertyNames)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public interface IExtensionNode
/// </summary>
string Description { get; }

/// <summary>
/// The TargetFramework of the extension assembly.
/// </summary>
IRuntimeFramework TargetFramework { get; }

/// <summary>
/// Gets a collection of the names of all this extension's properties
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,96 +22,60 @@
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using NUnit.Engine.Extensibility;
using NUnit.Framework;

Copy link
Member Author

Choose a reason for hiding this comment

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

Significant changes in this file are mainly due to tests here split out into ExtensionSelectorTests

namespace NUnit.Engine.Services.Tests
namespace NUnit.Engine.Tests.Extensibility
{
public class ExtensionAssemblyTests
{
private static readonly Assembly THIS_ASSEMBLY = Assembly.GetExecutingAssembly();
private static readonly string THIS_ASSEMBLY_PATH = THIS_ASSEMBLY.Location;
private static readonly string THIS_ASSEMBLY_NAME = THIS_ASSEMBLY.GetName().FullName;
private static readonly Assembly ENGINE_ASSEMBLY = Assembly.GetAssembly(typeof(ExtensionAssembly));
private static readonly string ENGINE_ASSEMBLY_PATH = ENGINE_ASSEMBLY.Location;
private static readonly string THIS_ASSEMBLY_FULL_NAME = THIS_ASSEMBLY.GetName().FullName;
private static readonly string THIS_ASSEMBLY_NAME = THIS_ASSEMBLY.GetName().Name;
private static readonly Version THIS_ASSEMBLY_VERSION = THIS_ASSEMBLY.GetName().Version;

private ExtensionAssembly _ea;
private ExtensionAssembly _eaCopy;
private ExtensionAssembly _eaBetter;
private ExtensionAssembly _eaOther;

[SetUp]
[OneTimeSetUp]
public void CreateExtensionAssemblies()
{
_ea = new ExtensionAssembly(THIS_ASSEMBLY_PATH, false);
_eaCopy = new ExtensionAssembly(THIS_ASSEMBLY_PATH, false);
_eaBetter = new ExtensionAssembly(THIS_ASSEMBLY_PATH, false);
_eaBetter.Assembly.Name.Version = new Version(7, 0);
_eaOther = new ExtensionAssembly(ENGINE_ASSEMBLY_PATH, false);
}

[Test]
public void AssemblyDefinition()
{
Assert.That(_ea.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_ea.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_FULL_NAME));
}

[Test]
public void MainModule()
{
Assert.That(_ea.MainModule.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_ea.MainModule.Assembly.FullName, Is.EqualTo(THIS_ASSEMBLY_FULL_NAME));
}

[Test]
public void AssemblyName()
{
Assert.That(_ea.AssemblyName.FullName, Is.EqualTo(THIS_ASSEMBLY_NAME));
Assert.That(_ea.AssemblyName, Is.EqualTo(THIS_ASSEMBLY_NAME));
}

[Test]
public void IsDuplicateOf_SameAssembly()
public void AssemblyVersion()
{
Assert.That(_ea.IsDuplicateOf(_eaCopy));
Assert.That(_eaCopy.IsDuplicateOf(_ea));
Assert.That(_ea.AssemblyVersion, Is.EqualTo(THIS_ASSEMBLY_VERSION));
}

[Test]
public void IsDuplicateOf_DifferentAssembly()
public void TargetFramework()
{
Assert.False(_ea.IsDuplicateOf(_eaOther));
Assert.False(_eaOther.IsDuplicateOf(_ea));
Assert.Multiple(() =>
{
Assert.That(_ea.TargetFramework, Has.Property(nameof(RuntimeFramework.Runtime)).EqualTo(RuntimeType.Any));
Assert.That(_ea.TargetFramework, Has.Property(nameof(RuntimeFramework.FrameworkVersion)).EqualTo(new Version(2, 0)));
});
}

[Test]
public void IsBetterVersionOf_DifferentVersions()
{
Assert.That(_eaBetter.IsBetterVersionOf(_ea));
Assert.False(_ea.IsBetterVersionOf(_eaBetter));
}

[Test]
public void IsBetterVersionOf_SameVersion()
{
Assert.False(_ea.IsBetterVersionOf(_eaCopy));
Assert.False(_eaCopy.IsBetterVersionOf(_ea));
}

[Test]
public void IsBetterVersionOf_SameVersion_OneWildCard()
{
var eaWild = new ExtensionAssembly(THIS_ASSEMBLY_PATH, true);
Assert.True(_ea.IsBetterVersionOf(eaWild));
Assert.False(eaWild.IsBetterVersionOf(_ea));
}

#if DEBUG
[Test]
public void IsBetterVersionOf_ThrowsIfNotDuplicates()
{
Assert.That(() => { _ea.IsBetterVersionOf(_eaOther); }, Throws.TypeOf<NUnitEngineException>());
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// ***********************************************************************
// Copyright (c) 2018 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************


using System;
using NSubstitute;
using NUnit.Engine.Extensibility;
using NUnit.Framework;

namespace NUnit.Engine.Tests.Extensibility
{
internal class ExtensionSelectorTests
{
[Test]
public void IsDuplicateOfWithSame()
{
var first = MockExtension("Extension1");
var second = MockExtension("Extension1");
Assert.Multiple(() =>
{
Assert.That(first.IsDuplicateOf(second), Is.True);
Assert.That(second.IsDuplicateOf(first), Is.True);
});
}

[Test]
public void IsDuplicateOfWithDifferent()
{
var first = MockExtension("Extension1");
var second = MockExtension("Extension2");
Assert.Multiple(() =>
{
Assert.That(first.IsDuplicateOf(second), Is.False);
Assert.That(second.IsDuplicateOf(first), Is.False);
});
}

[Test]
public void IsBetterVersionOfThrowsWhenNotDuplicates()
{
var first = MockExtension("Extension1");
var second = MockExtension("Extension2");
Assert.That(() => first.IsBetterVersionOf(second), Throws.InvalidOperationException);
}

[Test]
public void IsBetterVersionOfChoosesHighestAssemblyVersion()
{
var first = MockExtension(assemblyVersion: new Version(2, 0));
var second = MockExtension(assemblyVersion: new Version(4, 7));
Assert.Multiple(() =>
{
Assert.That(first.IsBetterVersionOf(second), Is.False);
Assert.That(second.IsBetterVersionOf(first), Is.True);
});
}

[Test]
public void IsBetterVersionOfChoosesHighestTargetFramework()
{
var first = MockExtension(targetFramework: new Version(2, 0));
var second = MockExtension(targetFramework: new Version(4, 7));
Assert.Multiple(() =>
{
Assert.That(first.IsBetterVersionOf(second), Is.False);
Assert.That(second.IsBetterVersionOf(first), Is.True);
});
}

[Test]
public void IsBetterVersionOfPrioritisesAssemblyVersionOverTargetFramework()
{
var first = MockExtension(assemblyVersion: new Version(2, 0), targetFramework: new Version(2, 0));
var second = MockExtension(assemblyVersion: new Version(1, 0), targetFramework: new Version(4, 7));
Assert.Multiple(() =>
{
Assert.That(first.IsBetterVersionOf(second), Is.True);
Assert.That(second.IsBetterVersionOf(first), Is.False);
});
}

[Test]
public void IsBetterVersionOfPrefersDirectlySpecifiedToWildcard()
{
var first = MockExtension(fromWildcard: false);
var second = MockExtension(fromWildcard: true);
Assert.Multiple(() =>
{
Assert.That(first.IsBetterVersionOf(second), Is.True);
Assert.That(second.IsBetterVersionOf(first), Is.False);
});
}

[Test]
public void IsBetterVersionOfPrefersNoChangeIfFromWildcard()
{
var first = MockExtension(fromWildcard: true);
var second = MockExtension(fromWildcard: true);
Assert.Multiple(() =>
{
Assert.That(first.IsBetterVersionOf(second), Is.False);
Assert.That(second.IsBetterVersionOf(first), Is.False);
});
}

private static IExtensionAssembly MockExtension(string assemblyName = "ExtensionSelectorTestsExtension",
Version assemblyVersion = null,
Version targetFramework = null,
bool fromWildcard = false)
{
var sub = Substitute.For<IExtensionAssembly>();
sub.AssemblyName.Returns(assemblyName);
sub.AssemblyVersion.Returns(assemblyVersion ?? new Version(1, 0));
targetFramework = targetFramework ?? new Version(2, 0);
sub.TargetFramework.Returns(new RuntimeFramework(RuntimeType.Any, targetFramework));
sub.FromWildCard.Returns(fromWildcard);
return sub;
}
}
}
77 changes: 77 additions & 0 deletions src/NUnitEngine/nunit.engine/Extensibility/ExtensionAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// ***********************************************************************
Copy link
Member Author

Choose a reason for hiding this comment

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

This file has been moved and isn't entirely new - I guess GitHub hasn't noticed as there's also some changes.

// Copyright (c) 2016 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.IO;
using Mono.Cecil;
using NUnit.Engine.Internal;

namespace NUnit.Engine.Extensibility
{
internal class ExtensionAssembly : IExtensionAssembly
{
private readonly TargetFrameworkHelper _targetFrameworkHelper;

public ExtensionAssembly(string filePath, bool fromWildCard)
{
FilePath = filePath;
FromWildCard = fromWildCard;
Assembly = GetAssemblyDefinition();
_targetFrameworkHelper = new TargetFrameworkHelper(Assembly);
}

public string FilePath { get; }
public bool FromWildCard { get; }
public AssemblyDefinition Assembly { get; }

public string AssemblyName
{
get { return Assembly.Name.Name; }
}

public Version AssemblyVersion
{
get { return Assembly.Name.Version; }
}

public ModuleDefinition MainModule
{
get { return Assembly.MainModule; }
}

public RuntimeFramework TargetFramework
{
get { return new RuntimeFramework(RuntimeType.Any, _targetFrameworkHelper.TargetRuntimeVersion); }
}

private AssemblyDefinition GetAssemblyDefinition()
{
var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory(Path.GetDirectoryName(FilePath));
resolver.AddSearchDirectory(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
var parameters = new ReaderParameters { AssemblyResolver = resolver };

return AssemblyDefinition.ReadAssembly(FilePath, parameters);
}
}
}
9 changes: 8 additions & 1 deletion src/NUnitEngine/nunit.engine/Extensibility/ExtensionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public class ExtensionNode : IExtensionNode
/// </summary>
/// <param name="assemblyPath">The path to the assembly where this extension is found.</param>
/// <param name="typeName">The full name of the Type of the extension object.</param>
public ExtensionNode(string assemblyPath, string typeName)
/// <param name="targetFramework">The target framework of the extension assembly.</param>
public ExtensionNode(string assemblyPath, string typeName, IRuntimeFramework targetFramework)
{
AssemblyPath = assemblyPath;
TypeName = typeName;
TargetFramework = targetFramework;
Enabled = true; // By default
}

Expand All @@ -56,6 +58,11 @@ public ExtensionNode(string assemblyPath, string typeName)
/// </summary>
public string TypeName { get; private set; }

/// <summary>
/// The TargetFramework of the extension assembly.
/// </summary>
public IRuntimeFramework TargetFramework { get; }

/// <summary>
/// Gets or sets a value indicating whether this <see cref="NUnit.Engine.Extensibility.ExtensionNode"/> is enabled.
/// </summary>
Expand Down
Loading