Skip to content

Commit 3436ce6

Browse files
authored
Merge branch 'release/3.1' into merge/release/3.1-preview1-to-release/3.1\n\nCommit migrated from dotnet/extensions@9d66ade
2 parents 735b89a + 8bde545 commit 3436ce6

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
namespace Microsoft.AspNetCore.Testing
8+
{
9+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
10+
public class OSMinVersionAttribute : Attribute, ITestCondition
11+
{
12+
private readonly OperatingSystems _targetOS;
13+
private readonly Version _minVersion;
14+
private readonly OperatingSystems _currentOS;
15+
private readonly Version _currentVersion;
16+
private readonly bool _skip;
17+
18+
/// <summary>
19+
/// Used to indicate the minimum version a test can run on for the given operating system.
20+
/// Also add <see cref="OSSkipConditionAttribute"/> to skip other operating systems.
21+
/// </summary>
22+
/// <param name="targetOS">The OS to check for a version. Only Windows is currently supported.</param>
23+
/// <param name="minVersion">The minimum OS version NOT to skip.</param>
24+
public OSMinVersionAttribute(OperatingSystems targetOS, string minVersion) :
25+
this(targetOS, Version.Parse(minVersion), GetCurrentOS(), GetCurrentOSVersion())
26+
{
27+
}
28+
29+
// to enable unit testing
30+
internal OSMinVersionAttribute(OperatingSystems targetOS, Version minVersion, OperatingSystems currentOS, Version currentVersion)
31+
{
32+
if (targetOS != OperatingSystems.Windows)
33+
{
34+
throw new NotImplementedException(targetOS.ToString());
35+
}
36+
37+
_targetOS = targetOS;
38+
_minVersion = minVersion;
39+
_currentOS = currentOS;
40+
_currentVersion = currentVersion;
41+
42+
_skip = _targetOS == _currentOS && _minVersion > _currentVersion;
43+
SkipReason = $"The test cannot run on this operating system version '{currentVersion}'.";
44+
}
45+
46+
// Since a test would be excuted only if 'IsMet' is true, return false if we want to skip
47+
public bool IsMet => !_skip;
48+
49+
public string SkipReason { get; set; }
50+
51+
static private OperatingSystems GetCurrentOS()
52+
{
53+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
54+
{
55+
return OperatingSystems.Windows;
56+
}
57+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
58+
{
59+
return OperatingSystems.Linux;
60+
}
61+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
62+
{
63+
return OperatingSystems.MacOSX;
64+
}
65+
throw new PlatformNotSupportedException();
66+
}
67+
68+
static private Version GetCurrentOSVersion()
69+
{
70+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
71+
{
72+
return Environment.OSVersion.Version;
73+
}
74+
else
75+
{
76+
// Not implmeneted, but this will still be called before the OS check happens so don't throw.
77+
return new Version(0, 0);
78+
}
79+
}
80+
}
81+
}

src/Testing/src/xunit/WindowsVersions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Testing
55
{
6+
/// <summary>
7+
/// https://en.wikipedia.org/wiki/Windows_10_version_history
8+
/// </summary>
69
public static class WindowsVersions
710
{
811
public const string Win7 = "6.1";
@@ -14,5 +17,20 @@ public static class WindowsVersions
1417
public const string Win81 = "6.3";
1518

1619
public const string Win10 = "10.0";
20+
21+
/// <summary>
22+
/// 1803, RS4, 17134
23+
/// </summary>
24+
public const string Win10_RS4 = "10.0.17134";
25+
26+
/// <summary>
27+
/// 1909, 19H2, 18363
28+
/// </summary>
29+
public const string Win10_19H2 = "10.0.18363";
30+
31+
/// <summary>
32+
/// _, 20H2, 18990
33+
/// </summary>
34+
public const string Win10_20H1 = "10.0.18990";
1735
}
1836
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Xunit;
6+
7+
namespace Microsoft.AspNetCore.Testing
8+
{
9+
public class OSMinVersionAttributeTest
10+
{
11+
[Fact]
12+
public void Linux_ThrowsNotImplemeneted()
13+
{
14+
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.Linux, "2.5"));
15+
}
16+
17+
[Fact]
18+
public void Mac_ThrowsNotImplemeneted()
19+
{
20+
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.MacOSX, "2.5"));
21+
}
22+
23+
[Fact]
24+
public void WindowsOrLinux_ThrowsNotImplemeneted()
25+
{
26+
Assert.Throws<NotImplementedException>(() => new OSMinVersionAttribute(OperatingSystems.Linux | OperatingSystems.Windows, "2.5"));
27+
}
28+
29+
[Fact]
30+
public void DoesNotSkip_LaterVersions()
31+
{
32+
var osSkipAttribute = new OSMinVersionAttribute(
33+
OperatingSystems.Windows,
34+
new Version("2.0"),
35+
OperatingSystems.Windows,
36+
new Version("2.5"));
37+
38+
Assert.True(osSkipAttribute.IsMet);
39+
}
40+
41+
[Fact]
42+
public void DoesNotSkip_SameVersion()
43+
{
44+
var osSkipAttribute = new OSMinVersionAttribute(
45+
OperatingSystems.Windows,
46+
new Version("2.5"),
47+
OperatingSystems.Windows,
48+
new Version("2.5"));
49+
50+
Assert.True(osSkipAttribute.IsMet);
51+
}
52+
53+
[Fact]
54+
public void Skip_EarlierVersion()
55+
{
56+
var osSkipAttribute = new OSMinVersionAttribute(
57+
OperatingSystems.Windows,
58+
new Version("3.0"),
59+
OperatingSystems.Windows,
60+
new Version("2.5"));
61+
62+
Assert.False(osSkipAttribute.IsMet);
63+
}
64+
65+
[Fact]
66+
public void DoesNotSkip_WhenOnlyVersionsMatch()
67+
{
68+
var osSkipAttribute = new OSMinVersionAttribute(
69+
OperatingSystems.Windows,
70+
new Version("2.5"),
71+
OperatingSystems.Linux,
72+
new Version("2.5"));
73+
74+
Assert.True(osSkipAttribute.IsMet);
75+
}
76+
}
77+
}

src/Testing/test/OSMinVersionTest.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using Microsoft.Win32;
7+
using Xunit;
8+
9+
namespace Microsoft.AspNetCore.Testing
10+
{
11+
public class OSMinVersionTest
12+
{
13+
[ConditionalFact]
14+
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
15+
public void RunTest_Win8DoesNotRunOnWin7()
16+
{
17+
Assert.False(
18+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
19+
Environment.OSVersion.Version.ToString().StartsWith("6.1"),
20+
"Test should not be running on Win7 or Win2008R2.");
21+
}
22+
23+
[ConditionalTheory]
24+
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
25+
[InlineData(1)]
26+
public void RunTheory_Win8DoesNotRunOnWin7(int arg)
27+
{
28+
Assert.False(
29+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
30+
Environment.OSVersion.Version.ToString().StartsWith("6.1"),
31+
"Test should not be running on Win7 or Win2008R2.");
32+
}
33+
34+
[ConditionalFact]
35+
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win10_RS4)]
36+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
37+
public void RunTest_Win10_RS4()
38+
{
39+
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
40+
var versionKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
41+
Assert.NotNull(versionKey);
42+
var currentVersion = (string)versionKey.GetValue("CurrentBuildNumber");
43+
Assert.NotNull(currentVersion);
44+
Assert.True(17134 <= int.Parse(currentVersion));
45+
}
46+
47+
[ConditionalFact]
48+
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win10_19H2)]
49+
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
50+
public void RunTest_Win10_19H2()
51+
{
52+
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
53+
var versionKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
54+
Assert.NotNull(versionKey);
55+
var currentVersion = (string)versionKey.GetValue("CurrentBuildNumber");
56+
Assert.NotNull(currentVersion);
57+
Assert.True(18363 <= int.Parse(currentVersion));
58+
}
59+
}
60+
61+
[OSMinVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
62+
public class OSMinVersionClassTest
63+
{
64+
[ConditionalFact]
65+
public void TestSkipClass_Win8DoesNotRunOnWin7()
66+
{
67+
Assert.False(
68+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
69+
Environment.OSVersion.Version.ToString().StartsWith("6.1"),
70+
"Test should not be running on Win7 or Win2008R2.");
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)