|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Linq; |
| 4 | +using System.Reflection; |
3 | 5 | using DotNetWorkQueue.Dashboard.Api; |
4 | 6 | using DotNetWorkQueue.Dashboard.Api.Configuration; |
5 | 7 | using DotNetWorkQueue.Dashboard.Api.Services; |
| 8 | +using FluentAssertions; |
6 | 9 | using Microsoft.Extensions.DependencyInjection; |
7 | 10 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
8 | 11 |
|
@@ -153,5 +156,85 @@ public void AddDotNetWorkQueueDashboard_Does_Not_Register_ConsumerPruningService |
153 | 156 | Assert.AreEqual(0, pruningServiceRegistrations.Count, |
154 | 157 | "ConsumerPruningService should not be registered when tracking is disabled"); |
155 | 158 | } |
| 159 | + |
| 160 | + [TestMethod] |
| 161 | + public void AddDotNetWorkQueueDashboard_PreloadsAssemblies_From_AssemblyPaths() |
| 162 | + { |
| 163 | + // Copy a known DLL to a temp "plugin" directory |
| 164 | + var pluginDir = Path.Combine(Path.GetTempPath(), "dnwq-preload-test-" + Guid.NewGuid().ToString("N")); |
| 165 | + Directory.CreateDirectory(pluginDir); |
| 166 | + try |
| 167 | + { |
| 168 | + // Use Newtonsoft.Json as a test DLL — it's in our bin but let's verify |
| 169 | + // the preload path works by copying it and checking it loads from there |
| 170 | + var sourceDll = Path.Combine(AppContext.BaseDirectory, "FluentAssertions.dll"); |
| 171 | + var destDll = Path.Combine(pluginDir, "FluentAssertions.dll"); |
| 172 | + File.Copy(sourceDll, destDll); |
| 173 | + |
| 174 | + var services = new ServiceCollection(); |
| 175 | + services.AddLogging(); |
| 176 | + |
| 177 | + services.AddDotNetWorkQueueDashboard(options => |
| 178 | + { |
| 179 | + options.EnableSwagger = false; |
| 180 | + options.AssemblyPaths = new[] { pluginDir }; |
| 181 | + }); |
| 182 | + |
| 183 | + // If PreloadAssemblies threw, we wouldn't get here |
| 184 | + var provider = services.BuildServiceProvider(); |
| 185 | + var opts = provider.GetRequiredService<DashboardOptions>(); |
| 186 | + opts.AssemblyPaths.Should().ContainSingle().Which.Should().Be(pluginDir); |
| 187 | + } |
| 188 | + finally |
| 189 | + { |
| 190 | + Directory.Delete(pluginDir, true); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + [TestMethod] |
| 195 | + public void AddDotNetWorkQueueDashboard_PreloadAssemblies_Ignores_NonexistentDir() |
| 196 | + { |
| 197 | + var services = new ServiceCollection(); |
| 198 | + services.AddLogging(); |
| 199 | + |
| 200 | + // Should not throw even if the directory doesn't exist |
| 201 | + services.AddDotNetWorkQueueDashboard(options => |
| 202 | + { |
| 203 | + options.EnableSwagger = false; |
| 204 | + options.AssemblyPaths = new[] { "/nonexistent/path/that/does/not/exist" }; |
| 205 | + }); |
| 206 | + |
| 207 | + var provider = services.BuildServiceProvider(); |
| 208 | + provider.GetRequiredService<DashboardOptions>().Should().NotBeNull(); |
| 209 | + } |
| 210 | + |
| 211 | + [TestMethod] |
| 212 | + public void AddDotNetWorkQueueDashboard_PreloadAssemblies_Ignores_InvalidDlls() |
| 213 | + { |
| 214 | + var pluginDir = Path.Combine(Path.GetTempPath(), "dnwq-preload-invalid-" + Guid.NewGuid().ToString("N")); |
| 215 | + Directory.CreateDirectory(pluginDir); |
| 216 | + try |
| 217 | + { |
| 218 | + // Write a non-.NET file with .dll extension |
| 219 | + File.WriteAllText(Path.Combine(pluginDir, "NotADotNet.dll"), "this is not a valid dll"); |
| 220 | + |
| 221 | + var services = new ServiceCollection(); |
| 222 | + services.AddLogging(); |
| 223 | + |
| 224 | + // Should not throw on invalid DLLs |
| 225 | + services.AddDotNetWorkQueueDashboard(options => |
| 226 | + { |
| 227 | + options.EnableSwagger = false; |
| 228 | + options.AssemblyPaths = new[] { pluginDir }; |
| 229 | + }); |
| 230 | + |
| 231 | + var provider = services.BuildServiceProvider(); |
| 232 | + provider.GetRequiredService<DashboardOptions>().Should().NotBeNull(); |
| 233 | + } |
| 234 | + finally |
| 235 | + { |
| 236 | + Directory.Delete(pluginDir, true); |
| 237 | + } |
| 238 | + } |
156 | 239 | } |
157 | 240 | } |
0 commit comments