Skip to content

Commit 1c3ae15

Browse files
committed
Add publish test for BlazorStandalone
1 parent 62e2fb2 commit 1c3ae15

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

.config/dotnet-tools.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-serve": {
6+
"version": "1.5.0",
7+
"commands": [
8+
"dotnet-serve"
9+
]
10+
}
11+
}
12+
}

src/ProjectTemplates/test/BlazorWasmTemplateTest.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.AspNetCore.E2ETesting;
9-
using Microsoft.AspNetCore.Testing;
9+
using Microsoft.Extensions.CommandLineUtils;
1010
using OpenQA.Selenium;
1111
using Templates.Test.Helpers;
1212
using Xunit;
@@ -28,18 +28,30 @@ public BlazorWasmTemplateTest(ProjectFactoryFixture projectFactory, BrowserFixtu
2828
public async Task BlazorWasmStandaloneTemplate_Works()
2929
{
3030
var project = await ProjectFactory.GetOrCreateProject("blazorstandalone", Output);
31+
project.TargetFramework = "netstandard2.1";
3132

3233
var createResult = await project.RunDotNetNewAsync("blazorwasm");
3334
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", project, createResult));
3435

35-
// We can't run a published standalone app, but let's just make sure it publishes fine
3636
var publishResult = await project.RunDotNetPublishAsync();
3737
Assert.True(0 == publishResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", project, publishResult));
3838

3939
var buildResult = await project.RunDotNetBuildAsync();
4040
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", project, buildResult));
4141

4242
await BuildAndRunTest(project.ProjectName, project);
43+
44+
var publishDir = Path.Combine(project.TemplatePublishDir, project.ProjectName, "dist");
45+
AspNetProcess.EnsureDevelopmentCertificates();
46+
47+
Output.WriteLine("Running dotnet serve on published output...");
48+
using var serveProcess = ProcessEx.Run(Output, publishDir, DotNetMuxer.MuxerPathOrDefault(), "serve -S");
49+
50+
// Todo: Use dynamic port assignment: https://github.com/natemcmaster/dotnet-serve/pull/40/files
51+
var listeningUri = "https://localhost:8080";
52+
Output.WriteLine($"Opening browser at {listeningUri}...");
53+
Browser.Navigate().GoToUrl(listeningUri);
54+
TestBasicNavigation(project.ProjectName);
4355
}
4456

4557
[Fact]
@@ -70,7 +82,7 @@ public async Task BlazorWasmHostedTemplate_Works()
7082
if (BrowserFixture.IsHostAutomationSupported())
7183
{
7284
aspNetProcess.VisitInBrowser(Browser);
73-
TestBasicNavigation(project.ProjectName, serverProject);
85+
TestBasicNavigation(project.ProjectName);
7486
}
7587
else
7688
{
@@ -90,15 +102,15 @@ protected async Task BuildAndRunTest(string appName, Project project)
90102
if (BrowserFixture.IsHostAutomationSupported())
91103
{
92104
aspNetProcess.VisitInBrowser(Browser);
93-
TestBasicNavigation(appName, project);
105+
TestBasicNavigation(appName);
94106
}
95107
else
96108
{
97109
BrowserFixture.EnforceSupportedConfigurations();
98110
}
99111
}
100112

101-
private void TestBasicNavigation(string appName, Project project)
113+
private void TestBasicNavigation(string appName)
102114
{
103115
// Give components.server enough time to load so that it can replace
104116
// the prerendered content before we start making assertions.

src/ProjectTemplates/test/Helpers/AspNetProcess.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ public AspNetProcess(
5151
Timeout = TimeSpan.FromMinutes(2)
5252
};
5353

54-
var now = DateTimeOffset.Now;
55-
new CertificateManager().EnsureAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1));
54+
EnsureDevelopmentCertificates();
5655

5756
output.WriteLine("Running ASP.NET application...");
5857

@@ -64,6 +63,12 @@ public AspNetProcess(
6463
}
6564
}
6665

66+
internal static void EnsureDevelopmentCertificates()
67+
{
68+
var now = DateTimeOffset.Now;
69+
new CertificateManager().EnsureAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1));
70+
}
71+
6772
public void VisitInBrowser(IWebDriver driver)
6873
{
6974
_output.WriteLine($"Opening browser at {ListeningUri}...");

src/ProjectTemplates/test/Helpers/Project.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ public class Project
2121
{
2222
private const string _urls = "http://127.0.0.1:0;https://127.0.0.1:0";
2323

24-
public const string DefaultFramework = "netcoreapp3.1";
25-
2624
public static bool IsCIEnvironment => typeof(Project).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
2725
.Any(a => a.Key == "ContinuousIntegrationBuild");
2826

29-
public static string ArtifactsLogDir => typeof(Project).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
30-
.Single(a => a.Key == "ArtifactsLogDir")?.Value;
27+
public static string ArtifactsLogDir => GetAssemblyMetadata("ArtifactsLogDir");
3128

3229
public SemaphoreSlim DotNetNewLock { get; set; }
3330
public SemaphoreSlim NodeLock { get; set; }
3431
public string ProjectName { get; set; }
3532
public string ProjectArguments { get; set; }
3633
public string ProjectGuid { get; set; }
3734
public string TemplateOutputDir { get; set; }
38-
public string TemplateBuildDir => Path.Combine(TemplateOutputDir, "bin", "Debug", DefaultFramework);
39-
public string TemplatePublishDir => Path.Combine(TemplateOutputDir, "bin", "Release", DefaultFramework, "publish");
35+
public string TargetFramework { get; set; } = GetAssemblyMetadata("Test.DefaultTargetFramework");
36+
37+
public string TemplateBuildDir => Path.Combine(TemplateOutputDir, "bin", "Debug", TargetFramework);
38+
public string TemplatePublishDir => Path.Combine(TemplateOutputDir, "bin", "Release", TargetFramework, "publish");
4039

4140
private string TemplateServerDir => Path.Combine(TemplateOutputDir, $"{ProjectName}.Server");
4241
private string TemplateClientDir => Path.Combine(TemplateOutputDir, $"{ProjectName}.Client");
43-
public string TemplateClientDebugDir => Path.Combine(TemplateClientDir, "bin", "Debug", DefaultFramework);
44-
public string TemplateClientReleaseDir => Path.Combine(TemplateClientDir, "bin", "Release", DefaultFramework, "publish");
45-
public string TemplateServerReleaseDir => Path.Combine(TemplateServerDir, "bin", "Release", DefaultFramework, "publish");
42+
public string TemplateClientDebugDir => Path.Combine(TemplateClientDir, "bin", "Debug", TargetFramework);
43+
public string TemplateClientReleaseDir => Path.Combine(TemplateClientDir, "bin", "Release", TargetFramework, "publish");
44+
public string TemplateServerReleaseDir => Path.Combine(TemplateServerDir, "bin", "Release", TargetFramework, "publish");
4645

4746
public ITestOutputHelper Output { get; set; }
4847
public IMessageSink DiagnosticsMessageSink { get; set; }
@@ -110,7 +109,7 @@ internal async Task<ProcessEx> RunDotNetNewAsync(
110109
}
111110
}
112111

113-
internal async Task<ProcessEx> RunDotNetPublishAsync(bool takeNodeLock = false, IDictionary<string,string> packageOptions = null, string additionalArgs = null)
112+
internal async Task<ProcessEx> RunDotNetPublishAsync(bool takeNodeLock = false, IDictionary<string, string> packageOptions = null, string additionalArgs = null)
114113
{
115114
Output.WriteLine("Publishing ASP.NET application...");
116115

@@ -132,7 +131,7 @@ internal async Task<ProcessEx> RunDotNetPublishAsync(bool takeNodeLock = false,
132131
}
133132
}
134133

135-
internal async Task<ProcessEx> RunDotNetBuildAsync(bool takeNodeLock = false, IDictionary<string,string> packageOptions = null, string additionalArgs = null)
134+
internal async Task<ProcessEx> RunDotNetBuildAsync(bool takeNodeLock = false, IDictionary<string, string> packageOptions = null, string additionalArgs = null)
136135
{
137136
Output.WriteLine("Building ASP.NET application...");
138137

@@ -524,5 +523,18 @@ private void CaptureBinLogOnFailure(ProcessEx result)
524523
}
525524

526525
public override string ToString() => $"{ProjectName}: {TemplateOutputDir}";
526+
527+
private static string GetAssemblyMetadata(string key)
528+
{
529+
var attribute = typeof(Project).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
530+
.FirstOrDefault(a => a.Key == key);
531+
532+
if (attribute is null)
533+
{
534+
throw new ArgumentException($"AssemblyMetadataAttribute with key {key} was not found.");
535+
}
536+
537+
return attribute.Value;
538+
}
527539
}
528540
}

src/ProjectTemplates/test/ProjectTemplates.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
<_Parameter1>TestPackageRestorePath</_Parameter1>
5959
<_Parameter2>$(TestPackageRestorePath)</_Parameter2>
6060
</AssemblyAttribute>
61+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
62+
<_Parameter1>Test.DefaultTargetFramework</_Parameter1>
63+
<_Parameter2>$(DefaultNetCoreTargetFramework)</_Parameter2>
64+
</AssemblyAttribute>
6165
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute" Condition="'$(ContinuousIntegrationBuild)' == 'true'">
6266
<_Parameter1>ContinuousIntegrationBuild</_Parameter1>
6367
<_Parameter2>true</_Parameter2>

0 commit comments

Comments
 (0)