From 2949ffbcdfc191985be963ee5cba2133a4b7fc2d Mon Sep 17 00:00:00 2001 From: William Sossamon <3278433+WillSoss@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:08:12 -0600 Subject: [PATCH] Fix unresolvable jobs when DOTNET_ENVIRONMENT is set to Development due to ServiceProvider validation --- Runly.sln | 18 +++++++++-- src/Client/Client.csproj | 14 ++++---- src/Runly/Runly.csproj | 22 ++++++------- src/Runly/ServiceExtensions.cs | 32 ++++++++----------- test/Runly.Tests.Cli/Program.cs | 5 +++ .../Properties/launchSettings.json | 32 +++++++++++++++++++ test/Runly.Tests.Cli/Runly.Tests.Cli.csproj | 14 ++++++++ test/Runly.Tests.Cli/TestJob.cs | 13 ++++++++ test/Runly.Tests/Runly.Tests.csproj | 10 +++--- test/Runly.Tests/UnitTest.cs | 2 +- 10 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 test/Runly.Tests.Cli/Program.cs create mode 100644 test/Runly.Tests.Cli/Properties/launchSettings.json create mode 100644 test/Runly.Tests.Cli/Runly.Tests.Cli.csproj create mode 100644 test/Runly.Tests.Cli/TestJob.cs diff --git a/Runly.sln b/Runly.sln index af3cbee..5131557 100644 --- a/Runly.sln +++ b/Runly.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29519.181 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "src\Client\Client.csproj", "{6A5344CC-7B78-4330-9EE9-E5C96EE975E4}" EndProject @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Diagnostics", "src\Diagnost EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Runly", "src\Runly\Runly.csproj", "{F74B45FF-BFFB-425C-9CF4-C509402E545E}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runly.Tests.Cli", "test\Runly.Tests.Cli\Runly.Tests.Cli.csproj", "{CAC30475-7E45-43E2-9775-14E6E31C837B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -83,6 +85,18 @@ Global {F74B45FF-BFFB-425C-9CF4-C509402E545E}.Release|x64.Build.0 = Release|Any CPU {F74B45FF-BFFB-425C-9CF4-C509402E545E}.Release|x86.ActiveCfg = Release|Any CPU {F74B45FF-BFFB-425C-9CF4-C509402E545E}.Release|x86.Build.0 = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|x64.ActiveCfg = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|x64.Build.0 = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|x86.ActiveCfg = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Debug|x86.Build.0 = Debug|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|Any CPU.Build.0 = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|x64.ActiveCfg = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|x64.Build.0 = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|x86.ActiveCfg = Release|Any CPU + {CAC30475-7E45-43E2-9775-14E6E31C837B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Client/Client.csproj b/src/Client/Client.csproj index b5882d2..4c40230 100644 --- a/src/Client/Client.csproj +++ b/src/Client/Client.csproj @@ -26,13 +26,13 @@ - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/src/Runly/Runly.csproj b/src/Runly/Runly.csproj index 5221578..f93eb80 100644 --- a/src/Runly/Runly.csproj +++ b/src/Runly/Runly.csproj @@ -28,22 +28,22 @@ - - - - - - - - + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - + + - + diff --git a/src/Runly/ServiceExtensions.cs b/src/Runly/ServiceExtensions.cs index f1afb2e..4237abd 100644 --- a/src/Runly/ServiceExtensions.cs +++ b/src/Runly/ServiceExtensions.cs @@ -45,9 +45,9 @@ public static IServiceCollection AddRunlyJobs(this IServiceCollection services, /// The same passed in for chaining. public static IServiceCollection AddRunlyJobs(this IServiceCollection services, Config config, params Assembly[] jobAssemblies) { - services.AddJobCache(jobAssemblies); + (var cache, _) = services.AddJobCache(jobAssemblies); - services.AddRunAction(config); + services.AddRunAction(cache, config); return services; } @@ -89,7 +89,7 @@ public static IServiceCollection AddRunlyJobs(this IServiceCollection services, ApplyCommandLineOverrides(config, result.Children); - services.AddRunAction(config); + services.AddRunAction(cache, config); } else { @@ -118,14 +118,6 @@ public static IServiceCollection AddRunlyJobs(this IServiceCollection services, services.AddSingleton(cache); services.AddSingleton(cfgReader); - foreach (var job in cache.Jobs) - { - if (job.IsValid) - { - services.AddTransient(job.JobType); - } - } - return (cache, cfgReader); } @@ -157,7 +149,7 @@ static CommandLineBuilder GetCommandLineBuilder(IServiceCollection services, Job new Option(new[] { "--debug", "-d" }, "Optional. Prompts the user to attach a debugger when the job starts."), new Option(new[] { "--silent", "-s" }, "Optional. Silences console output.") }; - run.Handler = CommandHandler.Create((configPath, resultsPath, debug, silent) => services.AddRunAction(reader, configPath, resultsPath, debug, silent)); + run.Handler = CommandHandler.Create((configPath, resultsPath, debug, silent) => services.AddRunAction(cache, reader, configPath, resultsPath, debug, silent)); root.AddCommand(run); // Add a command for each job, with each config path as an option @@ -286,7 +278,7 @@ static void AddGetAction(this IServiceCollection services, string type, string f /// The location of the JSON config to run. /// Indicates whether to attach a debugger. /// Indicates whether to output results to the console. - static void AddRunAction(this IServiceCollection services, ConfigReader reader, FileInfo configPath, FileInfo resultsPath,bool debug, bool silent) + static void AddRunAction(this IServiceCollection services, JobCache cache, ConfigReader reader, FileInfo configPath, FileInfo resultsPath,bool debug, bool silent) { if (debug) services.AddSingleton(new Debug() { AttachDebugger = true }); @@ -306,7 +298,7 @@ static void AddRunAction(this IServiceCollection services, ConfigReader reader, if (silent) config.Execution.ResultsToConsole = false; - services.AddRunAction(config); + services.AddRunAction(cache, config); } /// @@ -314,9 +306,9 @@ static void AddRunAction(this IServiceCollection services, ConfigReader reader, /// /// The service collection being modified. /// The for the job. - static void AddRunAction(this IServiceCollection services, Config config) + static void AddRunAction(this IServiceCollection services, JobCache cache, Config config) { - services.AddConfig(config); + services.AddJob(cache, config); services.AddSingleton(s => { @@ -373,9 +365,13 @@ static void AddRunAction(this IServiceCollection services, Config config) /// The service collection being modified. /// The to add to the service collection. /// The same passed in for chaining. - public static IServiceCollection AddConfig(this IServiceCollection services, Config config) + public static IServiceCollection AddJob(this IServiceCollection services, JobCache cache, Config config) { - var type = config.GetType(); + var info = cache.Get(config.Job.Type); + + services.AddTransient(info.JobType); + + var type = config.GetType(); foreach (var intf in type.GetInterfaces()) services.AddSingleton(intf, config); diff --git a/test/Runly.Tests.Cli/Program.cs b/test/Runly.Tests.Cli/Program.cs new file mode 100644 index 0000000..e1e0f99 --- /dev/null +++ b/test/Runly.Tests.Cli/Program.cs @@ -0,0 +1,5 @@ +using Runly; + +await JobHost.CreateDefaultBuilder(args) + .Build() + .RunJobAsync(); \ No newline at end of file diff --git a/test/Runly.Tests.Cli/Properties/launchSettings.json b/test/Runly.Tests.Cli/Properties/launchSettings.json new file mode 100644 index 0000000..6bcb1c2 --- /dev/null +++ b/test/Runly.Tests.Cli/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "profiles": { + "WSL": { + "commandName": "WSL2", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + }, + "distributionName": "" + }, + "List": { + "commandName": "Project", + "commandLineArgs": "list", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + }, + "Run TestJob": { + "commandName": "Project", + "commandLineArgs": "TestJob", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + }, + "Get TestJob": { + "commandName": "Project", + "commandLineArgs": "list", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/test/Runly.Tests.Cli/Runly.Tests.Cli.csproj b/test/Runly.Tests.Cli/Runly.Tests.Cli.csproj new file mode 100644 index 0000000..1c59745 --- /dev/null +++ b/test/Runly.Tests.Cli/Runly.Tests.Cli.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/test/Runly.Tests.Cli/TestJob.cs b/test/Runly.Tests.Cli/TestJob.cs new file mode 100644 index 0000000..c49ea18 --- /dev/null +++ b/test/Runly.Tests.Cli/TestJob.cs @@ -0,0 +1,13 @@ +namespace Runly.Tests.Cli; +public class TestJob : Job +{ + public TestJob(Config config) + : base(config) { } + + public override Task ProcessAsync() + { + Console.WriteLine("TestJob is running"); + + return Task.FromResult(Result.Success()); + } +} diff --git a/test/Runly.Tests/Runly.Tests.csproj b/test/Runly.Tests/Runly.Tests.csproj index 9b6ff9a..e307a39 100644 --- a/test/Runly.Tests/Runly.Tests.csproj +++ b/test/Runly.Tests/Runly.Tests.csproj @@ -10,11 +10,11 @@ - - - - - + + + + + all runtime; build; native; contentfiles; analyzers diff --git a/test/Runly.Tests/UnitTest.cs b/test/Runly.Tests/UnitTest.cs index e79a337..b14fbf3 100644 --- a/test/Runly.Tests/UnitTest.cs +++ b/test/Runly.Tests/UnitTest.cs @@ -9,7 +9,7 @@ public abstract class UnitTest : IAsyncLifetime, IDisposable { static UnitTest() { - AssertionOptions.AssertEquivalencyUsing(opts => opts.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 2000)).WhenTypeIs()); + AssertionOptions.AssertEquivalencyUsing(opts => opts.Using(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromMilliseconds(2000))).WhenTypeIs()); } public UnitTest() { }