Skip to content

Commit ae8f67f

Browse files
committed
Use randomized ports in dotnet-serve
1 parent dd62a69 commit ae8f67f

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"dotnet-serve": {
6-
"version": "1.5.0",
6+
"version": "1.7.125",
77
"commands": [
88
"dotnet-serve"
99
]

src/ProjectTemplates/test/BlazorWasmTemplateTest.cs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Net;
@@ -58,7 +59,7 @@ public async Task BlazorWasmStandaloneTemplate_Works()
5859

5960
await BuildAndRunTest(project.ProjectName, project);
6061

61-
using var serveProcess = RunPublishedStandaloneBlazorProject(project);
62+
RunPublishedStandaloneBlazorProject(project);
6263
}
6364

6465
[Fact]
@@ -133,13 +134,7 @@ public async Task BlazorWasmStandalonePwaTemplate_Works()
133134

134135
ValidatePublishedServiceWorker(project);
135136

136-
using (var serverProcess = RunPublishedStandaloneBlazorProject(project))
137-
{
138-
// We want to use this form to ensure that it gets disposed right after the test.
139-
}
140-
141-
// Todo: Use dynamic port assignment: https://github.com/natemcmaster/dotnet-serve/pull/40/files
142-
var listeningUri = "https://localhost:8080";
137+
var listeningUri = RunPublishedStandaloneBlazorProject(project);
143138

144139
// The PWA template supports offline use. By now, the browser should have cached everything it needs,
145140
// so we can continue working even without the server.
@@ -326,7 +321,7 @@ public async Task BlazorWasmStandaloneTemplate_IndividualAuth_Works()
326321
// for that, we use the common microsoft tenant.
327322
await BuildAndRunTest(project.ProjectName, project, usesAuth: false);
328323

329-
using var serveProcess = RunPublishedStandaloneBlazorProject(project);
324+
RunPublishedStandaloneBlazorProject(project);
330325
}
331326

332327
public static TheoryData<TemplateInstance> TemplateData => new TheoryData<TemplateInstance>
@@ -551,20 +546,47 @@ private void UpdatePublishedSettings(Project serverProject)
551546
}
552547

553548

554-
private ProcessEx RunPublishedStandaloneBlazorProject(Project project)
549+
private string RunPublishedStandaloneBlazorProject(Project project)
555550
{
556551
var publishDir = Path.Combine(project.TemplatePublishDir, "wwwroot");
557552
AspNetProcess.EnsureDevelopmentCertificates();
558553

559554
Output.WriteLine("Running dotnet serve on published output...");
560-
var serveProcess = ProcessEx.Run(Output, publishDir, DotNetMuxer.MuxerPathOrDefault(), "serve -S");
555+
using (var serveProcess = ProcessEx.Run(Output, publishDir, DotNetMuxer.MuxerPathOrDefault(), "serve -S --port 0"))
556+
{
557+
var listeningUri = ResolveListeningUrl(serveProcess);
558+
Output.WriteLine($"Opening browser at {listeningUri}...");
559+
Browser.Navigate().GoToUrl(listeningUri);
561560

562-
// Todo: Use dynamic port assignment: https://github.com/natemcmaster/dotnet-serve/pull/40/files
563-
var listeningUri = "https://localhost:8080";
564-
Output.WriteLine($"Opening browser at {listeningUri}...");
565-
Browser.Navigate().GoToUrl(listeningUri);
566-
TestBasicNavigation(project.ProjectName);
567-
return serveProcess;
561+
TestBasicNavigation(project.ProjectName);
562+
563+
return listeningUri;
564+
}
565+
}
566+
567+
private static string ResolveListeningUrl(ProcessEx process)
568+
{
569+
var buffer = new List<string>();
570+
try
571+
{
572+
foreach (var line in process.OutputLinesAsEnumerable)
573+
{
574+
if (line != null)
575+
{
576+
buffer.Add(line);
577+
if (line.Trim().Contains("https://", StringComparison.Ordinal) || line.Trim().Contains("http://", StringComparison.Ordinal))
578+
{
579+
return line.Trim();
580+
}
581+
}
582+
}
583+
}
584+
catch (OperationCanceledException)
585+
{
586+
}
587+
588+
throw new InvalidOperationException(@$"Couldn't find listening url:
589+
{string.Join(Environment.NewLine, buffer.Append(process.Error))}");
568590
}
569591
}
570592
}

0 commit comments

Comments
 (0)