3
3
4
4
using System ;
5
5
using System . Runtime . InteropServices ;
6
- using Microsoft . Win32 ;
7
6
8
7
namespace Microsoft . AspNetCore . Testing
9
8
{
@@ -14,49 +13,36 @@ namespace Microsoft.AspNetCore.Testing
14
13
[ AttributeUsage ( AttributeTargets . Method | AttributeTargets . Class | AttributeTargets . Assembly , AllowMultiple = true ) ]
15
14
public class MinimumOSVersionAttribute : Attribute , ITestCondition
16
15
{
17
- private readonly OperatingSystems _excludedOperatingSystem ;
16
+ private readonly OperatingSystems _targetOS ;
18
17
private readonly Version _minVersion ;
19
- private readonly OperatingSystems _osPlatform ;
20
- private readonly Version _osVersion ;
18
+ private readonly OperatingSystems _currentOS ;
19
+ private readonly Version _currentVersion ;
20
+ private readonly bool _skip ;
21
21
22
22
public MinimumOSVersionAttribute ( OperatingSystems operatingSystem , string minVersion ) :
23
- this (
24
- operatingSystem ,
25
- GetCurrentOS ( ) ,
26
- GetCurrentOSVersion ( ) ,
27
- Version . Parse ( minVersion ) )
23
+ this ( operatingSystem , Version . Parse ( minVersion ) , GetCurrentOS ( ) , GetCurrentOSVersion ( ) )
28
24
{
29
25
}
30
26
31
27
// to enable unit testing
32
- internal MinimumOSVersionAttribute (
33
- OperatingSystems operatingSystem , OperatingSystems osPlatform , Version osVersion , Version minVersion )
28
+ internal MinimumOSVersionAttribute ( OperatingSystems targetOS , Version minVersion , OperatingSystems currentOS , Version currentVersion )
34
29
{
35
- if ( operatingSystem != OperatingSystems . Windows )
30
+ if ( targetOS != OperatingSystems . Windows )
36
31
{
37
32
throw new NotImplementedException ( "Min version support is only implemented for Windows." ) ;
38
33
}
39
- _excludedOperatingSystem = operatingSystem ;
34
+ _targetOS = targetOS ;
40
35
_minVersion = minVersion ;
41
- _osPlatform = osPlatform ;
42
- _osVersion = osVersion ;
36
+ _currentOS = currentOS ;
37
+ _currentVersion = currentVersion ;
43
38
44
- SkipReason = $ "This test requires { _excludedOperatingSystem } { _minVersion } or later.";
39
+ // Do not skip other OS's, Use OSSkipConditionAttribute or a separate MinimumOSVersionAttribute for that.
40
+ _skip = _targetOS == _currentOS && _minVersion > _currentVersion ;
41
+ SkipReason = $ "This test requires { _targetOS } { _minVersion } or later.";
45
42
}
46
43
47
- public bool IsMet
48
- {
49
- get
50
- {
51
- // Do not skip other OS's, Use OSSkipConditionAttribute or a separate MinimumOSVersionAttribute for that.
52
- if ( _osPlatform != _excludedOperatingSystem )
53
- {
54
- return true ;
55
- }
56
-
57
- return _osVersion >= _minVersion ;
58
- }
59
- }
44
+ // Since a test would be executed only if 'IsMet' is true, return false if we want to skip
45
+ public bool IsMet => ! _skip ;
60
46
61
47
public string SkipReason { get ; set ; }
62
48
@@ -77,34 +63,16 @@ private static OperatingSystems GetCurrentOS()
77
63
throw new PlatformNotSupportedException ( ) ;
78
64
}
79
65
80
- private static Version GetCurrentOSVersion ( )
66
+ static private Version GetCurrentOSVersion ( )
81
67
{
82
- // currently not used on other OS's
83
68
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
84
69
{
85
- // Win10+
86
- var key = Registry . LocalMachine . OpenSubKey ( @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" ) ;
87
- var major = key . GetValue ( "CurrentMajorVersionNumber" ) as int ? ;
88
- var minor = key . GetValue ( "CurrentMinorVersionNumber" ) as int ? ;
89
-
90
- if ( major . HasValue && minor . HasValue )
91
- {
92
- return new Version ( major . Value , minor . Value ) ;
93
- }
94
-
95
- // CurrentVersion doesn't work past Win8.1
96
- var current = key . GetValue ( "CurrentVersion" ) as string ;
97
- if ( ! string . IsNullOrEmpty ( current ) && Version . TryParse ( current , out var currentVersion ) )
98
- {
99
- return currentVersion ;
100
- }
101
-
102
- // Environment.OSVersion doesn't work past Win8.
103
70
return Environment . OSVersion . Version ;
104
71
}
105
72
else
106
73
{
107
- return new Version ( ) ;
74
+ // Not implemented, but this will still be called before the OS check happens so don't throw.
75
+ return new Version ( 0 , 0 ) ;
108
76
}
109
77
}
110
78
}
0 commit comments