From b870836db01089e3e57bd68064ab3cc4e7494899 Mon Sep 17 00:00:00 2001 From: Peter Collins Date: Tue, 26 Nov 2024 21:37:30 -0500 Subject: [PATCH] [tests] Add apkdiff output to test attachment file (#9560) We've been seeing some strange output in the Azure Pipelines test output pane when apkdiff fails. This change should hopefully clean that up and instead upload an apkdiff.log file with the tools output when a related test fails. An issue with Mono.Android.NET_Tests Debug apk uploading has also been fixed. --- build-tools/automation/azure-pipelines.yaml | 2 +- .../Tests/Xamarin.Android.Build.Tests/BuildTest2.cs | 4 ++-- .../Utilities/BaseTest.cs | 12 +++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/build-tools/automation/azure-pipelines.yaml b/build-tools/automation/azure-pipelines.yaml index 1c8e2ce5b8f..361c7eb74fc 100644 --- a/build-tools/automation/azure-pipelines.yaml +++ b/build-tools/automation/azure-pipelines.yaml @@ -168,7 +168,7 @@ extends: testName: Mono.Android.NET_Tests-Debug project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj testResultsFiles: TestResult-Mono.Android.NET_Tests-Debug.xml - artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.apk + artifactSource: bin/TestDebug/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.apk artifactFolder: $(DotNetTargetFramework)-Debug - template: /build-tools/automation/yaml-templates/apk-instrumentation.yaml@self diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs index 058269ceaa4..01779040604 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest2.cs @@ -179,8 +179,8 @@ public void BuildReleaseArm64 ([Values (false, true)] bool forms) var apkFile = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, proj.PackageName + "-Signed.apk"); var apkDescPath = Path.Combine (Root, apkDescFilename); var apkDescReferencePath = Path.Combine (Root, b.ProjectDirectory, apkDescReference); - var (code, stdOut, stdErr) = RunApkDiffCommand ($"-s --save-description-2={apkDescPath} --descrease-is-regression {regressionCheckArgs} {apkDescReferencePath} {apkFile}"); - Assert.IsTrue (code == 0, $"apkdiff regression test failed with exit code: {code}\ncontext: https://github.com/xamarin/xamarin-android/blob/main/Documentation/project-docs/ApkSizeRegressionChecks.md\nstdOut: {stdOut}\nstdErr: {stdErr}"); + var (code, stdOut, stdErr) = RunApkDiffCommand ($"-s --save-description-2={apkDescPath} --descrease-is-regression {regressionCheckArgs} {apkDescReferencePath} {apkFile}", Path.Combine (Root, b.ProjectDirectory, "apkdiff.log")); + Assert.IsTrue (code == 0, $"apkdiff regression test failed with exit code: {code}. See test attachments."); } } diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs index 939f97603a0..1ce0280f232 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs @@ -101,12 +101,13 @@ protected static string RunAdbCommand (string command, bool ignoreErrors = true, return RunProcess (adb, $"{adbTarget} {command}", timeout); } - protected static (int code, string stdOutput, string stdError) RunApkDiffCommand (string args) + protected static (int code, string stdOutput, string stdError) RunApkDiffCommand (string args, string logFilePath) { string ext = Environment.OSVersion.Platform != PlatformID.Unix ? ".exe" : ""; + (int code, string stdOutput, string stdError) result; try { - return RunProcessWithExitCode ("apkdiff" + ext, args); + result = RunProcessWithExitCode ("apkdiff" + ext, args); } catch (System.ComponentModel.Win32Exception) { // apkdiff's location might not be in the $PATH, try known locations var profileDir = Environment.GetFolderPath (Environment.SpecialFolder.UserProfile); @@ -117,8 +118,13 @@ protected static (int code, string stdOutput, string stdError) RunApkDiffCommand apkdiffPath = Path.Combine (agentToolsDir, "apkdiff" + ext); } } - return RunProcessWithExitCode (apkdiffPath, args); + result = RunProcessWithExitCode (apkdiffPath, args); } + var logContent = $"apkdiff exited with code: {result.code}" + + $"\ncontext: https://github.com/xamarin/xamarin-android/blob/main/Documentation/project-docs/ApkSizeRegressionChecks.md" + + $"\nstdOut:\n{result.stdOutput}\nstdErr:\n{result.stdError}"; + File.WriteAllText (logFilePath, logContent); + return result; } protected static string RunProcess (string exe, string args, int timeoutInSeconds = 30)