-
Notifications
You must be signed in to change notification settings - Fork 189
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: Init method in Env.cs for better initialization #906
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reordered the Init method to check _initialized flag at the start, ensuring early return if already initialized. Moved _env dictionary initialization before the check to guarantee setup before any operations. Removed _initialized flag setting and env.json reading from the try block, simplifying the initialization logic.
Moved _initialized = true; to after JSON deserialization to ensure it is only set if deserialization is successful. Added a catch block for JsonException to handle parsing errors and output an error message to the console.
Refactored code for better readability and robustness: - Declared `path` as a `string`. - Ensured `path` ends with a directory separator. - Updated `StreamReader` initialization to new C# syntax.
_env = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(jsonString, new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}); | ||
_initialized = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it expected is remains false if an exception happens?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we don't want to set it to true if we fail.
Simplified the process of constructing the file path for "env.json" by replacing manual checks and concatenation with `Path.Combine`. This change makes the code more concise and less error-prone by leveraging built-in functionality for path handling.
mykola-mokhnach
approved these changes
Feb 13, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
List of changes
Reordered the Init method to check _initialized flag at the start, ensuring early return if already initialized. Moved _env dictionary initialization before the check to guarantee setup before any operations. Removed _initialized flag setting and env.json reading from the try block, simplifying the initialization logic.
Types of changes
What types of changes are you proposing/introducing to the .NET client?
Put an
x
in the boxes that applyDocumentation
This can be done by navigating to the documentation section on http://appium.io selecting the appropriate command/endpoint and clicking the 'Edit this doc' link to update the C# example
Integration tests
Details
Please provide more details about changes if necessary. You can provide code samples showing how they work and possible use cases if there are new features. Also, you can create gists with pasted C# code samples or put them here using markdown.
About markdown please read Mastering markdown and Writing on GitHub