Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
22 changes: 18 additions & 4 deletions src/NUnitEngine/nunit.engine.core/Internal/DirectoryFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -102,17 +104,29 @@ private static List<IDirectory> ExpandOneStep(IList<IDirectory> 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<IDirectory> GetSubDirectories(IDirectory dir, string pattern, SearchOption searchOption)
{
try
{
return dir.GetDirectories(pattern, searchOption);
}
catch (UnauthorizedAccessException)
{
return Enumerable.Empty<IDirectory>();
}
}
}
}
12 changes: 10 additions & 2 deletions src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/*/" }
Expand Down
Loading