diff --git a/docs/features/coding-style.md b/docs/features/coding-style.md index 3629572c..f30d5608 100644 --- a/docs/features/coding-style.md +++ b/docs/features/coding-style.md @@ -64,7 +64,7 @@ c => c.ObjectAsJson() ## Records are DTOs -Configures domain type records as valid input paramters. Methods containing +Configures domain type records as valid input parameters. Methods containing record parameters render as api endpoints. ```csharp diff --git a/src/core/Baked.Architecture/Testing/Nfr.cs b/src/core/Baked.Architecture/Testing/Nfr.cs index f4eb4c1e..e4c7c472 100644 --- a/src/core/Baked.Architecture/Testing/Nfr.cs +++ b/src/core/Baked.Architecture/Testing/Nfr.cs @@ -1,9 +1,17 @@ using NUnit.Framework; +using System.Reflection; namespace Baked.Testing; public abstract class Nfr { + public static Assembly? EntryAssembly { get; private set; } + + protected static void Init() where TEntryPoint : class + { + EntryAssembly = typeof(TEntryPoint).Assembly; + } + [OneTimeSetUp] public virtual Task OneTimeSetUp() => Task.CompletedTask; diff --git a/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreExtensions.cs b/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreExtensions.cs index cf999cc9..89311cc4 100644 --- a/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreExtensions.cs +++ b/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreExtensions.cs @@ -11,7 +11,6 @@ public static DotnetCoreFeature Dotnet(this CoreConfigurator _, Func? baseNamespace = default ) { - entryAssembly ??= Assembly.GetEntryAssembly() ?? throw new("'EntryAssembly' should have existed"); baseNamespace ??= assembly => Regexes.AssemblyNameBeforeApplicationSuffix().Match(assembly.FullName ?? string.Empty).Value; return new(entryAssembly, baseNamespace); diff --git a/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreFeature.cs b/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreFeature.cs index 47f79205..af020d25 100644 --- a/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreFeature.cs +++ b/src/recipe/Baked.Recipe.Service.Application/Core/Dotnet/DotnetCoreFeature.cs @@ -1,24 +1,27 @@ using Baked.Architecture; +using Baked.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using System.Reflection; namespace Baked.Core.Dotnet; -public class DotnetCoreFeature(Assembly _entryAssembly, Func _baseNamespace) +public class DotnetCoreFeature(Assembly? _entryAssembly, Func _baseNamespace) : IFeature { public void Configure(LayerConfigurator configurator) { configurator.ConfigureServiceCollection(services => { + var entryAssembly = _entryAssembly + ?? (configurator.IsNfr() ? Nfr.EntryAssembly : Assembly.GetEntryAssembly()) + ?? throw new("'EntryAssembly' should have existed"); + services.AddSingleton(TimeProvider.System); services.AddSingleton(); - services.AddFileProvider(new EmbeddedFileProvider(_entryAssembly, _baseNamespace(_entryAssembly))); - services.AddFileProvider(new PhysicalFileProvider(Path.GetDirectoryName(_entryAssembly.Location) ?? - throw new("'EntryAssembly' should have a not null location")) - ); + services.AddFileProvider(new EmbeddedFileProvider(entryAssembly, _baseNamespace(entryAssembly))); + services.AddFileProvider(new PhysicalFileProvider(Path.GetDirectoryName(entryAssembly.Location) ?? string.Empty)); }); } } \ No newline at end of file diff --git a/src/recipe/Baked.Recipe.Service.Application/Testing/WebApplicationNfr.cs b/src/recipe/Baked.Recipe.Service.Application/Testing/WebApplicationNfr.cs index 5c50e7d9..0dec3c95 100644 --- a/src/recipe/Baked.Recipe.Service.Application/Testing/WebApplicationNfr.cs +++ b/src/recipe/Baked.Recipe.Service.Application/Testing/WebApplicationNfr.cs @@ -15,8 +15,10 @@ public abstract class WebApplicationNfr : Nfr protected static IConfiguration Configuration => ServiceProvider.GetRequiredService(); - protected static void Init() where TEntryPoint : class + protected static new void Init() where TEntryPoint : class { + Nfr.Init(); + Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", nameof(Nfr)); var webApplicationFactory = new WebApplicationFactory(); diff --git a/test/recipe/Baked.Test.Recipe.Service.Application/Program.cs b/test/recipe/Baked.Test.Recipe.Service.Application/Program.cs index fd09ed42..d4dadf56 100644 --- a/test/recipe/Baked.Test.Recipe.Service.Application/Program.cs +++ b/test/recipe/Baked.Test.Recipe.Service.Application/Program.cs @@ -1,5 +1,4 @@ using Baked.Test.Orm; -using System.Reflection; Bake.New .Service( @@ -28,9 +27,7 @@ claims: ["User", "Admin", "BaseA", "BaseB", "GivenA", "GivenB", "GivenC"], baseClaims: ["BaseA", "BaseB"] ), - core: c => c - .Dotnet(baseNamespace: _ => "Baked.Test") - .ForNfr(c.Dotnet(entryAssembly: Assembly.GetExecutingAssembly(), baseNamespace: _ => "Baked.Test")), + core: c => c.Dotnet(baseNamespace: _ => "Baked.Test"), cors: c => c.AspNetCore(Settings.Required("CorsOrigin")), database: c => c .Sqlite() diff --git a/test/recipe/Baked.Test.Recipe.Service.Test/Runtime/ReadingResources.cs b/test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResources.cs similarity index 97% rename from test/recipe/Baked.Test.Recipe.Service.Test/Runtime/ReadingResources.cs rename to test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResources.cs index 7eb16da2..2af5b296 100644 --- a/test/recipe/Baked.Test.Recipe.Service.Test/Runtime/ReadingResources.cs +++ b/test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResources.cs @@ -1,6 +1,6 @@ using System.Net.Http.Json; -namespace Baked.Test.Runtime; +namespace Baked.Test.Core; public class ReadingResources : TestServiceNfr { diff --git a/test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResourcesDuringTests.cs b/test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResourcesDuringTests.cs new file mode 100644 index 00000000..cf378077 --- /dev/null +++ b/test/recipe/Baked.Test.Recipe.Service.Test/Core/ReadingResourcesDuringTests.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.FileProviders; + +namespace Baked.Test.Core; + +public class ReadingResourcesDuringTests : TestServiceSpec +{ + [TestCase("Core/DomainEmbedded.txt")] + public void Specs_includes_business_resources(string subpath) + { + var provider = GiveMe.The(); + + var exists = provider.Exists(subpath); + + exists.ShouldBeTrue(); + } + + [TestCase("Core/ApplicationEmbedded.txt")] + [TestCase("Core/ApplicationPhysical.txt")] + public void Specs_excludes_application_resources(string subpath) + { + var provider = GiveMe.The(); + + var exists = provider.Exists(subpath); + + exists.ShouldBeFalse(); + } +} \ No newline at end of file