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;
}
}
}
31 changes: 26 additions & 5 deletions src/NUnitEngine/nunit.engine.tests/RuntimeFrameworkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void AvailableFrameworksList_ContainsNoDuplicates()
Assert.That(names, Is.Unique);
}

[TestCaseSource("frameworkData")]
[TestCaseSource(nameof(frameworkData))]
public void CanCreateUsingFrameworkVersion(FrameworkData data)
{
RuntimeFramework framework = new RuntimeFramework(data.runtime, data.frameworkVersion);
Expand All @@ -97,7 +97,7 @@ public void CanCreateUsingFrameworkVersion(FrameworkData data)
Assert.AreEqual(data.clrVersion, framework.ClrVersion);
}

[TestCaseSource("frameworkData")]
[TestCaseSource(nameof(frameworkData))]
public void CanCreateUsingClrVersion(FrameworkData data)
{
Assume.That(data.frameworkVersion.Major != 3);
Expand All @@ -108,28 +108,34 @@ public void CanCreateUsingClrVersion(FrameworkData data)
Assert.AreEqual(data.clrVersion, framework.ClrVersion);
}

[TestCaseSource("frameworkData")]
[TestCaseSource(nameof(frameworkData))]
public void CanParseRuntimeFramework(FrameworkData data)
{
RuntimeFramework framework = RuntimeFramework.Parse(data.representation);
Assert.AreEqual(data.runtime, framework.Runtime);
Assert.AreEqual(data.clrVersion, framework.ClrVersion);
}

[TestCaseSource("frameworkData")]
[TestCaseSource(nameof(frameworkData))]
public void CanDisplayFrameworkAsString(FrameworkData data)
{
RuntimeFramework framework = new RuntimeFramework(data.runtime, data.frameworkVersion);
Assert.AreEqual(data.representation, framework.ToString());
Assert.AreEqual(data.displayName, framework.DisplayName);
}

[TestCaseSource("matchData")]
[TestCaseSource(nameof(matchData))]
public bool CanMatchRuntimes(RuntimeFramework f1, RuntimeFramework f2)
{
return f1.Supports(f2);
}

[TestCaseSource(nameof(CanLoadData))]
public bool CanLoad(RuntimeFramework f1, RuntimeFramework f2)
{
return f1.CanLoad(f2);
}

#pragma warning disable 414
static TestCaseData[] matchData = new TestCaseData[] {
new TestCaseData(
Expand Down Expand Up @@ -209,6 +215,21 @@ public bool CanMatchRuntimes(RuntimeFramework f1, RuntimeFramework f2)
new RuntimeFramework(RuntimeType.Any, RuntimeFramework.DefaultVersion))
.Returns(true)
};

private static readonly TestCaseData[] CanLoadData = {
new TestCaseData(
new RuntimeFramework(RuntimeType.Any, new Version(2,0)),
new RuntimeFramework(RuntimeType.Any, new Version(2,0)))
.Returns(true),
new TestCaseData(
new RuntimeFramework(RuntimeType.Any, new Version(2,0)),
new RuntimeFramework(RuntimeType.Any, new Version(4,0)))
.Returns(false),
new TestCaseData(
new RuntimeFramework(RuntimeType.Any, new Version(4,0)),
new RuntimeFramework(RuntimeType.Any, new Version(2,0)))
.Returns(true)
};
#pragma warning restore 414

public struct FrameworkData
Expand Down
Loading