From 2a2f8f7d0a5292003a1d0351b48a77f7f787b8da Mon Sep 17 00:00:00 2001 From: Zach Hollis <43797367+zhollis21@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:44:34 -0500 Subject: [PATCH 1/2] Let the v3 engine run tests on Android Two desktop assumptions stopped a Debug `dotnet test --device` run on an Android emulator before any test ran (#1966): - The extension scan walks every parent folder of the engine, and Android denies listing some of them, such as /data/user/0. DirectoryFinder now treats a folder it may not list as having no matches. - TestAssemblyResolver constructs an AssemblyDependencyResolver, which throws PlatformNotSupportedException on mobile runtimes. There is no deps.json to read there, so the resolver now goes without it and relies on its other strategies. --- .../Internal/DirectoryFinderTests.cs | 29 +++++++++++++++++++ .../Internal/DirectoryFinder.cs | 22 +++++++++++--- .../Internal/TestAssemblyResolver.cs | 12 ++++++-- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/NUnitEngine/nunit.engine.core.tests/Internal/DirectoryFinderTests.cs b/src/NUnitEngine/nunit.engine.core.tests/Internal/DirectoryFinderTests.cs index 6ce73b26b..a89f61ec8 100644 --- a/src/NUnitEngine/nunit.engine.core.tests/Internal/DirectoryFinderTests.cs +++ b/src/NUnitEngine/nunit.engine.core.tests/Internal/DirectoryFinderTests.cs @@ -436,6 +436,35 @@ public void GetDirectories_ParentThenParentThenWordThenWord() this.GetFakeDirectory("tools", "metamorphosator").Received().GetDirectories("addins", SIO.SearchOption.TopDirectoryOnly); } + [Test] + public void GetDirectories_Word_SkipsDirectoryThatCannotBeListed() + { + // On Android the extension scan walks up through folders such as /data/user/0, + // which the app may not list. Those folders should add no matches, not fail the scan. + var finder = new DirectoryFinder(this.fileSystem); + var baseDir = this.GetFakeDirectory("tools"); + this.GetFakeDirectory("tools", "frobuscator").GetDirectories("tests", SIO.SearchOption.TopDirectoryOnly).Returns(_ => throw new System.UnauthorizedAccessException()); + this.GetFakeDirectory("tools", "metamorphosator").GetDirectories("tests", SIO.SearchOption.TopDirectoryOnly).Returns(new[] { this.GetFakeDirectory("tools", "metamorphosator", "tests") }); + var expected = new[] { this.GetFakeDirectory("tools", "metamorphosator", "tests") }; + + var actual = finder.GetDirectories(baseDir, "*/tests"); + + Assert.That(actual, Is.EquivalentTo(expected)); + } + + [Test] + public void GetDirectories_Greedy_SkipsDirectoryThatCannotBeListed() + { + var finder = new DirectoryFinder(this.fileSystem); + var baseDir = this.GetFakeDirectory("tools", "frobuscator"); + baseDir.GetDirectories("*", SIO.SearchOption.AllDirectories).Returns(_ => throw new System.UnauthorizedAccessException()); + var expected = new[] { baseDir }; + + var actual = finder.GetDirectories(baseDir, "**"); + + Assert.That(actual, Is.EquivalentTo(expected)); + } + [Test] public void GetDirectories_StartDirectoryIsNull() { diff --git a/src/NUnitEngine/nunit.engine.core/Internal/DirectoryFinder.cs b/src/NUnitEngine/nunit.engine.core/Internal/DirectoryFinder.cs index f21dc197d..dedb7560a 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/DirectoryFinder.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/DirectoryFinder.cs @@ -2,8 +2,10 @@ using NUnit.Common; using NUnit.Engine.Internal.FileSystemAccess; +using System; using System.Collections.Generic; using System.IO; +using System.Linq; namespace NUnit.Engine.Internal { @@ -102,17 +104,29 @@ private static List ExpandOneStep(IList dirList, string // ** means zero or more intervening directories, so we // add the directory itself to start out. newList.Add(dir); - var subDirs = dir.GetDirectories("*", SearchOption.AllDirectories); - if (subDirs.Any()) newList.AddRange(subDirs); + newList.AddRange(GetSubDirectories(dir, "*", SearchOption.AllDirectories)); } else { - var subDirs = dir.GetDirectories(pattern, SearchOption.TopDirectoryOnly); - if (subDirs.Any()) newList.AddRange(subDirs); + newList.AddRange(GetSubDirectories(dir, pattern, SearchOption.TopDirectoryOnly)); } } return newList; } + + // A directory we may not list, such as /data/user/0 above an Android app's own + // folder, has nothing we can use, so it contributes no matches rather than an error. + private static IEnumerable GetSubDirectories(IDirectory dir, string pattern, SearchOption searchOption) + { + try + { + return dir.GetDirectories(pattern, searchOption); + } + catch (UnauthorizedAccessException) + { + return Enumerable.Empty(); + } + } } } diff --git a/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs b/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs index a824099a2..da9ff04f5 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs @@ -32,7 +32,15 @@ public TestAssemblyResolver(AssemblyLoadContext loadContext, string testAssembly { _loadContext = loadContext; _basePath = Path.GetDirectoryName(testAssemblyPath); - _assemblyDependencyResolver = new AssemblyDependencyResolver(testAssemblyPath); + // Mobile runtimes (Android, iOS) do not support AssemblyDependencyResolver. There is + // no deps.json to read there, so the other resolution strategies are all we need. + try + { + _assemblyDependencyResolver = new AssemblyDependencyResolver(testAssemblyPath); + } + catch (PlatformNotSupportedException) + { + } #if NET8_0_OR_GREATER AppContext.SetData("APP_CONTEXT_BASE_DIRECTORY", _basePath); #endif @@ -84,7 +92,7 @@ public void Dispose() private Assembly OnResolving(AssemblyLoadContext loadContext, AssemblyName assemblyName) { - var runtimeResolverPath = _assemblyDependencyResolver.ResolveAssemblyToPath(assemblyName); + var runtimeResolverPath = _assemblyDependencyResolver?.ResolveAssemblyToPath(assemblyName); if (!string.IsNullOrEmpty(runtimeResolverPath) && File.Exists(runtimeResolverPath)) { var loadedAssembly = _loadContext.LoadFromAssemblyPath(runtimeResolverPath); From e9437863bcedce8438b0bf028f84d3868df0efac Mon Sep 17 00:00:00 2001 From: Zach Hollis <43797367+zhollis21@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:50:53 -0500 Subject: [PATCH 2/2] Skip the extension scan when the host assembly has no location In an Android Release build the engine is packed inside the APK, so its Assembly.Location is empty. FindExtensionAssemblies passed Path.GetDirectoryName("") (null) to Path.Combine, which threw ArgumentNullException before any test could run (#1966). An assembly that is not a file has no folders around it to search, so the scan now returns early. --- .../Services/ExtensionManagerTests.cs | 14 ++++++++++++++ .../nunit.engine.core/Services/ExtensionManager.cs | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/src/NUnitEngine/nunit.engine.core.tests/Services/ExtensionManagerTests.cs b/src/NUnitEngine/nunit.engine.core.tests/Services/ExtensionManagerTests.cs index 375c755da..2cc6ac218 100644 --- a/src/NUnitEngine/nunit.engine.core.tests/Services/ExtensionManagerTests.cs +++ b/src/NUnitEngine/nunit.engine.core.tests/Services/ExtensionManagerTests.cs @@ -151,6 +151,20 @@ public void DisabledExtensionMayBeEnabled() .And.Property(nameof(ExtensionNode.Enabled)).True); } + [Test] + public void FindExtensionAssemblies_HostAssemblyHasNoLocation() + { + // On Android, Release builds pack assemblies inside the APK, so the engine + // has no Location. An assembly loaded from bytes is the same on desktop. + var bytes = File.ReadAllBytes(Path.Combine(THIS_ASSEMBLY_DIRECTORY, "mock-assembly.dll")); + var hostAssembly = Assembly.Load(bytes); + Assume.That(hostAssembly.Location, Is.Empty); + + var manager = new ExtensionManager(); + + Assert.That(() => manager.FindExtensionAssemblies(hostAssembly), Throws.Nothing); + } + [Test] public void SkipsGracefullyLoadingOtherFrameworkExtensionAssembly() { diff --git a/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs b/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs index db1eea36d..8b236ad06 100644 --- a/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs +++ b/src/NUnitEngine/nunit.engine.core/Services/ExtensionManager.cs @@ -165,6 +165,14 @@ public void FindExtensionAssemblies(Assembly hostAssembly) { log.Info($"FindExtensionAssemblies called for host {hostAssembly.FullName}"); + // An assembly that was not loaded from a file, such as one packed inside an + // Android APK, has no folders around it to search for extensions. + if (string.IsNullOrEmpty(hostAssembly.Location)) + { + log.Info(" Host assembly has no location, so there are no extension directories to search"); + return; + } + bool isChocolateyPackage = System.IO.File.Exists(Path.Combine(Path.GetDirectoryName(hostAssembly.Location)!, "VERIFICATION.txt")); string[] extensionPatterns = isChocolateyPackage ? new[] { "nunit-extension-*/**/tools/", "nunit-extension-*/**/tools/*/" }