Skip to content

Commit

Permalink
Fix unresolvable jobs when DOTNET_ENVIRONMENT is set to Development d…
Browse files Browse the repository at this point in the history
…ue to ServiceProvider validation
  • Loading branch information
WillSoss committed Feb 5, 2024
1 parent 0f5b1f4 commit 2949ffb
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 44 deletions.
18 changes: 16 additions & 2 deletions Runly.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.5" />
<PackageReference Include="MinVer" Version="2.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="MinVer" Version="2.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions src/Runly/Runly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ben.Demystifier" Version="0.1.6" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.5" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.5" />
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="MinVer" Version="2.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NJsonSchema" Version="10.1.20" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NJsonSchema" Version="10.1.20" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20303.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 14 additions & 18 deletions src/Runly/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public static IServiceCollection AddRunlyJobs(this IServiceCollection services,
/// <returns>The same <see cref="IServiceCollection"/> passed in for chaining.</returns>
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;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public static IServiceCollection AddRunlyJobs(this IServiceCollection services,

ApplyCommandLineOverrides(config, result.Children);

services.AddRunAction(config);
services.AddRunAction(cache, config);
}
else
{
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -157,7 +149,7 @@ static CommandLineBuilder GetCommandLineBuilder(IServiceCollection services, Job
new Option<bool>(new[] { "--debug", "-d" }, "Optional. Prompts the user to attach a debugger when the job starts."),
new Option<bool>(new[] { "--silent", "-s" }, "Optional. Silences console output.")
};
run.Handler = CommandHandler.Create<FileInfo, FileInfo, bool, bool>((configPath, resultsPath, debug, silent) => services.AddRunAction(reader, configPath, resultsPath, debug, silent));
run.Handler = CommandHandler.Create<FileInfo, FileInfo, bool, bool>((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
Expand Down Expand Up @@ -286,7 +278,7 @@ static void AddGetAction(this IServiceCollection services, string type, string f
/// <param name="configPath">The location of the JSON config to run.</param>
/// <param name="debug">Indicates whether to attach a debugger.</param>
/// <param name="silent">Indicates whether to output results to the console.</param>
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 });
Expand All @@ -306,17 +298,17 @@ static void AddRunAction(this IServiceCollection services, ConfigReader reader,
if (silent)
config.Execution.ResultsToConsole = false;

services.AddRunAction(config);
services.AddRunAction(cache, config);
}

/// <summary>
/// Adds a transient <see cref="RunAction"/> to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The service collection being modified.</param>
/// <param name="config">The <see cref="Config"/> for the job.</param>
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<TextWriter>(s =>
{
Expand Down Expand Up @@ -373,9 +365,13 @@ static void AddRunAction(this IServiceCollection services, Config config)
/// <param name="services">The service collection being modified.</param>
/// <param name="config">The <see cref="Config"/> to add to the service collection.</param>
/// <returns>The same <see cref="IServiceCollection"/> passed in for chaining.</returns>
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);
Expand Down
5 changes: 5 additions & 0 deletions test/Runly.Tests.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Runly;

await JobHost.CreateDefaultBuilder(args)
.Build()
.RunJobAsync();
32 changes: 32 additions & 0 deletions test/Runly.Tests.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
14 changes: 14 additions & 0 deletions test/Runly.Tests.Cli/Runly.Tests.Cli.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Runly\Runly.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions test/Runly.Tests.Cli/TestJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Runly.Tests.Cli;
public class TestJob : Job<Config>
{
public TestJob(Config config)
: base(config) { }

public override Task<Result> ProcessAsync()
{
Console.WriteLine("TestJob is running");

return Task.FromResult(Result.Success());
}
}
10 changes: 5 additions & 5 deletions test/Runly.Tests/Runly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion test/Runly.Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public abstract class UnitTest : IAsyncLifetime, IDisposable
{
static UnitTest()
{
AssertionOptions.AssertEquivalencyUsing(opts => opts.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 2000)).WhenTypeIs<DateTime>());
AssertionOptions.AssertEquivalencyUsing(opts => opts.Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromMilliseconds(2000))).WhenTypeIs<DateTime>());
}

public UnitTest() { }
Expand Down

0 comments on commit 2949ffb

Please sign in to comment.