Skip to content

Commit 866d5a4

Browse files
Merge pull request #4569 from manfred-brands/Issue4564-SupportedOSPlatform
Expand SupportOSPlatform to mean minimum supported version
2 parents d91b4cf + 5e557a6 commit 866d5a4

7 files changed

Lines changed: 118 additions & 23 deletions

File tree

nunit.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".config", ".config", "{0874
7777
.config\dotnet-tools.json = .config\dotnet-tools.json
7878
EndProjectSection
7979
EndProject
80+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "windows-tests", "src\NUnitFramework\windows-tests\windows-tests.csproj", "{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}"
81+
EndProject
8082
Global
8183
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8284
Debug|Any CPU = Debug|Any CPU
@@ -131,6 +133,10 @@ Global
131133
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
132134
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
133135
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Release|Any CPU.Build.0 = Release|Any CPU
136+
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
137+
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Debug|Any CPU.Build.0 = Debug|Any CPU
138+
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Release|Any CPU.ActiveCfg = Release|Any CPU
139+
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Release|Any CPU.Build.0 = Release|Any CPU
134140
EndGlobalSection
135141
GlobalSection(SolutionProperties) = preSolution
136142
HideSolutionNode = FALSE

src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal static class OSPlatformTranslator
1515
private static readonly Type? OsPlatformAttributeType = Type.GetType("System.Runtime.Versioning.OSPlatformAttribute, System.Runtime", false);
1616
private static readonly PropertyInfo? PlatformNameProperty = OsPlatformAttributeType?.GetProperty("PlatformName", typeof(string));
1717

18+
private static readonly int[] KnownWindowsVersions = { 7, 8, 10, 11 };
19+
1820
/// <summary>
1921
/// Converts one or more .NET 5+ OSPlatformAttributes into a single NUnit PlatformAttribute
2022
/// </summary>
@@ -70,17 +72,17 @@ static void Add(HashSet<string> set, string? platforms)
7072
continue;
7173
}
7274

73-
string nunitPlatform = Translate(platformName);
75+
IEnumerable<string> nunitPlatforms = Translate(platformName);
7476

7577
Type type = osPlatformAttribute.GetType();
7678

7779
if (type.FullName == "System.Runtime.Versioning.SupportedOSPlatformAttribute")
7880
{
79-
includes.Add(nunitPlatform);
81+
includes.UnionWith(nunitPlatforms);
8082
}
8183
else if (type.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute")
8284
{
83-
excludes.Add(nunitPlatform);
85+
excludes.UnionWith(nunitPlatforms);
8486
}
8587

8688
// Ignore others, e.g. SupportedOSPlatformGuard
@@ -97,10 +99,10 @@ static void Add(HashSet<string> set, string? platforms)
9799
}
98100
}
99101

100-
internal static string Translate(string platformName)
102+
internal static IEnumerable<string> Translate(string platformName)
101103
{
102104
ParseOSAndVersion(platformName, out string os, out int majorVersion);
103-
string nunit = Translate(os, majorVersion);
105+
IEnumerable<string> nunit = Translate(os, majorVersion);
104106

105107
return nunit;
106108
}
@@ -126,19 +128,34 @@ private static void ParseOSAndVersion(string plaformName,
126128
}
127129
}
128130

129-
private static string Translate(string osName, int majorVersion)
131+
private static IEnumerable<string> Translate(string osName, int majorVersion)
130132
{
131133
switch (osName.ToUpperInvariant())
132134
{
133135
case "WINDOWS":
134-
return majorVersion < 7 ? "Win" : "Windows" + majorVersion;
136+
if (majorVersion < 7)
137+
{
138+
yield return "Win";
139+
}
140+
else
141+
{
142+
foreach (var version in KnownWindowsVersions)
143+
{
144+
if (version >= majorVersion)
145+
yield return "Windows" + version;
146+
}
147+
}
148+
break;
135149
case "OSX":
136150
case "MACOS":
137-
return "MacOsX";
151+
yield return "MacOsX";
152+
break;
138153
case "LINUX":
139-
return "Linux";
154+
yield return "Linux";
155+
break;
140156
default:
141-
return osName; // It might or more likely is not support by NUnit.
157+
yield return osName; // It might or more likely is not support by NUnit.
158+
break;
142159
}
143160
}
144161
}

src/NUnitFramework/tests/Attributes/LifeCycleAttributeParallelTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,16 @@ public void EnsureParallelTestsRunInNewInstance3()
5252

5353
private void OutputReferenceId(string location)
5454
{
55-
if (!ObjectIds.TryGetValue(this, out long id))
55+
long id;
56+
57+
lock (ObjectIds)
5658
{
57-
ObjectIds[this] = id = ObjectIds.Count + 1;
59+
if (!ObjectIds.TryGetValue(this, out id))
60+
{
61+
ObjectIds[this] = id = ObjectIds.Count + 1;
62+
}
5863
}
64+
5965
TestContext.WriteLine($"{location}: {id}>");
6066
}
6167
}

src/NUnitFramework/tests/Attributes/OSPlatformAttributeTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public void SupportedForwardSlashDirectorySeparator()
1717
Assert.That(Path.DirectorySeparatorChar, Is.EqualTo('/'));
1818
}
1919

20-
[SupportedOSPlatform("Windows")]
21-
[SupportedOSPlatform("Windows10.0")]
22-
[SupportedOSPlatform("Windows11.0")]
20+
[SupportedOSPlatform("Windows7.0")]
2321
[Test]
2422
public void SupportedBackwardSlashDirectorySeparator()
2523
{

src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,42 @@ namespace NUnit.Framework.Tests.Attributes
1515
public class OSPlatformTranslatorTests
1616
{
1717
[TestCase("Windows", ExpectedResult = "Win")]
18-
[TestCase("Windows7.0", ExpectedResult = "Windows7")]
19-
[TestCase("Windows10.0", ExpectedResult = "Windows10")]
18+
[TestCase("Windows7.0", ExpectedResult = "Windows7,Windows8,Windows10,Windows11")]
19+
[TestCase("Windows10.0", ExpectedResult = "Windows10,Windows11")]
2020
[TestCase("Windows11.0", ExpectedResult = "Windows11")]
2121
[TestCase("Linux", ExpectedResult = "Linux")]
2222
[TestCase("OSX", ExpectedResult = "MacOsX")]
2323
[TestCase("MacOS", ExpectedResult = "MacOsX")]
2424
[TestCase("Android", ExpectedResult = "Android")]
2525
public string TranslatePlatform(string platformName)
2626
{
27-
return OSPlatformTranslator.Translate(platformName);
27+
return string.Join(",", OSPlatformTranslator.Translate(platformName));
2828
}
2929

3030
#if NET5_0_OR_GREATER
3131
[Test]
32-
public void TranslateSupportedOSPlatformAttribute()
32+
public void TranslateSupportedOSPlatformAttributeWindows7()
3333
{
3434
var supported = new SupportedOSPlatformAttribute("Windows7.0");
3535

3636
PlatformAttribute platform = TranslateIntoSinglePlatform(supported);
37-
Assert.That(platform.Include, Is.EqualTo("Windows7"), nameof(platform.Include));
37+
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
38+
Assert.That(platform.Include, Does.Contain("Windows8"), nameof(platform.Include));
39+
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
40+
Assert.That(platform.Include, Does.Contain("Windows11"), nameof(platform.Include));
41+
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
42+
}
43+
44+
[Test]
45+
public void TranslateSupportedOSPlatformAttributeWindows10()
46+
{
47+
var supported = new SupportedOSPlatformAttribute("Windows10.0");
48+
49+
PlatformAttribute platform = TranslateIntoSinglePlatform(supported);
50+
Assert.That(platform.Include, Does.Not.Contain("Windows7"), nameof(platform.Include));
51+
Assert.That(platform.Include, Does.Not.Contain("Windows8"), nameof(platform.Include));
52+
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
53+
Assert.That(platform.Include, Does.Contain("Windows11"), nameof(platform.Include));
3854
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
3955
}
4056

@@ -56,7 +72,9 @@ public void TranslateMultipleOSPlatformAttributes()
5672
var osPlatforms = new OSPlatformAttribute[] { supported1, supported2 };
5773

5874
PlatformAttribute platform = TranslateIntoSinglePlatform(osPlatforms);
59-
Assert.That(platform.Include, Is.EqualTo("Windows7,Linux"), nameof(platform.Include));
75+
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
76+
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
77+
Assert.That(platform.Include, Does.Contain("Linux"), nameof(platform.Include));
6078
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
6179
}
6280

@@ -68,7 +86,9 @@ public void TranslateMixedOSPlatformAttributes()
6886
var unsupported = new UnsupportedOSPlatformAttribute("Android");
6987

7088
PlatformAttribute platform = TranslateIntoSinglePlatform(supported1, unsupported, supported2);
71-
Assert.That(platform.Include, Is.EqualTo("Windows7,Linux"), nameof(platform.Include));
89+
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
90+
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
91+
Assert.That(platform.Include, Does.Contain("Linux"), nameof(platform.Include));
7292
Assert.That(platform.Exclude, Is.EqualTo("Android"), nameof(platform.Exclude));
7393
}
7494

@@ -80,7 +100,8 @@ public void TranslateMixedPlatformAndOSPlatformAttributes()
80100
var sourcePlatform = new PlatformAttribute("Win");
81101

82102
PlatformAttribute platform = TranslateIntoSinglePlatform(sourcePlatform, supported1, supported2);
83-
Assert.That(platform.Include, Is.EqualTo("Win,Windows10"), nameof(platform.Include));
103+
Assert.That(platform.Include, Does.Contain("Win"), nameof(platform.Include));
104+
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
84105
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
85106
}
86107

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
4+
namespace NUnit.Windows.Tests
5+
{
6+
[TestFixture]
7+
public sealed class PathTest
8+
{
9+
[Test]
10+
public void DirectorySeparator()
11+
{
12+
Assert.That(Path.DirectorySeparatorChar, Is.EqualTo('\\'));
13+
}
14+
15+
[Test]
16+
public void VolumeSeparator()
17+
{
18+
Assert.That(Path.VolumeSeparatorChar, Is.EqualTo(':'));
19+
}
20+
21+
[Test]
22+
public void PathSeparator()
23+
{
24+
Assert.That(Path.PathSeparator, Is.EqualTo(';'));
25+
}
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net6.0-windows7;net8.0-windows10.0.19041.0</TargetFrameworks>
5+
<RootNamespace>NUnit.Windows.Tests</RootNamespace>
6+
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
7+
<EnableWindowsTargeting>true</EnableWindowsTargeting>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\framework\nunit.framework.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
16+
<PackageReference Include="NUnit3TestAdapter" />
17+
<PackageReference Include="NUnit.Analyzers" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)