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

[dotnet] Add nullable reference annotations to Platform #14834

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

RenderMichael
Copy link
Contributor

@RenderMichael RenderMichael commented Nov 29, 2024

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Adds Nullable Reference Type annotations to the Platform type, as well as modernizes the code surrounding the platform handling.

Motivation and Context

Contributes to #14640

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

enhancement


Description

  • Added nullable reference type annotations to the Platform class, improving code safety and clarity.
  • Refactored the Platform class to use auto-properties and modern C# features like switch expressions.
  • Simplified platform detection logic in ChromiumDriverService and FirefoxDriverService by using the PlatformID enum instead of integer constants.
  • Improved the FromString method in the Platform class to use Enum.TryParse, enhancing robustness.

Changes walkthrough 📝

Relevant files
Enhancement
ChromiumDriverService.cs
Simplify platform detection using PlatformID enum               

dotnet/src/webdriver/Chromium/ChromiumDriverService.cs

  • Replaced integer constant with PlatformID enum for Mono Unix.
  • Simplified platform detection logic.
  • +3/-6     
    FirefoxDriverService.cs
    Simplify platform detection using PlatformID enum               

    dotnet/src/webdriver/Firefox/FirefoxDriverService.cs

  • Replaced integer constant with PlatformID enum for Mono Unix.
  • Simplified platform detection logic.
  • +2/-6     
    Platform.cs
    Add nullable annotations and modernize Platform class       

    dotnet/src/webdriver/Platform.cs

  • Enabled nullable reference types.
  • Refactored Platform class to use auto-properties.
  • Simplified IsPlatformType method using switch expressions.
  • Improved FromString method with Enum.TryParse.
  • +34/-81 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Null Reference
    The current field is marked as nullable but there's no null check in the CurrentPlatform property before dereferencing. While the null-coalescing operator is used, it's good practice to validate this critical platform-dependent code.

    Platform Detection
    The platform detection logic in the private constructor doesn't handle all possible PlatformID values. Some modern Windows versions might not be properly detected with the current version checks.

    Error Handling
    The FromString method silently falls back to PlatformType.Any when parsing fails. Consider logging a warning or throwing an exception for invalid platform names in debug builds.

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add support for Mono Unix platform detection to ensure consistent platform handling

    The platform detection in the constructor should handle the Mono Unix platform
    (PlatformID 128) to maintain consistency with other parts of the codebase.

    dotnet/src/webdriver/Platform.cs [105-131]

    +const PlatformID PlatformIDMonoUnix = (PlatformID)128;
     switch (Environment.OSVersion.Platform)
     {
         case PlatformID.Win32NT:
             if (this.MajorVersion == 5)
             {
                 this.PlatformType = PlatformType.XP;
             }
             else if (this.MajorVersion == 6)
             {
                 this.PlatformType = PlatformType.Vista;
             }
             else
             {
                 this.PlatformType = PlatformType.Windows;
             }
             break;
         case PlatformID.MacOSX:
             this.PlatformType = PlatformType.Mac;
             break;
         case PlatformID.Unix:
    +    case PlatformIDMonoUnix:
             this.PlatformType = PlatformType.Unix;
             break;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion ensures consistent platform detection by adding support for Mono Unix (PlatformID 128), aligning with the changes made in other files and preventing potential platform detection issues.

    8
    Explicitly handle all platform types in switch expression to prevent potential bugs

    The switch expression in IsPlatformType method should handle all possible
    PlatformType enum values explicitly. Add cases for Mac and Unix to avoid potential
    bugs when new platform types are added.

    dotnet/src/webdriver/Platform.cs [166-174]

     return compareTo switch
     {
         PlatformType.Any => true,
         PlatformType.Windows => this.PlatformType is PlatformType.Windows or PlatformType.XP or PlatformType.Vista,
         PlatformType.Vista => this.PlatformType is PlatformType.Windows or PlatformType.Vista,
         PlatformType.XP => this.PlatformType is PlatformType.Windows or PlatformType.XP,
         PlatformType.Linux => this.PlatformType is PlatformType.Linux or PlatformType.Unix,
    +    PlatformType.Mac => this.PlatformType == PlatformType.Mac,
    +    PlatformType.Unix => this.PlatformType == PlatformType.Unix,
         _ => this.PlatformType == compareTo,
     };
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves code maintainability and robustness by explicitly handling Mac and Unix cases in the switch expression, reducing the risk of bugs when new platform types are added.

    7

    💡 Need additional feedback ? start a PR chat

    return current;
    }
    }
    public static Platform CurrentPlatform => current ??= new Platform();
    Copy link
    Contributor Author

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    The only change I made here was using the modern ??= syntax. If we want to replace this with Lazy<Platform> I can make that change.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant