Skip to content

Commit

Permalink
[tests/xharness] Add support for setting test configuration using env…
Browse files Browse the repository at this point in the history
…ironment variables. (#18738)

Setting test configuration variables using the environment is useful
when running tests on a Windows machine (easier than having to deal with
make).

Also refactor the code a bit to not use constants, and more consistent
naming.
  • Loading branch information
rolfbjarne authored Aug 16, 2023
1 parent 4d1156b commit 47b5601
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
6 changes: 5 additions & 1 deletion tests/common/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ static IList<string> GetVariableArray (string variable, string @default = "")

public static string EvaluateVariable (string variable)
{
var result = Environment.GetEnvironmentVariable (variable);
if (!string.IsNullOrEmpty (result))
return result;

var output = new StringBuilder ();
var rv = ExecutionHelper.Execute ("/usr/bin/make", new string [] { "-C", Path.Combine (SourceRoot, "tools", "devops"), "print-abspath-variable", $"VARIABLE={variable}" }, environmentVariables: null, stdout: output, stderr: output, timeout: TimeSpan.FromSeconds (5));
if (rv != 0)
throw new Exception ($"Failed to evaluate variable '{variable}'. Exit code: {rv}. Output:\n{output}");
var result = output.ToString ().Split (new char [] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Where (v => v.StartsWith (variable + "=", StringComparison.Ordinal)).SingleOrDefault ();
result = output.ToString ().Split (new char [] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).Where (v => v.StartsWith (variable + "=", StringComparison.Ordinal)).SingleOrDefault ();
if (result is null)
throw new Exception ($"Could not find the variable '{variable}' to evaluate.");
return result.Substring (variable.Length + 1);
Expand Down
58 changes: 35 additions & 23 deletions tests/xharness/Harness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ string MlaunchPath {
// the Make.config will use the wrong version. The CI set the version in the environment variable {platform}_WORKLOAD_VERSION via a script.
var workloadVersion = Environment.GetEnvironmentVariable ($"{platform.ToUpperInvariant ()}_WORKLOAD_VERSION");
mlaunchPath = Path.Combine (mlaunchPath, $"Microsoft.{platform}.Sdk",
string.IsNullOrEmpty (workloadVersion) ? config [$"{platform.ToUpperInvariant ()}_NUGET_VERSION_NO_METADATA"] : workloadVersion);
string.IsNullOrEmpty (workloadVersion) ? GetVariable ($"{platform.ToUpperInvariant ()}_NUGET_VERSION_NO_METADATA") : workloadVersion);
mlaunchPath = Path.Combine (mlaunchPath, "tools", "bin", "mlaunch");
return mlaunchPath;
} else if (INCLUDE_XAMARIN_LEGACY && INCLUDE_IOS) {
Expand All @@ -178,6 +178,21 @@ string MlaunchPath {
}
}

bool IsVariableSet (string variable)
{
return !string.IsNullOrEmpty (GetVariable (variable));
}

string GetVariable (string variable, string @default = null)
{
var result = Environment.GetEnvironmentVariable (variable);
if (string.IsNullOrEmpty (result))
config.TryGetValue (variable, out result);
if (string.IsNullOrEmpty (result))
result = @default;
return result;
}

public List<iOSTestProject> IOSTestProjects { get; }
public List<MacTestProject> MacTestProjects { get; } = new List<MacTestProject> ();

Expand Down Expand Up @@ -207,7 +222,7 @@ string MlaunchPath {
public bool INCLUDE_XAMARIN_LEGACY { get; }
public string SYSTEM_MONO { get; set; }
public string DOTNET_DIR { get; set; }
public string DotNetTfm { get; set; }
public string DOTNET_TFM { get; set; }

// Run

Expand Down Expand Up @@ -265,35 +280,32 @@ public Harness (IResultParser resultParser, HarnessAction action, HarnessConfigu

LaunchTimeout = InCI ? 3 : 120;

var config = ParseConfigFiles ();
config = ParseConfigFiles ();
var src_root = Path.GetDirectoryName (Path.GetFullPath (RootDirectory));

MONO_PATH = Path.GetFullPath (Path.Combine (src_root, "external", "mono"));
TVOS_MONO_PATH = MONO_PATH;
INCLUDE_IOS = config.ContainsKey ("INCLUDE_IOS") && !string.IsNullOrEmpty (config ["INCLUDE_IOS"]);
INCLUDE_TVOS = config.ContainsKey ("INCLUDE_TVOS") && !string.IsNullOrEmpty (config ["INCLUDE_TVOS"]);
JENKINS_RESULTS_DIRECTORY = config ["JENKINS_RESULTS_DIRECTORY"];
INCLUDE_WATCH = config.ContainsKey ("INCLUDE_WATCH") && !string.IsNullOrEmpty (config ["INCLUDE_WATCH"]);
INCLUDE_MAC = config.ContainsKey ("INCLUDE_MAC") && !string.IsNullOrEmpty (config ["INCLUDE_MAC"]);
INCLUDE_MACCATALYST = config.ContainsKey ("INCLUDE_MACCATALYST") && !string.IsNullOrEmpty (config ["INCLUDE_MACCATALYST"]);
MAC_DESTDIR = config ["MAC_DESTDIR"];

IOS_DESTDIR = config ["IOS_DESTDIR"];
MONO_IOS_SDK_DESTDIR = config ["MONO_IOS_SDK_DESTDIR"];
MONO_MAC_SDK_DESTDIR = config ["MONO_MAC_SDK_DESTDIR"];
ENABLE_DOTNET = config.ContainsKey ("ENABLE_DOTNET") && !string.IsNullOrEmpty (config ["ENABLE_DOTNET"]);
SYSTEM_MONO = config ["SYSTEM_MONO"];
DOTNET_DIR = config ["DOTNET_DIR"];
INCLUDE_XAMARIN_LEGACY = config.ContainsKey ("INCLUDE_XAMARIN_LEGACY") && !string.IsNullOrEmpty (config ["INCLUDE_XAMARIN_LEGACY"]);
DotNetTfm = config ["DOTNET_TFM"];
INCLUDE_IOS = IsVariableSet (nameof (INCLUDE_IOS));
INCLUDE_TVOS = IsVariableSet (nameof (INCLUDE_TVOS));
JENKINS_RESULTS_DIRECTORY = GetVariable (nameof (JENKINS_RESULTS_DIRECTORY));
INCLUDE_WATCH = IsVariableSet (nameof (INCLUDE_WATCH));
INCLUDE_MAC = IsVariableSet (nameof (INCLUDE_MAC));
INCLUDE_MACCATALYST = IsVariableSet (nameof (INCLUDE_MACCATALYST));
MAC_DESTDIR = GetVariable (nameof (MAC_DESTDIR));
IOS_DESTDIR = GetVariable (nameof (IOS_DESTDIR));
MONO_IOS_SDK_DESTDIR = GetVariable (nameof (MONO_IOS_SDK_DESTDIR));
MONO_MAC_SDK_DESTDIR = GetVariable (nameof (MONO_MAC_SDK_DESTDIR));
ENABLE_DOTNET = IsVariableSet (nameof (ENABLE_DOTNET));
SYSTEM_MONO = GetVariable (nameof (SYSTEM_MONO));
DOTNET_DIR = GetVariable (nameof (DOTNET_DIR));
INCLUDE_XAMARIN_LEGACY = IsVariableSet (nameof (INCLUDE_XAMARIN_LEGACY));
DOTNET_TFM = GetVariable (nameof (DOTNET_TFM));

if (string.IsNullOrEmpty (SdkRoot))
SdkRoot = config ["XCODE_DEVELOPER_ROOT"] ?? configuration.SdkRoot;

this.config = config;
SdkRoot = GetVariable ("XCODE_DEVELOPER_ROOT", configuration.SdkRoot);

processManager = new MlaunchProcessManager (XcodeRoot, MlaunchPath);
AppBundleLocator = new AppBundleLocator (processManager, () => HarnessLog, XIBuildPath, "/usr/local/share/dotnet/dotnet", config ["DOTNET"]);
AppBundleLocator = new AppBundleLocator (processManager, () => HarnessLog, XIBuildPath, "/usr/local/share/dotnet/dotnet", GetVariable ("DOTNET"));
TunnelBore = new TunnelBore (processManager);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/xharness/IHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface IHarness {
bool INCLUDE_XAMARIN_LEGACY { get; }
string SYSTEM_MONO { get; set; }
string DOTNET_DIR { get; set; }
string DOTNET_TFM { get; }
string XcodeRoot { get; }
string LogDirectory { get; }
double Timeout { get; }
Expand All @@ -64,7 +65,6 @@ public interface IHarness {
bool UseGroupedApps { get; }
string VSDropsUri { get; }
bool DisableWatchOSOnWrench { get; }
string DotNetTfm { get; }

#endregion

Expand Down
2 changes: 1 addition & 1 deletion tests/xharness/Targets/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public abstract class Target {

public const string FSharpGuid = "{F2A71F9B-5D33-465A-A702-920D77279786}";
public const string CSharpGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
public string DotNetTfm => Harness.DotNetTfm;
public string DotNetTfm => Harness.DOTNET_TFM;

public string LanguageGuid { get { return IsFSharp ? FSharpGuid : CSharpGuid; } }

Expand Down

6 comments on commit 47b5601

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.