Skip to content

Commit

Permalink
Allow assemblies without extensions to be excluded (#6834) (#6838)
Browse files Browse the repository at this point in the history
* Restored previous behavior of only stripping .exe / .dll

* Allow assemblies without extensions to be excluded

---------

Co-authored-by: Ramon Smits <[email protected]>
  • Loading branch information
danielmarbach and ramonsmits authored Sep 5, 2023
1 parent 1ef06e3 commit f56864e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,22 @@ public void No_files_explicitly_excluded_are_returned()
Assert.That(explicitlySkippedDll, Is.Not.Null);
Assert.That(explicitlySkippedDll.SkipReason, Contains.Substring("File was explicitly excluded from scanning"));
}

[Test]
public void Assemblies_that_have_no_extension_can_be_excluded()
{
var results = new AssemblyScanner(Path.Combine(TestContext.CurrentContext.TestDirectory, "TestDlls"))
{
AssembliesToSkip = new List<string> { "some.random" },
ScanAppDomainAssemblies = false
}
.GetScannableAssemblies();

var skippedFiles = results.SkippedFiles;
var explicitlySkippedDll = skippedFiles.FirstOrDefault(s => s.FilePath.Contains("some.random.dll"));

Assert.That(explicitlySkippedDll, Is.Not.Null);
Assert.That(explicitlySkippedDll.SkipReason, Contains.Substring("File was explicitly excluded from scanning"));
}
}
}
1 change: 1 addition & 0 deletions src/NServiceBus.Core.Tests/TestDlls/some.random.dll
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is not a DLL
11 changes: 7 additions & 4 deletions src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ internal AssemblyScanner(Assembly assemblyToScan)

internal IReadOnlyCollection<string> AssembliesToSkip
{
set => assembliesToSkip = new HashSet<string>(value.Select(Path.GetFileNameWithoutExtension), StringComparer.OrdinalIgnoreCase);
set => assembliesToSkip = new HashSet<string>(value.Select(RemoveExtension), StringComparer.OrdinalIgnoreCase);
}

static string RemoveExtension(string assemblyName) =>
assemblyName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || assemblyName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)
? Path.GetFileNameWithoutExtension(assemblyName)
: assemblyName;

internal IReadOnlyCollection<Type> TypesToSkip
{
set => typesToSkip = new HashSet<Type>(value);
Expand Down Expand Up @@ -349,9 +354,7 @@ static List<FileInfo> ScanDirectoryForAssemblyFiles(string directoryToScan, bool
return fileInfo;
}

bool IsExcluded(string assemblyNameOrFileNameWithoutExtension) =>
assembliesToSkip.Contains(assemblyNameOrFileNameWithoutExtension) ||
DefaultAssemblyExclusions.Contains(assemblyNameOrFileNameWithoutExtension);
bool IsExcluded(string assemblyName) => assembliesToSkip.Contains(assemblyName) || DefaultAssemblyExclusions.Contains(assemblyName);

// The parameter and return types of this method are deliberately using the most concrete types
// to avoid unnecessary allocations
Expand Down

0 comments on commit f56864e

Please sign in to comment.