From 4663a6e7cd3dcec7a2226bda7445179ec29c2944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emek=20Vysok=C3=BD?= Date: Thu, 4 Nov 2021 09:58:32 +0100 Subject: [PATCH] Provide simulator/device flag in diagnostics data (#757) --- .../Commands/Android/AndroidCommand.cs | 13 +++++++++++++ .../Commands/Android/AndroidGetDeviceCommand.cs | 4 +--- .../Commands/Android/AndroidInstallCommand.cs | 16 +++++++++++----- .../Commands/Android/AndroidRunCommand.cs | 11 ++++++++--- .../Commands/Android/AndroidTestCommand.cs | 2 -- .../Commands/Apple/AppleAppCommand.cs | 2 ++ .../Commands/Apple/AppleGetDeviceCommand.cs | 1 + .../CommandDiagnostics.cs | 8 ++++++++ .../integration-tests/Android/Device.Tests.proj | 4 ++++ .../Helix.SDK.configuration.props | 5 +++++ 10 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidCommand.cs index 681896f9b7ff5..d2a51129998de 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidCommand.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using Microsoft.DotNet.XHarness.CLI.CommandArguments; using Microsoft.DotNet.XHarness.CLI.Commands; using Microsoft.DotNet.XHarness.Common; @@ -20,5 +21,17 @@ protected AndroidCommand(string name, bool allowsExtraArgs, string? help = null) { _diagnosticsData = new(() => Services.BuildServiceProvider().GetRequiredService()); } + + protected static void FillDiagnosticData( + IDiagnosticsData data, + string deviceName, + int apiVersion, + IEnumerable apkRequiredArchitecture) + { + data.Target = string.Join(",", apkRequiredArchitecture); + data.TargetOS = "API " + apiVersion; + data.Device = deviceName; + data.IsDevice = !deviceName.ToLowerInvariant().StartsWith("emulator"); + } } } diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidGetDeviceCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidGetDeviceCommand.cs index 24ceca52838fd..11a4dadb9d489 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidGetDeviceCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidGetDeviceCommand.cs @@ -71,9 +71,7 @@ protected override Task InvokeInternal(ILogger logger) runner.SetActiveDevice(deviceToUse); - DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture); - DiagnosticsData.TargetOS = "API " + runner.APIVersion; - DiagnosticsData.Device = deviceToUse; + FillDiagnosticData(DiagnosticsData, deviceToUse, runner.APIVersion, apkRequiredArchitecture); Console.WriteLine(deviceToUse); diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidInstallCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidInstallCommand.cs index 4d5fe91c8ff6a..57a7830f30e10 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidInstallCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidInstallCommand.cs @@ -59,8 +59,6 @@ protected override Task InvokeInternal(ILogger logger) } } - DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture); - // Package Name is not guaranteed to match file name, so it needs to be mandatory. try { @@ -86,7 +84,15 @@ protected override Task InvokeInternal(ILogger logger) return Task.FromResult(ExitCode.GENERAL_FAILURE); } - public static ExitCode InvokeHelper(ILogger logger, string apkPackageName, string appPackagePath, IEnumerable apkRequiredArchitecture, string? deviceId, TimeSpan bootTimeoutSeconds, AdbRunner runner, IDiagnosticsData diagnosticsData) + public static ExitCode InvokeHelper( + ILogger logger, + string apkPackageName, + string appPackagePath, + IEnumerable apkRequiredArchitecture, + string? deviceId, + TimeSpan bootTimeoutSeconds, + AdbRunner runner, + IDiagnosticsData diagnosticsData) { using (logger.BeginScope("Initialization and setup of APK on device")) { @@ -105,8 +111,8 @@ public static ExitCode InvokeHelper(ILogger logger, string apkPackageName, strin } runner.SetActiveDevice(deviceId); - diagnosticsData.TargetOS = "API " + runner.APIVersion; - diagnosticsData.Device = deviceId; + + FillDiagnosticData(diagnosticsData, deviceId, runner.APIVersion, apkRequiredArchitecture); runner.TimeToWaitForBootCompletion = bootTimeoutSeconds; diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidRunCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidRunCommand.cs index e3b9b77b4f528..fec3c46b94bdb 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidRunCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidRunCommand.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Threading.Tasks; using Microsoft.DotNet.XHarness.Android; using Microsoft.DotNet.XHarness.Android.Execution; @@ -66,12 +67,16 @@ protected override Task InvokeInternal(ILogger logger) } runner.SetActiveDevice(deviceId); - DiagnosticsData.TargetOS = "API " + runner.APIVersion; - DiagnosticsData.Device = deviceId; + + // For test command, this was already filled during installation + if (DiagnosticsData.Device == null) + { + FillDiagnosticData(DiagnosticsData, deviceId, runner.APIVersion, Enumerable.Empty()); + } runner.TimeToWaitForBootCompletion = Arguments.LaunchTimeout; - // Wait til at least device(s) are ready + // Wait till at least device(s) are ready runner.WaitForDevice(); return Task.FromResult(InvokeHelper( diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidTestCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidTestCommand.cs index 18e7fe6529017..1a699b295e1c7 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidTestCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Android/AndroidTestCommand.cs @@ -66,8 +66,6 @@ protected override Task InvokeInternal(ILogger logger) logger.LogInformation($"Will attempt to run device on detected architecture: '{string.Join("', '", apkRequiredArchitecture)}'"); } - DiagnosticsData.Target = string.Join(",", apkRequiredArchitecture); - // Package Name is not guaranteed to match file name, so it needs to be mandatory. string apkPackageName = Arguments.PackageName; string appPackagePath = Arguments.AppPackagePath; diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleAppCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleAppCommand.cs index a22f51c204c85..dfd9c26723e6e 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleAppCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleAppCommand.cs @@ -10,6 +10,7 @@ using Microsoft.DotNet.XHarness.Common; using Microsoft.DotNet.XHarness.Common.CLI; using Microsoft.DotNet.XHarness.Common.Logging; +using Microsoft.DotNet.XHarness.iOS.Shared; using Microsoft.DotNet.XHarness.iOS.Shared.Hardware; using Microsoft.DotNet.XHarness.iOS.Shared.Logging; using Microsoft.DotNet.XHarness.iOS.Shared.Utilities; @@ -51,6 +52,7 @@ protected sealed override async Task Invoke(Extensions.Logging.ILogger var diagnosticsData = serviceProvider.GetRequiredService(); diagnosticsData.Target = Arguments.Target.Value.AsString(); + diagnosticsData.IsDevice = !Arguments.Target.Value.Platform.IsSimulator(); var cts = new CancellationTokenSource(); cts.CancelAfter(Arguments.Timeout); diff --git a/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleGetDeviceCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleGetDeviceCommand.cs index f920cc295abb3..e0feb9ad3f878 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleGetDeviceCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/Commands/Apple/AppleGetDeviceCommand.cs @@ -60,6 +60,7 @@ protected override async Task Invoke(Extensions.Logging.ILogger logger diagnosticsData.TargetOS = device.OSVersion.Split(' ', 2).Last(); diagnosticsData.Device = device.Name ?? device.UDID; + diagnosticsData.IsDevice = !target.Platform.IsSimulator(); Console.WriteLine(device.UDID); } diff --git a/src/Microsoft.DotNet.XHarness.Common/CommandDiagnostics.cs b/src/Microsoft.DotNet.XHarness.Common/CommandDiagnostics.cs index 4af591182f241..86197a81f6468 100644 --- a/src/Microsoft.DotNet.XHarness.Common/CommandDiagnostics.cs +++ b/src/Microsoft.DotNet.XHarness.Common/CommandDiagnostics.cs @@ -30,6 +30,11 @@ public interface IDiagnosticsData /// Name of the used device. /// string? Device { get; set; } + + /// + /// True when the target is a real HW device, false for simulators, maccatalyst.. + /// + bool? IsDevice { get; set; } } /// @@ -56,6 +61,9 @@ public class CommandDiagnostics : IDiagnosticsData [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] public string? Device { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public bool? IsDevice { get; set; } + public int Duration => (int)Math.Round(_timer.Elapsed.TotalSeconds); public CommandDiagnostics(ILogger logger, TargetPlatform platform, string command) diff --git a/tests/integration-tests/Android/Device.Tests.proj b/tests/integration-tests/Android/Device.Tests.proj index d450dfbaa190c..463f647d033f5 100644 --- a/tests/integration-tests/Android/Device.Tests.proj +++ b/tests/integration-tests/Android/Device.Tests.proj @@ -1,4 +1,8 @@ + + false + + diff --git a/tests/integration-tests/Helix.SDK.configuration.props b/tests/integration-tests/Helix.SDK.configuration.props index 9cd2d1c85c85b..a9aa5ef977e4d 100644 --- a/tests/integration-tests/Helix.SDK.configuration.props +++ b/tests/integration-tests/Helix.SDK.configuration.props @@ -16,6 +16,11 @@ https://helix.dot.net 1.0.0-ci + + + true + cp "$XHARNESS_DIAGNOSTICS_PATH" "$HELIX_WORKITEM_UPLOAD_ROOT";$(HelixPostCommands) + copy "%XHARNESS_DIAGNOSTICS_PATH%" "%HELIX_WORKITEM_UPLOAD_ROOT%";$(HelixPostCommands)