From 5f6c8c290b90c389db4e1d3d2076b57bedafb855 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 29 Nov 2023 10:35:15 +0800 Subject: [PATCH 1/2] Add locking to dictionary access in ConcurrentTest --- .../Attributes/LifeCycleAttributeParallelTests.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/NUnitFramework/tests/Attributes/LifeCycleAttributeParallelTests.cs b/src/NUnitFramework/tests/Attributes/LifeCycleAttributeParallelTests.cs index 82cc1a25cd..06a7677827 100644 --- a/src/NUnitFramework/tests/Attributes/LifeCycleAttributeParallelTests.cs +++ b/src/NUnitFramework/tests/Attributes/LifeCycleAttributeParallelTests.cs @@ -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}>"); } } From 5e557a6919b208e4b8d3df1c854e1a675cd8bad5 Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Wed, 29 Nov 2023 10:09:32 +0800 Subject: [PATCH 2/2] Expand SupportOSPlatform to mean minimum supported version Windows7 => WIndows7, Windows8, Windows10, Windows11 Windows10 => Windows10, Windows11 Added windows only test assembly with MS generated SupportOSPlatform attribute --- nunit.sln | 6 +++ .../Attributes/OSPlatformTranslator.cs | 37 ++++++++++++++----- .../Attributes/OSPlatformAttributeTests.cs | 4 +- .../Attributes/OSPlatformTranslatorTests.cs | 37 +++++++++++++++---- .../windows-tests/WindowsOnlyTest.cs | 27 ++++++++++++++ .../windows-tests/windows-tests.csproj | 20 ++++++++++ 6 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 src/NUnitFramework/windows-tests/WindowsOnlyTest.cs create mode 100644 src/NUnitFramework/windows-tests/windows-tests.csproj diff --git a/nunit.sln b/nunit.sln index dee786eb20..e9c3bb4b6c 100644 --- a/nunit.sln +++ b/nunit.sln @@ -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 @@ -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 diff --git a/src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs b/src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs index 68d0413743..09d1ddc838 100644 --- a/src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs +++ b/src/NUnitFramework/framework/Attributes/OSPlatformTranslator.cs @@ -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 }; + /// /// Converts one or more .NET 5+ OSPlatformAttributes into a single NUnit PlatformAttribute /// @@ -70,17 +72,17 @@ static void Add(HashSet set, string? platforms) continue; } - string nunitPlatform = Translate(platformName); + IEnumerable 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 @@ -97,10 +99,10 @@ static void Add(HashSet set, string? platforms) } } - internal static string Translate(string platformName) + internal static IEnumerable Translate(string platformName) { ParseOSAndVersion(platformName, out string os, out int majorVersion); - string nunit = Translate(os, majorVersion); + IEnumerable nunit = Translate(os, majorVersion); return nunit; } @@ -126,19 +128,34 @@ internal static string Translate(string platformName) } } - private static string Translate(string osName, int majorVersion) + private static IEnumerable 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; } } } diff --git a/src/NUnitFramework/tests/Attributes/OSPlatformAttributeTests.cs b/src/NUnitFramework/tests/Attributes/OSPlatformAttributeTests.cs index 8cb32bb082..16c62ceecf 100644 --- a/src/NUnitFramework/tests/Attributes/OSPlatformAttributeTests.cs +++ b/src/NUnitFramework/tests/Attributes/OSPlatformAttributeTests.cs @@ -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() { diff --git a/src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs b/src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs index c83f5463b6..5b564a7252 100644 --- a/src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs +++ b/src/NUnitFramework/tests/Attributes/OSPlatformTranslatorTests.cs @@ -15,8 +15,8 @@ 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")] @@ -24,17 +24,33 @@ public class OSPlatformTranslatorTests [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)); } @@ -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)); } @@ -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)); } @@ -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)); } diff --git a/src/NUnitFramework/windows-tests/WindowsOnlyTest.cs b/src/NUnitFramework/windows-tests/WindowsOnlyTest.cs new file mode 100644 index 0000000000..77defcfe46 --- /dev/null +++ b/src/NUnitFramework/windows-tests/WindowsOnlyTest.cs @@ -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(';')); + } + } +} diff --git a/src/NUnitFramework/windows-tests/windows-tests.csproj b/src/NUnitFramework/windows-tests/windows-tests.csproj new file mode 100644 index 0000000000..9525e0ef2c --- /dev/null +++ b/src/NUnitFramework/windows-tests/windows-tests.csproj @@ -0,0 +1,20 @@ + + + + net6.0-windows7;net8.0-windows10.0.19041.0 + NUnit.Windows.Tests + true + true + + + + + + + + + + + + +