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

Merged
merged 17 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ public static partial class Environment
{
private static OperatingSystem GetOSVersion()
{
Version version = new Version(Interop.Sys.iOSSupportVersion());
Version version = Version.Parse(Interop.Sys.iOSSupportVersion());

// Check if build and revision are -1 and default them to 0
int major = version.Major;
int minor = version.Minor;
int build = version.Build < 0 ? 0 : version.Build;
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved
int revision = version.Revision < 0 ? 0 : version.Revision;
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved

version = new Version(major, minor, build, revision);
kotlarmilos marked this conversation as resolved.
Show resolved Hide resolved

return new OperatingSystem(PlatformID.Unix, version);
}
}
Expand Down
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 Down Expand Up @@ -281,5 +288,18 @@ private static void AssertVersionChecks(bool isCurrentOS, Func<int, int, int, bo
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