Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow assemblies without extensions to be excluded #6834

Merged
merged 2 commits into from
Sep 4, 2023
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 @@ -28,5 +28,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 @@ -55,9 +55,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 @@ -312,9 +317,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