Unify OS version xunit attributes (dotnet/extensions#2481)
\n\nCommit migrated from 65e70f58df
This commit is contained in:
parent
f823334582
commit
aae216ade2
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Microsoft.AspNetCore.Testing
|
||||
{
|
||||
|
|
@ -14,49 +13,36 @@ namespace Microsoft.AspNetCore.Testing
|
|||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
|
||||
public class MinimumOSVersionAttribute : Attribute, ITestCondition
|
||||
{
|
||||
private readonly OperatingSystems _excludedOperatingSystem;
|
||||
private readonly OperatingSystems _targetOS;
|
||||
private readonly Version _minVersion;
|
||||
private readonly OperatingSystems _osPlatform;
|
||||
private readonly Version _osVersion;
|
||||
private readonly OperatingSystems _currentOS;
|
||||
private readonly Version _currentVersion;
|
||||
private readonly bool _skip;
|
||||
|
||||
public MinimumOSVersionAttribute(OperatingSystems operatingSystem, string minVersion) :
|
||||
this(
|
||||
operatingSystem,
|
||||
GetCurrentOS(),
|
||||
GetCurrentOSVersion(),
|
||||
Version.Parse(minVersion))
|
||||
this(operatingSystem, Version.Parse(minVersion), GetCurrentOS(), GetCurrentOSVersion())
|
||||
{
|
||||
}
|
||||
|
||||
// to enable unit testing
|
||||
internal MinimumOSVersionAttribute(
|
||||
OperatingSystems operatingSystem, OperatingSystems osPlatform, Version osVersion, Version minVersion)
|
||||
internal MinimumOSVersionAttribute(OperatingSystems targetOS, Version minVersion, OperatingSystems currentOS, Version currentVersion)
|
||||
{
|
||||
if (operatingSystem != OperatingSystems.Windows)
|
||||
if (targetOS != OperatingSystems.Windows)
|
||||
{
|
||||
throw new NotImplementedException("Min version support is only implemented for Windows.");
|
||||
}
|
||||
_excludedOperatingSystem = operatingSystem;
|
||||
_targetOS = targetOS;
|
||||
_minVersion = minVersion;
|
||||
_osPlatform = osPlatform;
|
||||
_osVersion = osVersion;
|
||||
_currentOS = currentOS;
|
||||
_currentVersion = currentVersion;
|
||||
|
||||
SkipReason = $"This test requires {_excludedOperatingSystem} {_minVersion} or later.";
|
||||
// Do not skip other OS's, Use OSSkipConditionAttribute or a separate MinimumOSVersionAttribute for that.
|
||||
_skip = _targetOS == _currentOS && _minVersion > _currentVersion;
|
||||
SkipReason = $"This test requires {_targetOS} {_minVersion} or later.";
|
||||
}
|
||||
|
||||
public bool IsMet
|
||||
{
|
||||
get
|
||||
{
|
||||
// Do not skip other OS's, Use OSSkipConditionAttribute or a separate MinimumOSVersionAttribute for that.
|
||||
if (_osPlatform != _excludedOperatingSystem)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return _osVersion >= _minVersion;
|
||||
}
|
||||
}
|
||||
// Since a test would be executed only if 'IsMet' is true, return false if we want to skip
|
||||
public bool IsMet => !_skip;
|
||||
|
||||
public string SkipReason { get; set; }
|
||||
|
||||
|
|
@ -77,34 +63,16 @@ namespace Microsoft.AspNetCore.Testing
|
|||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
|
||||
private static Version GetCurrentOSVersion()
|
||||
static private Version GetCurrentOSVersion()
|
||||
{
|
||||
// currently not used on other OS's
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
// Win10+
|
||||
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
|
||||
var major = key.GetValue("CurrentMajorVersionNumber") as int?;
|
||||
var minor = key.GetValue("CurrentMinorVersionNumber") as int?;
|
||||
|
||||
if (major.HasValue && minor.HasValue)
|
||||
{
|
||||
return new Version(major.Value, minor.Value);
|
||||
}
|
||||
|
||||
// CurrentVersion doesn't work past Win8.1
|
||||
var current = key.GetValue("CurrentVersion") as string;
|
||||
if (!string.IsNullOrEmpty(current) && Version.TryParse(current, out var currentVersion))
|
||||
{
|
||||
return currentVersion;
|
||||
}
|
||||
|
||||
// Environment.OSVersion doesn't work past Win8.
|
||||
return Environment.OSVersion.Version;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Version();
|
||||
// Not implemented, but this will still be called before the OS check happens so don't throw.
|
||||
return new Version(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Microsoft.AspNetCore.Testing
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
|
||||
public class OSMinVersionAttribute : Attribute, ITestCondition
|
||||
{
|
||||
private readonly OperatingSystems _targetOS;
|
||||
private readonly Version _minVersion;
|
||||
private readonly OperatingSystems _currentOS;
|
||||
private readonly Version _currentVersion;
|
||||
private readonly bool _skip;
|
||||
|
||||
/// <summary>
|
||||
/// Used to indicate the minimum version a test can run on for the given operating system.
|
||||
/// Also add <see cref="OSSkipConditionAttribute"/> to skip other operating systems.
|
||||
/// </summary>
|
||||
/// <param name="targetOS">The OS to check for a version. Only Windows is currently supported.</param>
|
||||
/// <param name="minVersion">The minimum OS version NOT to skip.</param>
|
||||
public OSMinVersionAttribute(OperatingSystems targetOS, string minVersion) :
|
||||
this(targetOS, Version.Parse(minVersion), GetCurrentOS(), GetCurrentOSVersion())
|
||||
{
|
||||
}
|
||||
|
||||
// to enable unit testing
|
||||
internal OSMinVersionAttribute(OperatingSystems targetOS, Version minVersion, OperatingSystems currentOS, Version currentVersion)
|
||||
{
|
||||
if (targetOS != OperatingSystems.Windows)
|
||||
{
|
||||
throw new NotImplementedException(targetOS.ToString());
|
||||
}
|
||||
|
||||
_targetOS = targetOS;
|
||||
_minVersion = minVersion;
|
||||
_currentOS = currentOS;
|
||||
_currentVersion = currentVersion;
|
||||
|
||||
_skip = _targetOS == _currentOS && _minVersion > _currentVersion;
|
||||
SkipReason = $"The test cannot run on this operating system version '{currentVersion}'.";
|
||||
}
|
||||
|
||||
// Since a test would be excuted only if 'IsMet' is true, return false if we want to skip
|
||||
public bool IsMet => !_skip;
|
||||
|
||||
public string SkipReason { get; set; }
|
||||
|
||||
static private OperatingSystems GetCurrentOS()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return OperatingSystems.Windows;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
||||
{
|
||||
return OperatingSystems.Linux;
|
||||
}
|
||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||||
{
|
||||
return OperatingSystems.MacOSX;
|
||||
}
|
||||
throw new PlatformNotSupportedException();
|
||||
}
|
||||
|
||||
static private Version GetCurrentOSVersion()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
return Environment.OSVersion.Version;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not implmeneted, but this will still be called before the OS check happens so don't throw.
|
||||
return new Version(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,30 +6,30 @@ using Xunit;
|
|||
|
||||
namespace Microsoft.AspNetCore.Testing
|
||||
{
|
||||
public class OSMinVersionAttributeTest
|
||||
public class MinimumOSVersionAttributeTest
|
||||
{
|
||||
[Fact]
|
||||
public void Linux_ThrowsNotImplemeneted()
|
||||
{
|
||||
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.Linux, "2.5"));
|
||||
Assert.Throws<NotImplementedException>(() => new MinimumOSVersionAttribute(OperatingSystems.Linux, "2.5"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Mac_ThrowsNotImplemeneted()
|
||||
{
|
||||
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.MacOSX, "2.5"));
|
||||
Assert.Throws<NotImplementedException>(() => new MinimumOSVersionAttribute(OperatingSystems.MacOSX, "2.5"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowsOrLinux_ThrowsNotImplemeneted()
|
||||
{
|
||||
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, "2.5"));
|
||||
Assert.Throws<NotImplementedException>(() => new MinimumOSVersionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, "2.5"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DoesNotSkip_LaterVersions()
|
||||
{
|
||||
var osSkipAttribute = new OSMinVersionAttribute(
|
||||
var osSkipAttribute = new MinimumOSVersionAttribute(
|
||||
OperatingSystems.Windows,
|
||||
new Version("2.0"),
|
||||
OperatingSystems.Windows,
|
||||
|
|
@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
[Fact]
|
||||
public void DoesNotSkip_SameVersion()
|
||||
{
|
||||
var osSkipAttribute = new OSMinVersionAttribute(
|
||||
var osSkipAttribute = new MinimumOSVersionAttribute(
|
||||
OperatingSystems.Windows,
|
||||
new Version("2.5"),
|
||||
OperatingSystems.Windows,
|
||||
|
|
@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
[Fact]
|
||||
public void Skip_EarlierVersion()
|
||||
{
|
||||
var osSkipAttribute = new OSMinVersionAttribute(
|
||||
var osSkipAttribute = new MinimumOSVersionAttribute(
|
||||
OperatingSystems.Windows,
|
||||
new Version("3.0"),
|
||||
OperatingSystems.Windows,
|
||||
|
|
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
[Fact]
|
||||
public void DoesNotSkip_WhenOnlyVersionsMatch()
|
||||
{
|
||||
var osSkipAttribute = new OSMinVersionAttribute(
|
||||
var osSkipAttribute = new MinimumOSVersionAttribute(
|
||||
OperatingSystems.Windows,
|
||||
new Version("2.5"),
|
||||
OperatingSystems.Linux,
|
||||
|
|
@ -8,10 +8,10 @@ using Xunit;
|
|||
|
||||
namespace Microsoft.AspNetCore.Testing
|
||||
{
|
||||
public class OSMinVersionTest
|
||||
public class MinimumOSVersionTest
|
||||
{
|
||||
[ConditionalFact]
|
||||
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
public void RunTest_Win8DoesNotRunOnWin7()
|
||||
{
|
||||
Assert.False(
|
||||
|
|
@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
}
|
||||
|
||||
[ConditionalTheory]
|
||||
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
[InlineData(1)]
|
||||
public void RunTheory_Win8DoesNotRunOnWin7(int arg)
|
||||
{
|
||||
|
|
@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
}
|
||||
|
||||
[ConditionalFact]
|
||||
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win10_RS4)]
|
||||
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_RS4)]
|
||||
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
|
||||
public void RunTest_Win10_RS4()
|
||||
{
|
||||
|
|
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
}
|
||||
|
||||
[ConditionalFact]
|
||||
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2)]
|
||||
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2)]
|
||||
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
|
||||
public void RunTest_Win10_19H2()
|
||||
{
|
||||
|
|
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.Testing
|
|||
}
|
||||
}
|
||||
|
||||
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
|
||||
public class OSMinVersionClassTest
|
||||
{
|
||||
[ConditionalFact]
|
||||
Loading…
Reference in New Issue