csharp: Fix running headless tests in dotnet

Summary:
CoreCLR, the runtime of dotnet, has some issues regarding storing
environment variables (see dotnet/coreclr issue #15812), keeping them in
a local cache instead of flushing to the native `setenv`.

This commit replaces the usage of
`System.Environment.SetEnvironmentVariable` with a `setenv` wrapper.

Test Plan: Run without DISPLAY set and with dotnet.

Reviewers: felipealmeida, brunobelo, segfaultxavi, YOhoho

Reviewed By: brunobelo

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Differential Revision: https://phab.enlightenment.org/D10619
This commit is contained in:
Lauro Moura 2019-11-07 22:46:15 -03:00
parent 01bf9a34d6
commit 77207f9b58
6 changed files with 76 additions and 4 deletions

View File

@ -41,6 +41,7 @@ if get_option('build-tests')
mono_friend_assemblies += 'efl_sharp_test_suite'
else
mono_friend_assemblies += 'efl_mono_test'
mono_friend_assemblies += 'efl-mono-suite'
endif
endif

View File

@ -31,7 +31,7 @@ internal delegate void EinaFreeCb(IntPtr data);
}
internal static class NativeCustomExportFunctions
internal static partial class NativeCustomExportFunctions
{
[DllImport(efl.Libs.CustomExports)] public static extern void
efl_mono_native_free(IntPtr ptr);

View File

@ -0,0 +1,53 @@
using System;
using System.Runtime.InteropServices;
namespace Eina
{
/// <summary>
/// Class to deal with native Environment variables.
///
/// <para>To be used in place of <see cref="System.Environment" /> methods when
/// accessing the native environment directly.</para>
/// <para>Since EFL 1.24.</para>
/// </summary>
internal static class Environment
{
/// <summary>
/// Returns the value of the environment variable named <c>name</c>.
///
/// <para>Since EFL 1.24</para>
/// </summary>
/// <param name="name">The name of the variable to be retrieved</param>
/// <returns>The value of the variable. <c>null</c> if not set.</returns>
public static string GetEnv(string name)
{
return Eina.NativeCustomExportFunctions.efl_mono_native_getenv(name);
}
/// <summary>
/// Sets a native environment variable.
///
/// <para>Since EFL 1.24</para>
/// </summary>
/// <param name="name">The name of the variable</param>
/// <param name="value">The value to be set.</param>
/// <param name="overwrite"><c>true</c> if an existing variable must be overwritten.</param>
public static void SetEnv(string name, string value, bool overwrite=true)
{
Eina.Error error = Eina.NativeCustomExportFunctions.efl_mono_native_setenv(name, value, overwrite ? 1 : 0);
Eina.Error.Raise(error);
}
}
internal static partial class NativeCustomExportFunctions
{
[DllImport(efl.Libs.CustomExports, CharSet=CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))]
public static extern string efl_mono_native_getenv(string name);
[DllImport(efl.Libs.CustomExports, CharSet=CharSet.Ansi)]
public static extern Eina.Error efl_mono_native_setenv(string name, string value, int overwrite);
}
}

View File

@ -16,5 +16,6 @@ mono_files += files(
'eina_value.cs',
'eina_promises.cs',
'eina_accessor.cs',
'eina_strbuf.cs'
'eina_strbuf.cs',
'eina_environment.cs',
)

View File

@ -202,6 +202,17 @@ EAPI Efl_Substitute_Ctor_Cb efl_mono_avoid_top_level_constructor_callback_addr_g
return &_efl_mono_avoid_top_level_constructor_cb;
}
// Environment wrappers //
EAPI const char *efl_mono_native_getenv(const char *name)
{
return getenv(name);
}
EAPI Eina_Error efl_mono_native_setenv(const char *name, const char *value, int overwrite)
{
return setenv(name, value, overwrite);
}
// Iterator Wrapper //
typedef struct _Eina_Iterator_Wrapper_Mono

View File

@ -30,8 +30,13 @@ class TestMain
static int Main(string[] args)
{
if (Environment.GetEnvironmentVariable("ELM_ENGINE") == null)
Environment.SetEnvironmentVariable("ELM_ENGINE", "buffer");
/// We do not use System.Environment due to CoreCLR open issues regarding
/// setenv modifying the actual C environment. See issue #1592 in CoreCLR repo.
Eina.Config.Init();
if (Eina.Environment.GetEnv("ELM_ENGINE") == null)
{
Eina.Environment.SetEnv("ELM_ENGINE", "buffer", true);
}
Efl.All.Init(Efl.Csharp.Components.Ui);
@ -114,6 +119,7 @@ class TestMain
Console.WriteLine("[ END SUITE ] " + ckRunSuite);
Efl.All.Shutdown();
Eina.Config.Shutdown(); // For the extra init in getenv/setenv above
if (!pass)
return -1;