From c21e5a6b64494e19ba4ed279c63a5b259643aee9 Mon Sep 17 00:00:00 2001 From: Herman Ho Date: Thu, 21 Mar 2024 04:06:25 +0000 Subject: [PATCH] chore: fix test path --- .../TemplateServices/TemplateService.cs | 15 ++++++-- src/Postal.Tests/EmailViewRenderTests.cs | 2 ++ .../Controllers/Test1Controller.cs | 2 +- .../CustomWebApplicationFactory.cs | 3 +- .../Properties/launchSettings.json | 36 ++++++++++++------- 5 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Postal.AspNetCore/TemplateServices/TemplateService.cs b/src/Postal.AspNetCore/TemplateServices/TemplateService.cs index 958493c..82fad9b 100644 --- a/src/Postal.AspNetCore/TemplateServices/TemplateService.cs +++ b/src/Postal.AspNetCore/TemplateServices/TemplateService.cs @@ -17,6 +17,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; namespace Postal.AspNetCore @@ -125,13 +126,23 @@ public async Task RenderTemplateAsync(RouteData routeData, if ((viewResult == null || !viewResult.Success) && (razorPageResult == null || (razorPageResult != null && razorPageResult?.Page == null))) { + StringBuilder sb = new StringBuilder(); + sb.AppendLine($"Failed to render template {viewName} because it was not found."); var searchedLocations = viewResult?.SearchedLocations ?? []; if (razorPageResult != null && razorPageResult?.SearchedLocations != null) { searchedLocations = searchedLocations.Union(razorPageResult?.SearchedLocations!); } - _logger.LogError($"Failed to render template {viewName} because it was not found. \r\nThe following locations are searched: \r\n{string.Join("\r\n", searchedLocations)}"); - throw new TemplateServiceException($"Failed to render template {viewName} because it was not found. \r\nThe following locations are searched: \r\n{string.Join("\r\n", searchedLocations)}"); + if (_hostingEnvironment.ContentRootPath != null + && File.Exists(Path.Combine(_hostingEnvironment.ContentRootPath, viewName)) + && !IsApplicationRelativePath(viewName)) + { + sb.AppendLine($"ViewName requires \"~/\" prefix. Try change the viewName to \"~/{viewName.TrimStart('/')}\""); + } + sb.AppendLine("The following locations are searched:"); + sb.AppendLine(string.Join("\r\n", searchedLocations)); + _logger.LogError(sb.ToString()); + throw new TemplateServiceException(sb.ToString()); } var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); diff --git a/src/Postal.Tests/EmailViewRenderTests.cs b/src/Postal.Tests/EmailViewRenderTests.cs index 4a8341d..2c145ea 100644 --- a/src/Postal.Tests/EmailViewRenderTests.cs +++ b/src/Postal.Tests/EmailViewRenderTests.cs @@ -131,6 +131,7 @@ public async Task Render_throws_exception_when_email_view_not_found() var serviceProvider = new Mock(); var tempDataProvider = new Mock(); var hostingEnvironment = new Mock(); + hostingEnvironment.SetupGet(e => e.ContentRootPath).Returns(""); var diagnosticListener = new System.Diagnostics.DiagnosticListener("Postal.Tests"); var urlHelperFactory = new Mock(); var options = new Mock>(); @@ -165,6 +166,7 @@ public async Task Render_throws_exception_when_email_view_retrievepath_not_found var serviceProvider = new Mock(); var tempDataProvider = new Mock(); var hostingEnvironment = new Mock(); + hostingEnvironment.SetupGet(e => e.ContentRootPath).Returns(""); var diagnosticListener = new System.Diagnostics.DiagnosticListener("Postal.Tests"); var urlHelperFactory = new Mock(); var options = new Mock>(); diff --git a/test/Postal.Tests.Integration/Controllers/Test1Controller.cs b/test/Postal.Tests.Integration/Controllers/Test1Controller.cs index a06896a..2b915bb 100644 --- a/test/Postal.Tests.Integration/Controllers/Test1Controller.cs +++ b/test/Postal.Tests.Integration/Controllers/Test1Controller.cs @@ -20,7 +20,7 @@ public IActionResult SendEmail1() public IActionResult SendEmail2() { - var emailData = new Email("Pages/Emails/Testing2.cshtml"); + var emailData = new Email("~/Pages/Emails/Testing2.cshtml"); emailData.ViewData["to"] = "hello@example.com"; emailData.CaptureHttpContext(HttpContext); diff --git a/test/Postal.Tests.Integration/CustomWebApplicationFactory.cs b/test/Postal.Tests.Integration/CustomWebApplicationFactory.cs index 65199f5..27601ea 100644 --- a/test/Postal.Tests.Integration/CustomWebApplicationFactory.cs +++ b/test/Postal.Tests.Integration/CustomWebApplicationFactory.cs @@ -8,7 +8,6 @@ public class CustomWebApplicationFactory { protected override void ConfigureWebHost(IWebHostBuilder builder) { - //builder.UseContentRoot(AppContext.BaseDirectory); - builder.UseSolutionRelativeContentRoot("test/Postal.Tests.Integration"); + builder.UseSolutionRelativeContentRoot("test/Postal.Tests.Integration/"); } } diff --git a/test/Postal.Tests.Integration/Properties/launchSettings.json b/test/Postal.Tests.Integration/Properties/launchSettings.json index 80cf205..713afd6 100644 --- a/test/Postal.Tests.Integration/Properties/launchSettings.json +++ b/test/Postal.Tests.Integration/Properties/launchSettings.json @@ -1,21 +1,13 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:62985", - "sslPort": 0 - } - }, +{ "profiles": { "Postal.Tests.Integration": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5208", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5208" }, "IIS Express": { "commandName": "IISExpress", @@ -23,6 +15,24 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "WSL": { + "commandName": "WSL2", + "launchBrowser": true, + "launchUrl": "http://localhost:5208", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "ASPNETCORE_URLS": "http://localhost:5208" + }, + "distributionName": "" + } + }, + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:62985", + "sslPort": 0 } } -} +} \ No newline at end of file