Remove version checks from OSSkipCondition (dotnet/extensions#2755)

\n\nCommit migrated from 0aa6213e1c
This commit is contained in:
Chris Ross 2019-12-05 10:58:15 -08:00 committed by GitHub
parent b4e59217aa
commit e467364175
6 changed files with 28 additions and 127 deletions

View File

@ -193,6 +193,8 @@ namespace Microsoft.AspNetCore.Testing
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)] [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
public partial class OSSkipConditionAttribute : System.Attribute, Microsoft.AspNetCore.Testing.ITestCondition public partial class OSSkipConditionAttribute : System.Attribute, Microsoft.AspNetCore.Testing.ITestCondition
{ {
public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem) { }
[System.ObsoleteAttribute("Use the Minimum/MaximumOSVersionAttribute for version checks.", true)]
public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem, params string[] versions) { } public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem, params string[] versions) { }
public bool IsMet { get { throw null; } } public bool IsMet { get { throw null; } }
public string SkipReason { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public string SkipReason { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
@ -331,6 +333,7 @@ namespace Microsoft.AspNetCore.Testing
public const string Win10_20H1 = "10.0.19033"; public const string Win10_20H1 = "10.0.19033";
public const string Win10_RS4 = "10.0.17134"; public const string Win10_RS4 = "10.0.17134";
public const string Win10_RS5 = "10.0.17763"; public const string Win10_RS5 = "10.0.17763";
[System.ObsoleteAttribute("Use Win7 instead.", true)]
public const string Win2008R2 = "6.1"; public const string Win2008R2 = "6.1";
public const string Win7 = "6.1"; public const string Win7 = "6.1";
public const string Win8 = "6.2"; public const string Win8 = "6.2";

View File

@ -193,6 +193,8 @@ namespace Microsoft.AspNetCore.Testing
[System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)] [System.AttributeUsageAttribute(System.AttributeTargets.Assembly | System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple=true)]
public partial class OSSkipConditionAttribute : System.Attribute, Microsoft.AspNetCore.Testing.ITestCondition public partial class OSSkipConditionAttribute : System.Attribute, Microsoft.AspNetCore.Testing.ITestCondition
{ {
public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem) { }
[System.ObsoleteAttribute("Use the Minimum/MaximumOSVersionAttribute for version checks.", true)]
public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem, params string[] versions) { } public OSSkipConditionAttribute(Microsoft.AspNetCore.Testing.OperatingSystems operatingSystem, params string[] versions) { }
public bool IsMet { get { throw null; } } public bool IsMet { get { throw null; } }
public string SkipReason { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } } public string SkipReason { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
@ -331,6 +333,7 @@ namespace Microsoft.AspNetCore.Testing
public const string Win10_20H1 = "10.0.19033"; public const string Win10_20H1 = "10.0.19033";
public const string Win10_RS4 = "10.0.17134"; public const string Win10_RS4 = "10.0.17134";
public const string Win10_RS5 = "10.0.17763"; public const string Win10_RS5 = "10.0.17763";
[System.ObsoleteAttribute("Use Win7 instead.", true)]
public const string Win2008R2 = "6.1"; public const string Win2008R2 = "6.1";
public const string Win7 = "6.1"; public const string Win7 = "6.1";
public const string Win8 = "6.2"; public const string Win8 = "6.2";

View File

@ -2,8 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Microsoft.AspNetCore.Testing namespace Microsoft.AspNetCore.Testing
@ -12,46 +10,31 @@ namespace Microsoft.AspNetCore.Testing
public class OSSkipConditionAttribute : Attribute, ITestCondition public class OSSkipConditionAttribute : Attribute, ITestCondition
{ {
private readonly OperatingSystems _excludedOperatingSystem; private readonly OperatingSystems _excludedOperatingSystem;
private readonly IEnumerable<string> _excludedVersions;
private readonly OperatingSystems _osPlatform; private readonly OperatingSystems _osPlatform;
private readonly string _osVersion;
public OSSkipConditionAttribute(OperatingSystems operatingSystem, params string[] versions) : public OSSkipConditionAttribute(OperatingSystems operatingSystem) :
this( this(operatingSystem, GetCurrentOS())
operatingSystem, {
GetCurrentOS(), }
GetCurrentOSVersion(),
versions) [Obsolete("Use the Minimum/MaximumOSVersionAttribute for version checks.", error: true)]
public OSSkipConditionAttribute(OperatingSystems operatingSystem, params string[] versions) :
this(operatingSystem, GetCurrentOS())
{ {
} }
// to enable unit testing // to enable unit testing
internal OSSkipConditionAttribute( internal OSSkipConditionAttribute(OperatingSystems operatingSystem, OperatingSystems osPlatform)
OperatingSystems operatingSystem, OperatingSystems osPlatform, string osVersion, params string[] versions)
{ {
_excludedOperatingSystem = operatingSystem; _excludedOperatingSystem = operatingSystem;
_excludedVersions = versions ?? Enumerable.Empty<string>();
_osPlatform = osPlatform; _osPlatform = osPlatform;
_osVersion = osVersion;
} }
public bool IsMet public bool IsMet
{ {
get get
{ {
var currentOSInfo = new OSInfo() var skip = (_excludedOperatingSystem & _osPlatform) == _osPlatform;
{
OperatingSystem = _osPlatform,
Version = _osVersion,
};
var skip = (_excludedOperatingSystem & currentOSInfo.OperatingSystem) == currentOSInfo.OperatingSystem;
if (_excludedVersions.Any())
{
skip = skip
&& _excludedVersions.Any(ex => _osVersion.StartsWith(ex, StringComparison.OrdinalIgnoreCase));
}
// Since a test would be excuted only if 'IsMet' is true, return false if we want to skip // Since a test would be excuted only if 'IsMet' is true, return false if we want to skip
return !skip; return !skip;
} }
@ -75,25 +58,5 @@ namespace Microsoft.AspNetCore.Testing
} }
throw new PlatformNotSupportedException(); throw new PlatformNotSupportedException();
} }
static private string GetCurrentOSVersion()
{
// currently not used on other OS's
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Environment.OSVersion.Version.ToString();
}
else
{
return string.Empty;
}
}
private class OSInfo
{
public OperatingSystems OperatingSystem { get; set; }
public string Version { get; set; }
}
} }
} }

View File

@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNetCore.Testing namespace Microsoft.AspNetCore.Testing
{ {
/// <summary> /// <summary>
@ -10,6 +12,7 @@ namespace Microsoft.AspNetCore.Testing
{ {
public const string Win7 = "6.1"; public const string Win7 = "6.1";
[Obsolete("Use Win7 instead.", error: true)]
public const string Win2008R2 = Win7; public const string Win2008R2 = Win7;
public const string Win8 = "6.2"; public const string Win8 = "6.2";

View File

@ -10,13 +10,12 @@ namespace Microsoft.AspNetCore.Testing
public class OSSkipConditionAttributeTest public class OSSkipConditionAttributeTest
{ {
[Fact] [Fact]
public void Skips_WhenOnlyOperatingSystemIsSupplied() public void Skips_WhenOperatingSystemMatches()
{ {
// Act // Act
var osSkipAttribute = new OSSkipConditionAttribute( var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Windows, OperatingSystems.Windows,
OperatingSystems.Windows, OperatingSystems.Windows);
"2.5");
// Assert // Assert
Assert.False(osSkipAttribute.IsMet); Assert.False(osSkipAttribute.IsMet);
@ -28,77 +27,18 @@ namespace Microsoft.AspNetCore.Testing
// Act // Act
var osSkipAttribute = new OSSkipConditionAttribute( var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Linux, OperatingSystems.Linux,
OperatingSystems.Windows, OperatingSystems.Windows);
"2.5");
// Assert // Assert
Assert.True(osSkipAttribute.IsMet); Assert.True(osSkipAttribute.IsMet);
} }
[Fact]
public void DoesNotSkip_WhenVersionsDoNotMatch()
{
// Act
var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Windows,
OperatingSystems.Windows,
"2.5",
"10.0");
// Assert
Assert.True(osSkipAttribute.IsMet);
}
[Fact]
public void DoesNotSkip_WhenOnlyVersionsMatch()
{
// Act
var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Linux,
OperatingSystems.Windows,
"2.5",
"2.5");
// Assert
Assert.True(osSkipAttribute.IsMet);
}
[Theory]
[InlineData("2.5", "2.5")]
[InlineData("blue", "Blue")]
public void Skips_WhenVersionsMatches(string currentOSVersion, string skipVersion)
{
// Act
var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Windows,
OperatingSystems.Windows,
currentOSVersion,
skipVersion);
// Assert
Assert.False(osSkipAttribute.IsMet);
}
[Fact]
public void Skips_WhenVersionsMatchesOutOfMultiple()
{
// Act
var osSkipAttribute = new OSSkipConditionAttribute(
OperatingSystems.Windows,
OperatingSystems.Windows,
"2.5",
"10.0", "3.4", "2.5");
// Assert
Assert.False(osSkipAttribute.IsMet);
}
[Fact] [Fact]
public void Skips_BothMacOSXAndLinux() public void Skips_BothMacOSXAndLinux()
{ {
// Act // Act
var osSkipAttributeLinux = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.MacOSX, OperatingSystems.Linux, string.Empty); var osSkipAttributeLinux = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.MacOSX, OperatingSystems.Linux);
var osSkipAttributeMacOSX = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.MacOSX, OperatingSystems.MacOSX, string.Empty); var osSkipAttributeMacOSX = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.MacOSX, OperatingSystems.MacOSX);
// Assert // Assert
Assert.False(osSkipAttributeLinux.IsMet); Assert.False(osSkipAttributeLinux.IsMet);
@ -109,8 +49,8 @@ namespace Microsoft.AspNetCore.Testing
public void Skips_BothMacOSXAndWindows() public void Skips_BothMacOSXAndWindows()
{ {
// Act // Act
var osSkipAttribute = new OSSkipConditionAttribute(OperatingSystems.Windows | OperatingSystems.MacOSX, OperatingSystems.Windows, string.Empty); var osSkipAttribute = new OSSkipConditionAttribute(OperatingSystems.Windows | OperatingSystems.MacOSX, OperatingSystems.Windows);
var osSkipAttributeMacOSX = new OSSkipConditionAttribute(OperatingSystems.Windows | OperatingSystems.MacOSX, OperatingSystems.MacOSX, string.Empty); var osSkipAttributeMacOSX = new OSSkipConditionAttribute(OperatingSystems.Windows | OperatingSystems.MacOSX, OperatingSystems.MacOSX);
// Assert // Assert
Assert.False(osSkipAttribute.IsMet); Assert.False(osSkipAttribute.IsMet);
@ -121,8 +61,8 @@ namespace Microsoft.AspNetCore.Testing
public void Skips_BothWindowsAndLinux() public void Skips_BothWindowsAndLinux()
{ {
// Act // Act
var osSkipAttribute = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, OperatingSystems.Windows, string.Empty); var osSkipAttribute = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, OperatingSystems.Windows);
var osSkipAttributeLinux = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, OperatingSystems.Linux, string.Empty); var osSkipAttributeLinux = new OSSkipConditionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, OperatingSystems.Linux);
// Assert // Assert
Assert.False(osSkipAttribute.IsMet); Assert.False(osSkipAttribute.IsMet);

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Xunit; using Xunit;
@ -27,16 +26,6 @@ namespace Microsoft.AspNetCore.Testing
"Test should not be running on MacOSX."); "Test should not be running on MacOSX.");
} }
[ConditionalFact]
[OSSkipCondition(OperatingSystems.Windows, WindowsVersions.Win7, WindowsVersions.Win2008R2)]
public void RunTest_DoesNotRunOnWin7OrWin2008R2()
{
Assert.False(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
Environment.OSVersion.Version.ToString().StartsWith("6.1"),
"Test should not be running on Win7 or Win2008R2.");
}
[ConditionalFact] [ConditionalFact]
[OSSkipCondition(OperatingSystems.Windows)] [OSSkipCondition(OperatingSystems.Windows)]
public void TestSkipWindows() public void TestSkipWindows()