Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IsOSVersionAtLeast when build or revision are not provided #108748

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,19 @@ private static bool IsOSVersionAtLeast(int major, int minor, int build, int revi
{
return current.Minor > minor;
}
if (current.Build != build)
// Unspecified build component is to be treated as zero
int currentBuild = current.Build == -1 ? 0 : current.Build;
build = build < 0 ? 0 : build;
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
if (currentBuild != build)
{
return current.Build > build;
return currentBuild > build;
}

return current.Revision >= revision
|| (current.Revision == -1 && revision == 0); // it is unavailable on OSX and Environment.OSVersion.Version.Revision returns -1
// Unspecified revision component is to be treated as zero
int currentRevision = current.Revision == -1 ? 0 : current.Revision;
revision = revision < 0 ? 0 : revision;
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved

return currentRevision >= revision;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,18 @@ private static void TestIsOSVersionAtLeast(string currentOSName)
isCurrentOS = true;
}

// Four-parameter overload
AssertVersionChecks(isCurrentOS, (major, minor, build, revision) => OperatingSystem.IsOSPlatformVersionAtLeast(platformName, major, minor, build, revision));
AssertVersionChecks(isCurrentOS, (major, minor, build, revision) => OperatingSystem.IsOSPlatformVersionAtLeast(platformName.ToLower(), major, minor, build, revision));
AssertVersionChecks(isCurrentOS, (major, minor, build, revision) => OperatingSystem.IsOSPlatformVersionAtLeast(platformName.ToUpper(), major, minor, build, revision));

// Three-parameter overload
AssertVersionChecks(isCurrentOS, (major, minor, build) => OperatingSystem.IsOSPlatformVersionAtLeast(platformName, major, minor, build));

// Two-parameter overload
AssertVersionChecks(isCurrentOS, (major, minor) => OperatingSystem.IsOSPlatformVersionAtLeast(platformName, major, minor));
}

AssertVersionChecks(currentOSName.Equals("Android", StringComparison.OrdinalIgnoreCase), OperatingSystem.IsAndroidVersionAtLeast);
AssertVersionChecks(currentOSName == "MacCatalyst" || currentOSName.Equals("iOS", StringComparison.OrdinalIgnoreCase), OperatingSystem.IsIOSVersionAtLeast);
AssertVersionChecks(currentOSName.Equals("macOS", StringComparison.OrdinalIgnoreCase), OperatingSystem.IsMacOSVersionAtLeast);
Expand All @@ -256,8 +263,8 @@ private static void AssertVersionChecks(bool isCurrentOS, Func<int, int, int, in

Assert.False(isOSVersionAtLeast(current.Major + 1, current.Minor, current.Build, current.Revision));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor + 1, current.Build, current.Revision));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, current.Build + 1, current.Revision));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, current.Build, Math.Max(current.Revision + 1, 1))); // OSX Revision reports -1
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, Math.Max(current.Build + 1, 1), current.Revision));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, current.Build, Math.Max(current.Revision + 1, 1)));

Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor, current.Build, current.Revision));

Expand All @@ -273,13 +280,26 @@ private static void AssertVersionChecks(bool isCurrentOS, Func<int, int, int, bo

Assert.False(isOSVersionAtLeast(current.Major + 1, current.Minor, current.Build));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor + 1, current.Build));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, current.Build + 1));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor, Math.Max(current.Build + 1, 1)));

Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor, current.Build));

Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major - 1, current.Minor, current.Build));
Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor - 1, current.Build));
Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor, current.Build - 1));
}

private static void AssertVersionChecks(bool isCurrentOS, Func<int, int, bool> isOSVersionAtLeast)
{
Version current = Environment.OSVersion.Version;

Assert.False(isOSVersionAtLeast(current.Major + 1, current.Minor));
Assert.False(isOSVersionAtLeast(current.Major, current.Minor + 1));

Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor));

Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major - 1, current.Minor));
Assert.Equal(isCurrentOS, isOSVersionAtLeast(current.Major, current.Minor - 1));
}
}
}
Loading