|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 3 | +using NSubstitute; |
| 4 | +using StabilityMatrix.Core.Helper; |
| 5 | +using StabilityMatrix.Core.Helper.Cache; |
| 6 | +using StabilityMatrix.Core.Models.Packages; |
| 7 | +using StabilityMatrix.Core.Python; |
| 8 | +using StabilityMatrix.Core.Services; |
| 9 | + |
| 10 | +namespace StabilityMatrix.Tests.Models.Packages; |
| 11 | + |
| 12 | +public static class PackageHelper |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Get all BasePackage implementations in the assembly. |
| 16 | + /// </summary> |
| 17 | + public static IEnumerable<BasePackage> GetPackages() |
| 18 | + { |
| 19 | + var services = new ServiceCollection(); |
| 20 | + services |
| 21 | + .AddSingleton(Substitute.For<IGithubApiCache>()) |
| 22 | + .AddSingleton(Substitute.For<ISettingsManager>()) |
| 23 | + .AddSingleton(Substitute.For<IDownloadService>()) |
| 24 | + .AddSingleton(Substitute.For<IPyRunner>()) |
| 25 | + .AddSingleton(Substitute.For<IPrerequisiteHelper>()); |
| 26 | + |
| 27 | + var assembly = typeof(BasePackage).Assembly; |
| 28 | + var packageTypes = assembly |
| 29 | + .GetTypes() |
| 30 | + .Where(t => t.IsSubclassOf(typeof(BasePackage)) && !t.IsAbstract) |
| 31 | + .Where(t => t != typeof(DankDiffusion) && t != typeof(UnknownPackage)) |
| 32 | + .ToList(); |
| 33 | + |
| 34 | + // Register all package types |
| 35 | + services.TryAddEnumerable( |
| 36 | + packageTypes.Select(t => ServiceDescriptor.Singleton(typeof(BasePackage), t)) |
| 37 | + ); |
| 38 | + |
| 39 | + var serviceProvider = services.BuildServiceProvider(); |
| 40 | + return serviceProvider.GetServices<BasePackage>(); |
| 41 | + } |
| 42 | +} |
0 commit comments