Skip to content

Commit

Permalink
Merge pull request #4569 from manfred-brands/Issue4564-SupportedOSPla…
Browse files Browse the repository at this point in the history
…tform

Expand SupportOSPlatform to mean minimum supported version
  • Loading branch information
manfred-brands committed Nov 29, 2023
2 parents d91b4cf + 5e557a6 commit 866d5a4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 23 deletions.
6 changes: 6 additions & 0 deletions nunit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".config", ".config", "{0874
.config\dotnet-tools.json = .config\dotnet-tools.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "windows-tests", "src\NUnitFramework\windows-tests\windows-tests.csproj", "{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -131,6 +133,10 @@ Global
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59DC5039-5FCD-4C0C-AC92-7F06610136D8}.Release|Any CPU.Build.0 = Release|Any CPU
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C19F745-B5F0-4A3A-B545-1E1DF4FA1F78}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
37 changes: 27 additions & 10 deletions src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal static class OSPlatformTranslator
private static readonly Type? OsPlatformAttributeType = Type.GetType("System.Runtime.Versioning.OSPlatformAttribute, System.Runtime", false);
private static readonly PropertyInfo? PlatformNameProperty = OsPlatformAttributeType?.GetProperty("PlatformName", typeof(string));

private static readonly int[] KnownWindowsVersions = { 7, 8, 10, 11 };

/// <summary>
/// Converts one or more .NET 5+ OSPlatformAttributes into a single NUnit PlatformAttribute
/// </summary>
Expand Down Expand Up @@ -70,17 +72,17 @@ static void Add(HashSet<string> set, string? platforms)
continue;
}

string nunitPlatform = Translate(platformName);
IEnumerable<string> nunitPlatforms = Translate(platformName);

Type type = osPlatformAttribute.GetType();

if (type.FullName == "System.Runtime.Versioning.SupportedOSPlatformAttribute")
{
includes.Add(nunitPlatform);
includes.UnionWith(nunitPlatforms);
}
else if (type.FullName == "System.Runtime.Versioning.UnsupportedOSPlatformAttribute")
{
excludes.Add(nunitPlatform);
excludes.UnionWith(nunitPlatforms);
}

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

internal static string Translate(string platformName)
internal static IEnumerable<string> Translate(string platformName)
{
ParseOSAndVersion(platformName, out string os, out int majorVersion);
string nunit = Translate(os, majorVersion);
IEnumerable<string> nunit = Translate(os, majorVersion);

return nunit;
}
Expand All @@ -126,19 +128,34 @@ internal static string Translate(string platformName)
}
}

private static string Translate(string osName, int majorVersion)
private static IEnumerable<string> Translate(string osName, int majorVersion)
{
switch (osName.ToUpperInvariant())
{
case "WINDOWS":
return majorVersion < 7 ? "Win" : "Windows" + majorVersion;
if (majorVersion < 7)
{
yield return "Win";
}
else
{
foreach (var version in KnownWindowsVersions)
{
if (version >= majorVersion)
yield return "Windows" + version;
}
}
break;
case "OSX":
case "MACOS":
return "MacOsX";
yield return "MacOsX";
break;
case "LINUX":
return "Linux";
yield return "Linux";
break;
default:
return osName; // It might or more likely is not support by NUnit.
yield return osName; // It might or more likely is not support by NUnit.
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ public void EnsureParallelTestsRunInNewInstance3()

private void OutputReferenceId(string location)
{
if (!ObjectIds.TryGetValue(this, out long id))
long id;

lock (ObjectIds)
{
ObjectIds[this] = id = ObjectIds.Count + 1;
if (!ObjectIds.TryGetValue(this, out id))
{
ObjectIds[this] = id = ObjectIds.Count + 1;
}
}

TestContext.WriteLine($"{location}: {id}>");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public void SupportedForwardSlashDirectorySeparator()
Assert.That(Path.DirectorySeparatorChar, Is.EqualTo('/'));
}

[SupportedOSPlatform("Windows")]
[SupportedOSPlatform("Windows10.0")]
[SupportedOSPlatform("Windows11.0")]
[SupportedOSPlatform("Windows7.0")]
[Test]
public void SupportedBackwardSlashDirectorySeparator()
{
Expand Down
37 changes: 29 additions & 8 deletions src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,42 @@ namespace NUnit.Framework.Tests.Attributes
public class OSPlatformTranslatorTests
{
[TestCase("Windows", ExpectedResult = "Win")]
[TestCase("Windows7.0", ExpectedResult = "Windows7")]
[TestCase("Windows10.0", ExpectedResult = "Windows10")]
[TestCase("Windows7.0", ExpectedResult = "Windows7,Windows8,Windows10,Windows11")]
[TestCase("Windows10.0", ExpectedResult = "Windows10,Windows11")]
[TestCase("Windows11.0", ExpectedResult = "Windows11")]
[TestCase("Linux", ExpectedResult = "Linux")]
[TestCase("OSX", ExpectedResult = "MacOsX")]
[TestCase("MacOS", ExpectedResult = "MacOsX")]
[TestCase("Android", ExpectedResult = "Android")]
public string TranslatePlatform(string platformName)
{
return OSPlatformTranslator.Translate(platformName);
return string.Join(",", OSPlatformTranslator.Translate(platformName));
}

#if NET5_0_OR_GREATER
[Test]
public void TranslateSupportedOSPlatformAttribute()
public void TranslateSupportedOSPlatformAttributeWindows7()
{
var supported = new SupportedOSPlatformAttribute("Windows7.0");

PlatformAttribute platform = TranslateIntoSinglePlatform(supported);
Assert.That(platform.Include, Is.EqualTo("Windows7"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows8"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows11"), nameof(platform.Include));
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
}

[Test]
public void TranslateSupportedOSPlatformAttributeWindows10()
{
var supported = new SupportedOSPlatformAttribute("Windows10.0");

PlatformAttribute platform = TranslateIntoSinglePlatform(supported);
Assert.That(platform.Include, Does.Not.Contain("Windows7"), nameof(platform.Include));
Assert.That(platform.Include, Does.Not.Contain("Windows8"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows11"), nameof(platform.Include));
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
}

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

PlatformAttribute platform = TranslateIntoSinglePlatform(osPlatforms);
Assert.That(platform.Include, Is.EqualTo("Windows7,Linux"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Linux"), nameof(platform.Include));
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
}

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

PlatformAttribute platform = TranslateIntoSinglePlatform(supported1, unsupported, supported2);
Assert.That(platform.Include, Is.EqualTo("Windows7,Linux"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows7"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Linux"), nameof(platform.Include));
Assert.That(platform.Exclude, Is.EqualTo("Android"), nameof(platform.Exclude));
}

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

PlatformAttribute platform = TranslateIntoSinglePlatform(sourcePlatform, supported1, supported2);
Assert.That(platform.Include, Is.EqualTo("Win,Windows10"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Win"), nameof(platform.Include));
Assert.That(platform.Include, Does.Contain("Windows10"), nameof(platform.Include));
Assert.That(platform.Exclude, Is.Null, nameof(platform.Exclude));
}

Expand Down
27 changes: 27 additions & 0 deletions src/NUnitFramework/windows-tests/WindowsOnlyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using NUnit.Framework;

namespace NUnit.Windows.Tests
{
[TestFixture]
public sealed class PathTest
{
[Test]
public void DirectorySeparator()
{
Assert.That(Path.DirectorySeparatorChar, Is.EqualTo('\\'));
}

[Test]
public void VolumeSeparator()
{
Assert.That(Path.VolumeSeparatorChar, Is.EqualTo(':'));
}

[Test]
public void PathSeparator()
{
Assert.That(Path.PathSeparator, Is.EqualTo(';'));
}
}
}
20 changes: 20 additions & 0 deletions src/NUnitFramework/windows-tests/windows-tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0-windows7;net8.0-windows10.0.19041.0</TargetFrameworks>
<RootNamespace>NUnit.Windows.Tests</RootNamespace>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\framework\nunit.framework.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="NUnit.Analyzers" />
</ItemGroup>

</Project>

0 comments on commit 866d5a4

Please sign in to comment.