Skip to content

Commit 2bbb977

Browse files
authored
Add Native AOT test (#2324)
1 parent ca9b756 commit 2bbb977

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

test/FunctionalTests/Linker/Helpers/DotNetProcess.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -54,9 +54,10 @@ public DotNetProcess()
5454
public int ExitCode => Process.ExitCode;
5555
public bool HasExited => Process.HasExited;
5656

57-
public void Start(string arguments)
57+
public void Start(string fileName, string? arguments)
5858
{
59-
Process.StartInfo.Arguments = arguments;
59+
Process.StartInfo.FileName = fileName;
60+
Process.StartInfo.Arguments = arguments ?? string.Empty;
6061
Process.Start();
6162

6263
Process.BeginOutputReadLine();

test/FunctionalTests/Linker/LinkerTests.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@ public class LinkerTests
3333
{
3434
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(120);
3535

36+
#if NET8_0_OR_GREATER
3637
[Test]
37-
public async Task RunWebsiteAndCallWithClient_Success()
38+
public async Task RunWebsiteAndCallWithClient_Aot_Success()
39+
{
40+
await RunWebsiteAndCallWithClient(publishAot: true);
41+
}
42+
#endif
43+
44+
[Test]
45+
public async Task RunWebsiteAndCallWithClient_Trimming_Success()
46+
{
47+
await RunWebsiteAndCallWithClient(publishAot: false);
48+
}
49+
50+
private async Task RunWebsiteAndCallWithClient(bool publishAot)
3851
{
3952
var projectDirectory = typeof(LinkerTests).Assembly
4053
.GetCustomAttributes<AssemblyMetadataAttribute>()
@@ -51,13 +64,11 @@ public async Task RunWebsiteAndCallWithClient_Success()
5164
try
5265
{
5366
using var cts = new CancellationTokenSource();
54-
using var websiteProcess = new WebsiteProcess();
55-
using var clientProcess = new DotNetProcess();
5667

5768
try
5869
{
59-
var publishWebsiteTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", linkerTestsWebsitePath, cts.Token);
60-
var publishClientTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsClient\LinkerTestsClient.csproj", linkerTestsClientPath, cts.Token);
70+
var publishWebsiteTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", linkerTestsWebsitePath, publishAot, cts.Token);
71+
var publishClientTask = PublishAppAsync(projectDirectory + @"\..\..\testassets\LinkerTestsClient\LinkerTestsClient.csproj", linkerTestsClientPath, publishAot, cts.Token);
6172

6273
await Task.WhenAll(publishWebsiteTask, publishClientTask).TimeoutAfter(Timeout);
6374
Console.WriteLine("Successfully published app.");
@@ -67,12 +78,15 @@ public async Task RunWebsiteAndCallWithClient_Success()
6778
cts.Dispose();
6879
}
6980

81+
using var websiteProcess = new WebsiteProcess();
82+
using var clientProcess = new DotNetProcess();
83+
7084
try
7185
{
72-
websiteProcess.Start(Path.Combine(linkerTestsWebsitePath, "LinkerTestsWebsite.dll"));
86+
websiteProcess.Start(BuildStartPath(linkerTestsWebsitePath, "LinkerTestsWebsite"), arguments: null);
7387
await websiteProcess.WaitForReadyAsync().TimeoutAfter(Timeout);
7488

75-
clientProcess.Start(Path.Combine(linkerTestsClientPath, $"LinkerTestsClient.dll {websiteProcess.ServerPort}"));
89+
clientProcess.Start(BuildStartPath(linkerTestsClientPath, "LinkerTestsClient"), arguments: websiteProcess.ServerPort!.ToString());
7690
await clientProcess.WaitForExitAsync().TimeoutAfter(Timeout);
7791
}
7892
finally
@@ -92,6 +106,13 @@ public async Task RunWebsiteAndCallWithClient_Success()
92106
}
93107
}
94108

109+
private static string BuildStartPath(string path, string projectName)
110+
{
111+
// Executable on Windows has an *.exe extension.
112+
// We don't need to add it to the start path because *.exe is in the PATHEXT env var.
113+
return Path.Combine(path, projectName);
114+
}
115+
95116
private static void EnsureDeleted(string path)
96117
{
97118
if (Directory.Exists(path))
@@ -100,7 +121,7 @@ private static void EnsureDeleted(string path)
100121
}
101122
}
102123

103-
private static async Task PublishAppAsync(string path, string outputPath, CancellationToken cancellationToken)
124+
private static async Task PublishAppAsync(string path, string outputPath, bool publishAot, CancellationToken cancellationToken)
104125
{
105126
var resolvedPath = Path.GetFullPath(path);
106127
Console.WriteLine($"Publishing {resolvedPath}");
@@ -110,7 +131,9 @@ private static async Task PublishAppAsync(string path, string outputPath, Cancel
110131

111132
try
112133
{
113-
process.Start($"publish {resolvedPath} -r {GetRuntimeIdentifier()} -c Release -o {outputPath} --self-contained");
134+
// The AppPublishAot parameter is used to tell the compiler to publish as AOT.
135+
// AppPublishAot is used instead of PublishAot because dependency projects have non-AOT targets. Setting "PublishAot=true" causes build errors.
136+
process.Start("dotnet", $"publish {resolvedPath} -r {GetRuntimeIdentifier()} -c Release -o {outputPath} -p:AppPublishAot={publishAot} --self-contained");
114137
await process.WaitForExitAsync().TimeoutAfter(Timeout);
115138
}
116139
catch (Exception ex)

testassets/LinkerTestsClient/LinkerTestsClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<PublishTrimmed>true</PublishTrimmed>
7+
<PublishAot>$(AppPublishAot)</PublishAot>
78
</PropertyGroup>
89

910
<ItemGroup>

testassets/LinkerTestsWebsite/LinkerTestsWebsite.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<OutputType>Exe</OutputType>
66
<PublishTrimmed>true</PublishTrimmed>
7+
<PublishAot>$(AppPublishAot)</PublishAot>
78
<!--
89
ASP.NET Core SDK has partial trimming in .NET 7. Change to full because gRPC works with full
910
and it removes linker warnings from unused ASP.NET Core assemblies.

0 commit comments

Comments
 (0)