Skip to content

Commit 42bf238

Browse files
authored
fix: Init method in Env.cs for better initialization (#906)
* Refactor Init method in Env.cs for better initialization 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. * Ensure _initialized is set only after successful deserialization 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. * Refactor path handling and update StreamReader syntax 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. * Refactor file path construction for "env.json" 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.
1 parent 86fbe1b commit 42bf238

File tree

1 file changed

+8
-6
lines changed
  • test/integration/helpers

1 file changed

+8
-6
lines changed

test/integration/helpers/Env.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ public class Env
1515

1616
private static void Init()
1717
{
18+
if (_initialized) return;
19+
1820
_env = new Dictionary<string, JsonElement>
1921
{
2022
{ "DEV", JsonDocument.Parse("true").RootElement },
2123
{ "isRemoteAppiumServer", JsonDocument.Parse("false").RootElement },
2224
{ "remoteAppiumServerUri", JsonDocument.Parse("\"http://localhost:4723\"").RootElement }
2325
};
2426

25-
if (_initialized) return;
26-
2727
try
2828
{
29-
_initialized = true;
30-
var path = AppDomain.CurrentDomain.BaseDirectory;
31-
var sr = new StreamReader(path + "env.json");
32-
var jsonString = sr.ReadToEnd();
29+
string path = AppDomain.CurrentDomain.BaseDirectory;
30+
var filepath = Path.Combine(path, "env.json");
31+
32+
StreamReader sr = new(filepath);
33+
string jsonString = sr.ReadToEnd();
3334
_env = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(jsonString, new JsonSerializerOptions
3435
{
3536
PropertyNameCaseInsensitive = true
3637
});
38+
_initialized = true;
3739
}
3840
catch (JsonException jsonEx)
3941
{

0 commit comments

Comments
 (0)