Skip to content

Commit 0b7484e

Browse files
authored
Test with trimming enabled in CI and in release builds. (#30822)
* Test with trimming enabeld in CI and in release builds.
1 parent fd75fcd commit 0b7484e

17 files changed

+207
-130
lines changed

src/Components/Shared/src/WebEventData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ private static EventArgs ParseEventArgsJson(Renderer renderer, JsonSerializerOpt
7979
}
8080
}
8181

82+
[DynamicDependency(JsonSerialized, typeof(DataTransfer))]
83+
[DynamicDependency(JsonSerialized, typeof(DataTransferItem))]
8284
private static bool TryDeserializeStandardWebEventArgs(string eventName, string eventArgsJson, [NotNullWhen(true)] out EventArgs? eventArgs)
8385
{
8486
// For back-compatibility, we recognize the built-in list of web event names and hard-code

src/Components/Web/src/Microsoft.AspNetCore.Components.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.E2ETesting;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Reflection;
14+
using DevHostServerProgram = Microsoft.AspNetCore.Components.WebAssembly.DevServer.Server.Program;
15+
16+
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
17+
{
18+
public class BlazorWasmTestAppFixture<TProgram> : WebHostServerFixture
19+
{
20+
public readonly bool TestTrimmedApps = typeof(ToggleExecutionModeServerFixture<>).Assembly
21+
.GetCustomAttributes<AssemblyMetadataAttribute>()
22+
.First(m => m.Key == "Microsoft.AspNetCore.E2ETesting.TestTrimmedApps")
23+
.Value == "true";
24+
25+
public string Environment { get; set; }
26+
public string PathBase { get; set; }
27+
public string ContentRoot { get; private set; }
28+
29+
protected override IHost CreateWebHost()
30+
{
31+
if (TestTrimmedApps)
32+
{
33+
var staticFilePath = Path.Combine(AppContext.BaseDirectory, "trimmed", typeof(TProgram).Assembly.GetName().Name);
34+
if (!Directory.Exists(staticFilePath))
35+
{
36+
throw new DirectoryNotFoundException($"Test is configured to use trimmed outputs, but trimmed outputs were not found in {staticFilePath}.");
37+
}
38+
39+
return CreateStaticWebHost(staticFilePath);
40+
}
41+
42+
ContentRoot = FindSampleOrTestSitePath(
43+
typeof(TProgram).Assembly.FullName);
44+
45+
var host = "127.0.0.1";
46+
if (E2ETestOptions.Instance.SauceTest)
47+
{
48+
host = E2ETestOptions.Instance.Sauce.HostName;
49+
}
50+
51+
var args = new List<string>
52+
{
53+
"--urls", $"http://{host}:0",
54+
"--contentroot", ContentRoot,
55+
"--pathbase", PathBase,
56+
"--applicationpath", typeof(TProgram).Assembly.Location,
57+
};
58+
59+
if (!string.IsNullOrEmpty(Environment))
60+
{
61+
args.Add("--environment");
62+
args.Add(Environment);
63+
}
64+
65+
return DevHostServerProgram.BuildWebHost(args.ToArray());
66+
}
67+
68+
private IHost CreateStaticWebHost(string contentRoot)
69+
{
70+
var host = "127.0.0.1";
71+
return new HostBuilder()
72+
.ConfigureWebHost(webHostBuilder => webHostBuilder
73+
.UseKestrel()
74+
.UseContentRoot(contentRoot)
75+
.UseStartup(_ => new StaticSiteStartup { PathBase = PathBase })
76+
.UseUrls($"http://{host}:0"))
77+
.Build();
78+
}
79+
80+
private class StaticSiteStartup
81+
{
82+
public string PathBase { get; init; }
83+
84+
public void ConfigureServices(IServiceCollection serviceCollection)
85+
{
86+
serviceCollection.AddRouting();
87+
}
88+
89+
public void Configure(IApplicationBuilder app)
90+
{
91+
app.UseBlazorFrameworkFiles();
92+
app.UseStaticFiles(new StaticFileOptions
93+
{
94+
ServeUnknownFileTypes = true,
95+
});
96+
97+
app.UseRouting();
98+
99+
app.UseEndpoints(endpoints =>
100+
{
101+
var fallback = "index.html";
102+
if (!string.IsNullOrEmpty(PathBase))
103+
{
104+
fallback = PathBase + '/' + fallback;
105+
}
106+
107+
endpoints.MapFallbackToFile(fallback);
108+
});
109+
}
110+
}
111+
}
112+
}

src/Components/test/E2ETest/Infrastructure/ServerFixtures/DevHostServerFixture.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Components/test/E2ETest/Infrastructure/ServerFixtures/StaticSiteServerFixture.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Components/test/E2ETest/Infrastructure/ServerFixtures/ToggleExecutionModeServerFixture.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using Microsoft.AspNetCore.Hosting;
76
using Microsoft.Extensions.Hosting;
87

98
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures
@@ -32,8 +31,7 @@ protected override string StartAndGetRootUri()
3231
{
3332
if (_buildWebHostMethod == null)
3433
{
35-
// Use Blazor's dev host server
36-
var underlying = new DevHostServerFixture<TClientProgram>();
34+
var underlying = new BlazorWasmTestAppFixture<TClientProgram>();
3735
underlying.PathBase = "/subdir";
3836
_serverToDispose = underlying;
3937
var uri = underlying.RootUri.AbsoluteUri; // As a side-effect, this starts the server

src/Components/test/E2ETest/Microsoft.AspNetCore.Components.E2ETests.csproj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
<!-- This project references the shared framework transitively. Prevent restore errors by setting this flag. -->
2323
<GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
24+
25+
<TestTrimmedApps Condition="'$(ContinuousIntegrationBuild)' == 'true'">true</TestTrimmedApps>
26+
<TestTrimmedApps Condition="'$(Configuration)' == 'Release'">true</TestTrimmedApps>
2427
</PropertyGroup>
2528

2629
<ItemGroup>
@@ -43,6 +46,29 @@
4346
<ProjectReference Include="..\testassets\GlobalizationWasmApp\GlobalizationWasmApp.csproj" />
4447
<ProjectReference Include="..\testassets\TestServer\Components.TestServer.csproj" />
4548
<ProjectReference Include="..\..\WebAssembly\testassets\Wasm.Authentication.Server\Wasm.Authentication.Server.csproj" />
49+
50+
<ProjectReference Include="..\..\benchmarkapps\Wasm.Performance\TestApp\Wasm.Performance.TestApp.csproj"
51+
Targets="Publish"
52+
Properties="TestTrimmedApps=true;PublishDir=$(MSBuildProjectDirectory)\$(OutputPath)trimmed\Wasm.Performance.TestApp\"
53+
Condition="'$(TestTrimmedApps)' == 'true'" />
54+
55+
<ProjectReference
56+
Include="..\testassets\BasicTestApp\BasicTestApp.csproj"
57+
Targets="Publish"
58+
Properties="TestTrimmedApps=true;PublishDir=$(MSBuildProjectDirectory)\$(OutputPath)trimmed\BasicTestApp\"
59+
Condition="'$(TestTrimmedApps)' == 'true'" />
60+
61+
<ProjectReference
62+
Include="..\testassets\GlobalizationWasmApp\GlobalizationWasmApp.csproj"
63+
Targets="Publish"
64+
Properties="TestTrimmedApps=true;PublishDir=$(MSBuildProjectDirectory)\$(OutputPath)trimmed\GlobalizationWasmApp\;"
65+
Condition="'$(TestTrimmedApps)' == 'true'" />
66+
67+
<ProjectReference
68+
Include="..\..\WebAssembly\testassets\StandaloneApp\StandaloneApp.csproj"
69+
Targets="Publish"
70+
Properties="TestTrimmedApps=true;PublishDir=$(MSBuildProjectDirectory)\$(OutputPath)trimmed\StandaloneApp\;"
71+
Condition="'$(TestTrimmedApps)' == 'true'" />
4672
</ItemGroup>
4773

4874
<!-- Shared testing infrastructure for running E2E tests using selenium -->
@@ -58,6 +84,13 @@
5884
<Compile Include="$(RepoRoot)src\Shared\Components\WebAssemblyComponentMarker.cs" />
5985
</ItemGroup>
6086

87+
<ItemGroup>
88+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
89+
<_Parameter1>Microsoft.AspNetCore.E2ETesting.TestTrimmedApps</_Parameter1>
90+
<_Parameter2>$(TestTrimmedApps)</_Parameter2>
91+
</AssemblyAttribute>
92+
</ItemGroup>
93+
6194
<ItemGroup>
6295
<None Update="xunit.runner.json">
6396
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

src/Components/test/E2ETest/Tests/BinaryHttpClientTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
1515
{
1616
public class BinaryHttpClientTest : BrowserTestBase,
1717
IClassFixture<BasicTestAppServerSiteFixture<CorsStartup>>,
18-
IClassFixture<DevHostServerFixture<BasicTestApp.Program>>
18+
IClassFixture<BlazorWasmTestAppFixture<BasicTestApp.Program>>
1919
{
20-
private readonly DevHostServerFixture<BasicTestApp.Program> _devHostServerFixture;
20+
private readonly BlazorWasmTestAppFixture<BasicTestApp.Program> _devHostServerFixture;
2121
readonly ServerFixture _apiServerFixture;
2222
IWebElement _appElement;
2323
IWebElement _responseStatus;
@@ -26,7 +26,7 @@ public class BinaryHttpClientTest : BrowserTestBase,
2626

2727
public BinaryHttpClientTest(
2828
BrowserFixture browserFixture,
29-
DevHostServerFixture<BasicTestApp.Program> devHostServerFixture,
29+
BlazorWasmTestAppFixture<BasicTestApp.Program> devHostServerFixture,
3030
BasicTestAppServerSiteFixture<CorsStartup> apiServerFixture,
3131
ITestOutputHelper output)
3232
: base(browserFixture, output)

src/Components/test/E2ETest/Tests/HttpClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
1818
{
19-
public class HttpClientTest : ServerTestBase<DevHostServerFixture<BasicTestApp.Program>>,
19+
public class HttpClientTest : ServerTestBase<BlazorWasmTestAppFixture<BasicTestApp.Program>>,
2020
IClassFixture<BasicTestAppServerSiteFixture<CorsStartup>>
2121
{
2222
private readonly ServerFixture _apiServerFixture;
@@ -27,7 +27,7 @@ public class HttpClientTest : ServerTestBase<DevHostServerFixture<BasicTestApp.P
2727

2828
public HttpClientTest(
2929
BrowserFixture browserFixture,
30-
DevHostServerFixture<BasicTestApp.Program> devHostServerFixture,
30+
BlazorWasmTestAppFixture<BasicTestApp.Program> devHostServerFixture,
3131
BasicTestAppServerSiteFixture<CorsStartup> apiServerFixture,
3232
ITestOutputHelper output)
3333
: base(browserFixture, devHostServerFixture, output)

src/Components/test/E2ETest/Tests/PerformanceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
1515
{
1616
public class PerformanceTest
17-
: ServerTestBase<DevHostServerFixture<Wasm.Performance.TestApp.Program>>
17+
: ServerTestBase<BlazorWasmTestAppFixture<Wasm.Performance.TestApp.Program>>
1818
{
1919
public PerformanceTest(
2020
BrowserFixture browserFixture,
21-
DevHostServerFixture<Wasm.Performance.TestApp.Program> serverFixture,
21+
BlazorWasmTestAppFixture<Wasm.Performance.TestApp.Program> serverFixture,
2222
ITestOutputHelper output)
2323
: base(browserFixture, serverFixture, output)
2424
{

src/Components/test/E2ETest/Tests/SaveStateTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Xunit;
1010
using Xunit.Abstractions;
1111

12-
namespace Microsoft.AspNetCore.Components.E2ETests.Tests
12+
namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
1313
{
1414
public class SaveStateTest : ServerTestBase<AspNetSiteServerFixture>
1515
{

src/Components/test/E2ETest/Tests/SignalRClientTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
1919
{
20-
public class SignalRClientTest : ServerTestBase<DevHostServerFixture<BasicTestApp.Program>>,
20+
public class SignalRClientTest : ServerTestBase<BlazorWasmTestAppFixture<BasicTestApp.Program>>,
2121
IClassFixture<BasicTestAppServerSiteFixture<CorsStartup>>
2222
{
2323
private readonly ServerFixture _apiServerFixture;
2424

2525
public SignalRClientTest(
2626
BrowserFixture browserFixture,
27-
DevHostServerFixture<BasicTestApp.Program> devHostServerFixture,
27+
BlazorWasmTestAppFixture<BasicTestApp.Program> devHostServerFixture,
2828
BasicTestAppServerSiteFixture<CorsStartup> apiServerFixture,
2929
ITestOutputHelper output)
3030
: base(browserFixture, devHostServerFixture, output)

src/Components/test/E2ETest/Tests/StandaloneAppTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
1515
{
1616
public class StandaloneAppTest
17-
: ServerTestBase<DevHostServerFixture<StandaloneApp.Program>>, IDisposable
17+
: ServerTestBase<BlazorWasmTestAppFixture<StandaloneApp.Program>>, IDisposable
1818
{
1919
public StandaloneAppTest(
2020
BrowserFixture browserFixture,
21-
DevHostServerFixture<StandaloneApp.Program> serverFixture,
21+
BlazorWasmTestAppFixture<StandaloneApp.Program> serverFixture,
2222
ITestOutputHelper output)
2323
: base(browserFixture, serverFixture, output)
2424
{

0 commit comments

Comments
 (0)