Skip to content

Commit

Permalink
Fix cake scripts to work locally (#24111)
Browse files Browse the repository at this point in the history
* Fix cake scripts to work locally

* - fix for windows

* - cleanup device retrieval

* - fix processSettings.RedirectedStandardOutputHandler

* Add build tasks
  • Loading branch information
PureWeen authored Aug 16, 2024
1 parent 700fc8f commit 0370361
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 36 deletions.
12 changes: 9 additions & 3 deletions eng/cake/dotnet.cake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ string officialBuildId = Argument("officialbuildid", "");

string testFilter = Argument("test-filter", EnvironmentVariable("TEST_FILTER"));

var rootFolder = Context.Environment.WorkingDirectory;

if (rootFolder.FullPath.EndsWith("/devices", StringComparison.OrdinalIgnoreCase))
rootFolder = rootFolder.Combine("../../").Collapse();

var arcadeBin = MakeAbsolute(new DirectoryPath("./artifacts/bin/"));

string TestTFM = Argument("testtfm", "");
Expand Down Expand Up @@ -47,12 +52,12 @@ Task("dotnet")
if(!string.IsNullOrEmpty(nugetSource))
{
EnsureDirectoryExists(nugetSource);
var originalNuget = File("./NuGet.config");
var originalNuget = File($"{rootFolder}/NuGet.config");
ReplaceTextInFiles(originalNuget, "<add key=\"nuget-only\" value=\"true\" />", "");
ReplaceTextInFiles(originalNuget, "NUGET_ONLY_PLACEHOLDER", nugetSource);
}

DotNetBuild("./src/DotNet/DotNet.csproj", new DotNetBuildSettings
DotNetBuild($"{rootFolder}/src/DotNet/DotNet.csproj", new DotNetBuildSettings
{
MSBuildSettings = new DotNetMSBuildSettings()
.EnableBinaryLogger($"{GetLogDirectory()}/dotnet-{configuration}-{DateTime.UtcNow.ToFileTimeUtc()}.binlog")
Expand Down Expand Up @@ -101,7 +106,7 @@ Task("dotnet-buildtasks")
.IsDependentOn("dotnet")
.Does(() =>
{
RunMSBuildWithDotNet("./Microsoft.Maui.BuildTasks.slnf");
RunMSBuildWithDotNet($"{rootFolder}/Microsoft.Maui.BuildTasks.slnf");
})
.OnError(exception =>
{
Expand Down Expand Up @@ -158,6 +163,7 @@ Task("dotnet-build")
});

Task("dotnet-samples")
.IsDependentOn("dotnet-buildtasks")
.Does(() =>
{
var tempDir = PrepareSeparateBuildContext("samplesTest");
Expand Down
16 changes: 8 additions & 8 deletions eng/devices/android.cake
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#addin nuget:?package=Cake.Android.Adb&version=3.2.0
#addin nuget:?package=Cake.Android.AvdManager&version=2.2.0
#load "../cake/helpers.cake"
#load "../cake/dotnet.cake"
#load "./devices-shared.cake"
#load "./uitests-shared.cake"

const int DefaultApiLevel = 30;

Information("Local Dotnet: {0}", localDotnet);

string DEFAULT_ANDROID_PROJECT = "../../src/Controls/tests/TestCases.Android.Tests/Controls.TestCases.Android.Tests.csproj";
var projectPath = Argument("project", EnvironmentVariable("ANDROID_TEST_PROJECT") ?? DEFAULT_ANDROID_PROJECT);
var testDevice = Argument("device", EnvironmentVariable("ANDROID_TEST_DEVICE") ?? $"android-emulator-64_{DefaultApiLevel}");
Expand All @@ -23,9 +23,6 @@ var deviceSkin = Argument("skin", EnvironmentVariable("ANDROID_TEST_SKIN") ?? "N
var androidAvd = "DEVICE_TESTS_EMULATOR";
var androidAvdImage = "";
var deviceArch = "";
bool deviceBoot = Argument("boot", true);
bool deviceBootWait = Argument("wait", true);

var androidVersion = Argument("apiversion", EnvironmentVariable("ANDROID_PLATFORM_VERSION") ?? DefaultApiLevel.ToString());

// Directory setup
Expand Down Expand Up @@ -58,6 +55,7 @@ var dotnetToolPath = GetDotnetToolPath();
Setup(context =>
{
LogSetupInfo(dotnetToolPath);

PerformCleanupIfNeeded(deviceCleanupEnabled);

DetermineDeviceCharacteristics(testDevice, DefaultApiLevel);
Expand Down Expand Up @@ -87,18 +85,20 @@ Task("test")
});

Task("uitest-build")
.IsDependentOn("dotnet-buildtasks")
.Does(() =>
{
ExecuteBuildUITestApp(testAppProjectPath, testDevice, binlogDirectory, configuration, targetFramework, "", dotnetToolPath);

});

Task("uitest")
.Does(() =>
{
ExecuteUITests(projectPath, testAppProjectPath, testAppPackageName, testDevice, testResultsPath, binlogDirectory, configuration, targetFramework, "", androidVersion, dotnetToolPath, testAppInstrumentation);
});

Task("cg-uitest")
.IsDependentOn("dotnet-buildtasks")
.Does(() =>
{
ExecuteCGLegacyUITests(projectPath, testAppProjectPath, testAppPackageName, testDevice, testResultsPath, configuration, targetFramework, dotnetToolPath, testAppInstrumentation);
Expand Down Expand Up @@ -460,7 +460,7 @@ void HandleVirtualDevice(AndroidEmulatorToolSettings emuSettings, AndroidAvdMana
void CleanUpVirtualDevice(AndroidEmulatorProcess emulatorProcess, AndroidAvdManagerToolSettings avdSettings)
{
// no virtual device was used
if (emulatorProcess == null || !deviceBoot || TARGET.ToLower() == "boot")
if (emulatorProcess == null || !deviceBoot || targetBoot)
return;

//stop and cleanup the emulator
Expand Down
4 changes: 1 addition & 3 deletions eng/devices/catalyst.cake
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#load "../cake/helpers.cake"
#load "../cake/dotnet.cake"
#load "./devices-shared.cake"
#load "./uitests-shared.cake"

// Argument handling
string DEFAULT_MAC_PROJECT = "../../src/Controls/tests/TestCases.Mac.Tests/Controls.TestCases.Mac.Tests.csproj";
Expand Down
13 changes: 12 additions & 1 deletion eng/devices/devices-shared.cake
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,18 @@ void LogSetupInfo(string toolPath)
string GetDotnetToolPath()
{
var isLocalDotnet = GetBuildVariable("workloads", "local") == "local";
var toolPath = isLocalDotnet ? $"{MakeAbsolute(Directory("../../bin/dotnet/")).ToString()}/dotnet" : DotnetToolPathDefault;
string toolPath;


if(IsRunningOnWindows())
{
toolPath = isLocalDotnet ? $"{MakeAbsolute(Directory("../../bin/dotnet/")).ToString()}/dotnet" : null;
}
else
{
toolPath = isLocalDotnet ? $"{MakeAbsolute(Directory("../../bin/dotnet/")).ToString()}/dotnet" : DotnetToolPathDefault;
}

Information(isLocalDotnet ? "Using local dotnet" : "Using system dotnet");
return toolPath;
}
Expand Down
74 changes: 64 additions & 10 deletions eng/devices/ios.cake
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#addin nuget:?package=Cake.AppleSimulator&version=0.2.0
#load "../cake/helpers.cake"
#load "../cake/dotnet.cake"
#load "./devices-shared.cake"
#load "./uitests-shared.cake"

const string DefaultVersion = "17.2";
const string DefaultTestDevice = $"ios-simulator-64_{DefaultVersion}";

// Required arguments
string DEFAULT_IOS_PROJECT = "../../src/Controls/tests/TestCases.iOS.Tests/Controls.TestCases.iOS.Tests.csproj";
var projectPath = Argument("project", EnvironmentVariable("IOS_TEST_PROJECT") ?? DEFAULT_IOS_PROJECT);
var testDevice = Argument("device", EnvironmentVariable("IOS_TEST_DEVICE") ?? $"ios-simulator-64_{DefaultVersion}");
var testDevice = Argument("device", EnvironmentVariable("IOS_TEST_DEVICE") ?? DefaultTestDevice);
var targetFramework = Argument("tfm", EnvironmentVariable("TARGET_FRAMEWORK") ?? $"{DotnetVersion}-ios");
var binlogArg = Argument("binlog", EnvironmentVariable("IOS_TEST_BINLOG") ?? "");
var testApp = Argument("app", EnvironmentVariable("IOS_TEST_APP") ?? "");
Expand Down Expand Up @@ -44,6 +43,12 @@ var dotnetToolPath = GetDotnetToolPath();
Setup(context =>
{
LogSetupInfo(dotnetToolPath);

if (!deviceBoot)
{
return;
}

PerformCleanupIfNeeded(deviceCleanupEnabled, false);

// Device or simulator setup
Expand All @@ -58,10 +63,21 @@ Setup(context =>
}
});

Teardown(context => PerformCleanupIfNeeded(deviceCleanupEnabled, true));
Teardown(context =>
{
if (!deviceBoot || targetBoot)
{
return;
}

PerformCleanupIfNeeded(deviceCleanupEnabled, true);
});

Task("Cleanup");

// Todo this doesn't work for iOS currently
// Task("boot");

Task("Build")
.WithCriteria(!string.IsNullOrEmpty(projectPath))
.Does(() =>
Expand All @@ -77,10 +93,10 @@ Task("Test")
});

Task("uitest-build")
.IsDependentOn("dotnet-buildtasks")
.Does(() =>
{
ExecuteBuildUITestApp(testAppProjectPath, testDevice, binlogDirectory, configuration, targetFramework, runtimeIdentifier, dotnetToolPath);

});

Task("uitest")
Expand All @@ -91,6 +107,7 @@ Task("uitest")
});

Task("cg-uitest")
.IsDependentOn("dotnet-buildtasks")
.Does(() =>
{
ExecuteCGLegacyUITests(projectPath, testAppProjectPath, testDevice, testResultsPath, configuration, targetFramework, runtimeIdentifier, iosVersion, dotnetToolPath);
Expand Down Expand Up @@ -377,7 +394,7 @@ string GetDefaultRuntimeIdentifier(string testDeviceIdentifier)

void InstallIpa(string testApp, string testAppPackageName, string testDevice, string testResultsDirectory, string version, string toolPath)
{
Information("Install with xharness: {0}", testApp);
Information("Install with xharness: {0} testDevice:{1}", testApp, testDevice);
var settings = new DotNetToolSettings
{
ToolPath = toolPath,
Expand All @@ -389,6 +406,7 @@ void InstallIpa(string testApp, string testAppPackageName, string testDevice, st
$"--targets=\"{testDevice}\" " +
$"--output-directory=\"{testResultsDirectory}\" " +
$"--verbosity=\"Debug\" ");

if (testDevice.Contains("device"))
{
if (string.IsNullOrEmpty(DEVICE_UDID))
Expand Down Expand Up @@ -427,12 +445,14 @@ void InstallIpa(string testApp, string testAppPackageName, string testDevice, st
}
else
{
var simulatorName = "XHarness";
Information("Looking for simulator: {0} iosversion {1}", simulatorName, iosVersionToRun);
var UDID = GetUDID(testDevice, dotnetToolPath);
var sims = ListAppleSimulators();
var simXH = sims.Where(s => s.Name.Contains(simulatorName) && s.Name.Contains(iosVersionToRun)).FirstOrDefault();

var simXH = sims.Where(s => s.UDID == UDID).FirstOrDefault();
if (simXH == null)
{
throw new Exception("No simulator was found to run tests on.");
}

deviceToRun = simXH.UDID;
DEVICE_NAME = simXH.Name;
Expand All @@ -446,6 +466,40 @@ void InstallIpa(string testApp, string testAppPackageName, string testDevice, st
}
}

string GetUDID(string testDevice, string tool)
{
Information("Looking for simulator: {0}", testDevice);
string result = string.Empty;

DotNetTool("tool", new DotNetToolSettings
{
ToolPath = tool,
ArgumentCustomization = args => args.Append($"run xharness apple device {testDevice}"),
SetupProcessSettings = processSettings =>
{
processSettings.RedirectStandardOutput = true;
processSettings.RedirectedStandardOutputHandler = line =>
{
// The output from this command returns the UDID of the simulator
// and NULL so we're filtering out the NULL
if (!string.IsNullOrWhiteSpace(line))
{
result = line;
}

return line;
};
}
});

if(!string.IsNullOrWhiteSpace(result))
Information("Yay we found your device: {0}", result);
else
Information("No device found installed: {0}", testDevice);

return result;
}

void GetSimulators(string version, string tool)
{
Information("Getting simulators for version {0}", version);
Expand Down
13 changes: 13 additions & 0 deletions eng/devices/uitests-shared.cake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#load "../cake/helpers.cake"

if (!IsCIBuild() && GetBuildVariable("workloads", "notset") == "notset")
{
SetEnvironmentVariable("workloads", "global");
}

#load "../cake/dotnet.cake"
#load "./devices-shared.cake"

bool deviceBoot = Argument("boot", TARGET.ToLower() != "uitest-build");
bool targetBoot = TARGET.ToLower() == "boot";
bool deviceBootWait = Argument("wait", true);
4 changes: 1 addition & 3 deletions eng/devices/windows.cake
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#load "../cake/helpers.cake"
#load "../cake/dotnet.cake"
#load "./devices-shared.cake"
#load "./uitests-shared.cake"

using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
Expand Down
3 changes: 0 additions & 3 deletions eng/pipelines/common/ui-tests-build-sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ steps:
- pwsh: echo "##vso[task.prependpath]$(DotNet.Dir)"
displayName: 'Add .NET to PATH'

- pwsh: ./build.ps1 --target=dotnet-buildtasks --configuration="${{ parameters.configuration }}"
displayName: 'Build the MSBuild Tasks'

- pwsh: ./build.ps1 --target=dotnet-samples --configuration="${{ parameters.configuration }}" --${{ parameters.platform }} --verbosity=diagnostic --usenuget=false
displayName: 'Build the samples'

Expand Down
3 changes: 0 additions & 3 deletions eng/pipelines/common/ui-tests-compatibility-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ steps:
- pwsh: echo "##vso[task.prependpath]$(DotNet.Dir)"
displayName: 'Add .NET to PATH'

- pwsh: ./build.ps1 --target=dotnet-buildtasks --configuration="${{ parameters.configuration }}"
displayName: 'Build the MSBuild Tasks'

- pwsh: ./build.ps1 --target=${{ parameters.targetSample }} --configuration="${{ parameters.configuration }}" --${{ parameters.platform }} --verbosity=diagnostic
displayName: 'Build the Legacy ControlGallery'

Expand Down
11 changes: 9 additions & 2 deletions src/Controls/tests/TestCases.Shared.Tests/UITest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using NUnit.Framework;
using OpenQA.Selenium.Appium.iOS;
using UITest.Appium;
using UITest.Appium.NUnit;
using UITest.Core;
Expand Down Expand Up @@ -107,6 +108,7 @@ public void VerifyScreenshot(string? name = null)
void Verify(string? name)
{
string deviceName = GetTestConfig().GetProperty<string>("DeviceName") ?? string.Empty;

// Remove the XHarness suffix if present
deviceName = deviceName.Replace(" - created by XHarness", "", StringComparison.Ordinal);

Expand Down Expand Up @@ -136,16 +138,21 @@ but both can happen.
break;

case TestDevice.iOS:
if (deviceName == "iPhone Xs (iOS 17.2)")

var platformVersion = (string)((AppiumApp)App).Driver.Capabilities.GetCapability("platformVersion");
var device = (string)((AppiumApp)App).Driver.Capabilities.GetCapability("deviceName");

if (deviceName == "iPhone Xs (iOS 17.2)" || (device.Contains(" Xs", StringComparison.OrdinalIgnoreCase) && platformVersion == "17.2"))
{
environmentName = "ios";
}
else if (deviceName == "iPhone X (iOS 16.4)")
else if (deviceName == "iPhone X (iOS 16.4)" || (device.Contains(" X", StringComparison.OrdinalIgnoreCase) && platformVersion == "16.4"))
{
environmentName = "ios-iphonex";
}
else
{

Assert.Fail($"iOS visual tests should be run on iPhone Xs (iOS 17.2) or iPhone X (iOS 16.4) simulator images, but the current device is '{deviceName}'. Follow the steps on the MAUI UI testing wiki.");
}
break;
Expand Down

0 comments on commit 0370361

Please sign in to comment.