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: Init method in Env.cs for better initialization #906

Merged
merged 4 commits into from
Feb 13, 2025

Conversation

Dor-bl
Copy link
Collaborator

@Dor-bl Dor-bl commented Feb 12, 2025

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 apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change that adds functionality or value)
  • Breaking change (fix or feature that would cause existing functionality not to work as expected)
  • Test fix (non-breaking change that improves test stability or correctness)

Documentation

  • Have you proposed a file change/ PR with Appium to update documentation?

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

  • Have you provided integration tests for your changes? (required for Bugfix, New feature, or Test fix)

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

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;

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?

Copy link
Collaborator Author

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.
@Dor-bl Dor-bl merged commit 42bf238 into appium:main Feb 13, 2025
3 checks passed
@Dor-bl Dor-bl deleted the env_init_fix branch February 14, 2025 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants