Skip to content

Commit c458a05

Browse files
[tests] Make DotNetPublish pack assertions deterministic (#12359)
Assert against the reference and runtime pack paths resolved in MSBuild's detailed output instead of relying on filesystem enumeration order. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 0e4fecd commit c458a05

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

  • src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,13 @@ public void DotNetPublish ([Values] bool isRelease, [ValueSource (nameof(Get_Dot
355355

356356
// Only check latest TFM, as previous or preview TFMs will come from NuGet
357357
if (dotnetVersion == XABuildConfig.LatestDotNetTargetFramework && !preview) {
358+
var buildOutput = dotnet.LastBuildOutput.ToArray ();
358359
var versionString = apiLevel.Minor == 0 ? $"{apiLevel.Major}" : $"{apiLevel.Major}.{apiLevel.Minor}";
359-
var refDirectory = Directory.GetDirectories (Path.Combine (TestEnvironment.DotNetPreviewPacksDirectory, $"Microsoft.Android.Ref.{versionString}")).LastOrDefault ();
360-
var expectedMonoAndroidRefPath = Path.Combine (refDirectory, "ref", dotnetVersion, "Mono.Android.dll");
361-
Assert.IsTrue (dotnet.LastBuildOutput.ContainsText (expectedMonoAndroidRefPath), $"Build should be using {expectedMonoAndroidRefPath}");
360+
AssertUsingPack (buildOutput, $"Microsoft.Android.Ref.{versionString}", "ref", dotnetVersion, "Mono.Android.dll");
362361

363362
var runtimeApiLevel = (apiLevel == XABuildConfig.AndroidDefaultTargetDotnetApiLevel && apiLevel < XABuildConfig.AndroidLatestStableApiLevel) ? XABuildConfig.AndroidLatestStableApiLevel : apiLevel;
364363
versionString = runtimeApiLevel.Minor == 0 ? $"{runtimeApiLevel.Major}" : $"{runtimeApiLevel.Major}.{runtimeApiLevel.Minor}";
365-
var runtimeDirectory = Directory.GetDirectories (Path.Combine (TestEnvironment.DotNetPreviewPacksDirectory, $"Microsoft.Android.Runtime.{versionString}.android")).LastOrDefault ();
366-
var expectedMonoAndroidRuntimePath = Path.Combine (runtimeDirectory, "runtimes", "android", "lib", dotnetVersion, "Mono.Android.dll");
367-
Assert.IsTrue (dotnet.LastBuildOutput.ContainsText (expectedMonoAndroidRuntimePath), $"Build should be using {expectedMonoAndroidRuntimePath}");
364+
AssertUsingPack (buildOutput, $"Microsoft.Android.Runtime.{versionString}.android", "runtimes", "android", "lib", dotnetVersion, "Mono.Android.dll");
368365
}
369366

370367
var publishDirectory = Path.Combine (Root, projBuilder.ProjectDirectory, proj.OutputPath, runtimeIdentifier, "publish");
@@ -385,6 +382,17 @@ public void DotNetPublish ([Values] bool isRelease, [ValueSource (nameof(Get_Dot
385382
}
386383
}
387384

385+
static void AssertUsingPack (IEnumerable<string> buildOutput, string packName, params string [] assemblyPath)
386+
{
387+
var packDirectory = Path.Combine (TestEnvironment.DotNetPreviewPacksDirectory, packName);
388+
var assemblyPathSuffix = Path.Combine (assemblyPath);
389+
var assemblyFileName = Path.GetFileName (assemblyPathSuffix);
390+
Assert.That (Directory.EnumerateFiles (packDirectory, assemblyFileName, SearchOption.AllDirectories),
391+
Has.Exactly (1).Matches<string> (path =>
392+
path.EndsWith (assemblyPathSuffix, StringComparison.OrdinalIgnoreCase) && buildOutput.ContainsText (path)),
393+
$"Build should use exactly one installed '{packName}' pack.");
394+
}
395+
388396
[Test]
389397
[TestCaseSource (nameof (Get_DotNetTargetFrameworks_Data))]
390398
public void MauiTargetFramework (string dotnetVersion, string platform, Version apiLevel, AndroidRuntime runtime)

0 commit comments

Comments
 (0)