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

build(deps): Migration from Newtonsoft.Json to System.Text.Json package #836

Merged
merged 4 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion test/integration/Appium.Net.Integration.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Appium.Net\Appium.Net.csproj" />
Expand Down
58 changes: 40 additions & 18 deletions test/integration/helpers/Env.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Text.Json;

namespace Appium.Net.Integration.Tests.helpers
{
Expand All @@ -10,32 +10,45 @@ public class Env
public static TimeSpan InitTimeoutSec = TimeSpan.FromSeconds(180);
public static TimeSpan ImplicitTimeoutSec = TimeSpan.FromSeconds(10);

private static Dictionary<string, string> _env;
private static Dictionary<string, JsonElement> _env;
private static bool _initialized;

private static void Init()
{
_env = new Dictionary<string, JsonElement>
{
{ "DEV", JsonDocument.Parse("true").RootElement },
{ "isRemoteAppiumServer", JsonDocument.Parse("false").RootElement },
{ "remoteAppiumServerUri", JsonDocument.Parse("\"http://localhost:4723\"").RootElement }
};

if (_initialized) return;

try
{
if (!_initialized)
_initialized = true;
var path = AppDomain.CurrentDomain.BaseDirectory;
var sr = new StreamReader(path + "env.json");
var jsonString = sr.ReadToEnd();
_env = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(jsonString, new JsonSerializerOptions
{
_initialized = true;
var path = AppDomain.CurrentDomain.BaseDirectory;
var sr = new StreamReader(path + "env.json");
var jsonString = sr.ReadToEnd();
_env = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonString);
}
PropertyNameCaseInsensitive = true
});
}
catch
catch (JsonException jsonEx)
{
_env = new Dictionary<string, string>();
Console.WriteLine($"Error parsing JSON: {jsonEx.Message}");
Dor-bl marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex)
{
Console.WriteLine($"Error initializing environment: {ex.Message}");
}
}

private static bool IsTrue(string val)
private static bool IsTrue(object val)
{
val = val?.ToLower().Trim();
return (val == "true") || (val == "1");
val = val?.ToString().ToLower().Trim();
Dor-bl marked this conversation as resolved.
Show resolved Hide resolved
return val.Equals("true") || val.Equals("1");
}

public static bool ServerIsRemote()
Expand All @@ -47,16 +60,25 @@ public static bool ServerIsRemote()
public static bool ServerIsLocal()
{
Init();
return _env.ContainsKey("DEV") && IsTrue(_env["DEV"]) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
return (_env.ContainsKey("DEV") && IsTrue(_env["DEV"])) || IsTrue(Environment.GetEnvironmentVariable("DEV"));
}

public static string GetEnvVar(string name)
{
if (_env.ContainsKey(name) && (_env[name] != null))
if (_env.ContainsKey(name))
{
return _env[name];
JsonElement element = _env[name];

return element.ValueKind switch
{
JsonValueKind.String => element.GetString(),
JsonValueKind.Number => element.GetRawText(),
JsonValueKind.True or JsonValueKind.False => element.GetRawText(),
JsonValueKind.Null => null,
_ => element.GetRawText()
};
}
return Environment.GetEnvironmentVariable(name);
return Environment.GetEnvironmentVariable(name);
}
}
}