|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.IO;
|
6 | 7 | using System.Linq;
|
7 | 8 | using System.Net;
|
@@ -58,7 +59,7 @@ public async Task BlazorWasmStandaloneTemplate_Works()
|
58 | 59 |
|
59 | 60 | await BuildAndRunTest(project.ProjectName, project);
|
60 | 61 |
|
61 |
| - using var serveProcess = RunPublishedStandaloneBlazorProject(project); |
| 62 | + RunPublishedStandaloneBlazorProject(project); |
62 | 63 | }
|
63 | 64 |
|
64 | 65 | [Fact]
|
@@ -133,13 +134,7 @@ public async Task BlazorWasmStandalonePwaTemplate_Works()
|
133 | 134 |
|
134 | 135 | ValidatePublishedServiceWorker(project);
|
135 | 136 |
|
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); |
143 | 138 |
|
144 | 139 | // The PWA template supports offline use. By now, the browser should have cached everything it needs,
|
145 | 140 | // so we can continue working even without the server.
|
@@ -326,7 +321,7 @@ public async Task BlazorWasmStandaloneTemplate_IndividualAuth_Works()
|
326 | 321 | // for that, we use the common microsoft tenant.
|
327 | 322 | await BuildAndRunTest(project.ProjectName, project, usesAuth: false);
|
328 | 323 |
|
329 |
| - using var serveProcess = RunPublishedStandaloneBlazorProject(project); |
| 324 | + RunPublishedStandaloneBlazorProject(project); |
330 | 325 | }
|
331 | 326 |
|
332 | 327 | public static TheoryData<TemplateInstance> TemplateData => new TheoryData<TemplateInstance>
|
@@ -551,20 +546,47 @@ private void UpdatePublishedSettings(Project serverProject)
|
551 | 546 | }
|
552 | 547 |
|
553 | 548 |
|
554 |
| - private ProcessEx RunPublishedStandaloneBlazorProject(Project project) |
| 549 | + private string RunPublishedStandaloneBlazorProject(Project project) |
555 | 550 | {
|
556 | 551 | var publishDir = Path.Combine(project.TemplatePublishDir, "wwwroot");
|
557 | 552 | AspNetProcess.EnsureDevelopmentCertificates();
|
558 | 553 |
|
559 | 554 | 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); |
561 | 560 |
|
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))}"); |
568 | 590 | }
|
569 | 591 | }
|
570 | 592 | }
|
0 commit comments