Skip to content

Commit a593bcf

Browse files
committed
Add NativeAOT test. Fixes #1265
1 parent 29d8e35 commit a593bcf

File tree

8 files changed

+94
-3
lines changed

8 files changed

+94
-3
lines changed

.github/workflows/ci.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ on:
55
branches: [ master, test ]
66

77
jobs:
8-
build:
9-
name: Build
8+
component-detection:
9+
name: Component Detection
1010
runs-on: ubuntu-latest
1111
permissions:
1212
contents: write
1313

1414
steps:
15-
- name: Checkout repository
15+
- name: Check out repository
1616
uses: actions/checkout@v4
1717

1818
- name: Set up .NET
@@ -26,3 +26,32 @@ jobs:
2626

2727
- name: Component detection
2828
uses: advanced-security/[email protected]
29+
30+
native-aot:
31+
name: Native AOT
32+
runs-on: ubuntu-latest
33+
strategy:
34+
matrix:
35+
tfm: [ 'net8.0', 'net9.0' ]
36+
services:
37+
mysql:
38+
image: mysql:9.1
39+
env:
40+
MYSQL_DATABASE: testdb
41+
MYSQL_ROOT_PASSWORD: pass
42+
ports:
43+
- 3306:3306
44+
# Before continuing, verify the mysql container is reachable from the ubuntu host
45+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
46+
steps:
47+
- name: Check out repository
48+
uses: actions/checkout@v4
49+
50+
- name: Set up .NET
51+
uses: actions/setup-dotnet@v4
52+
53+
- name: Publish
54+
run: dotnet publish -c Release -r linux-x64 -f ${{ matrix.tfm }} tests/MySqlConnector.NativeAot.Tests/MySqlConnector.NativeAot.Tests.csproj
55+
56+
- name: Test Native AOT
57+
run: ./artifacts/publish/MySqlConnector.NativeAot.Tests/release_${{ matrix.tfm }}_linux-x64/MySqlConnector.NativeAot.Tests

MySqlConnector.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.DependencyIn
2828
EndProject
2929
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.DependencyInjection.Tests", "tests\MySqlConnector.DependencyInjection.Tests\MySqlConnector.DependencyInjection.Tests.csproj", "{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}"
3030
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySqlConnector.NativeAot.Tests", "tests\MySqlConnector.NativeAot.Tests\MySqlConnector.NativeAot.Tests.csproj", "{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}"
32+
EndProject
3133
Global
3234
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3335
Debug|Any CPU = Debug|Any CPU
@@ -82,6 +84,10 @@ Global
8284
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Debug|Any CPU.Build.0 = Debug|Any CPU
8385
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Release|Any CPU.ActiveCfg = Release|Any CPU
8486
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Release|Any CPU.Build.0 = Release|Any CPU
87+
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
88+
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
89+
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
90+
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Release|Any CPU.Build.0 = Release|Any CPU
8591
EndGlobalSection
8692
GlobalSection(SolutionProperties) = preSolution
8793
HideSolutionNode = FALSE

src/MySqlConnector.DependencyInjection/MySqlConnector.DependencyInjection.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<Authors>Bradley Grainger</Authors>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
1313
<PackageTags>mysqlconnector;dependency injection;iservicecollection</PackageTags>
14+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
1415
</PropertyGroup>
1516

1617
<ItemGroup>

src/MySqlConnector/MySqlConnectionStringBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,9 @@ internal string GetConnectionString(bool includePassword)
871871
/// Fills in <paramref name="propertyDescriptors"/> with information about the available properties on this object.
872872
/// </summary>
873873
/// <param name="propertyDescriptors">The collection of <see cref="PropertyDescriptor"/> objects to populate.</param>
874+
#if NET6_0_OR_GREATER
875+
[RequiresUnreferencedCode("PropertyDescriptor's PropertyType cannot be statically discovered.")]
876+
#endif
874877
protected override void GetProperties(Hashtable propertyDescriptors)
875878
{
876879
base.GetProperties(propertyDescriptors);

src/MySqlConnector/MySqlConnector.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Nullable>enable</Nullable>
1414
<CheckEolTargetFramework>false</CheckEolTargetFramework>
1515
<ImplicitUsings>enable</ImplicitUsings>
16+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
1617
</PropertyGroup>
1718

1819
<ItemGroup>

src/MySqlConnector/MySqlDataReader.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections;
22
using System.Collections.ObjectModel;
33
using System.Diagnostics;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Globalization;
56
using MySqlConnector.Core;
67
using MySqlConnector.Logging;
@@ -245,6 +246,10 @@ public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int
245246
public override string GetDataTypeName(int ordinal) => GetResultSet().GetDataTypeName(ordinal);
246247

247248
public Type GetFieldType(string name) => GetFieldType(GetOrdinal(name));
249+
250+
#if NET6_0_OR_GREATER
251+
[UnconditionalSuppressMessage("ILLink", "IL2093", Justification = "This method is provided to implement the DbDataReader API. We do not want to retain all public methods of Types just used as sentinel values for field mapping.")]
252+
#endif
248253
public override Type GetFieldType(int ordinal) => GetResultSet().GetFieldType(ordinal);
249254

250255
public override object GetValue(int ordinal) => GetResultSet().GetCurrentRow().GetValue(ordinal);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
9+
<InvariantGlobalization>true</InvariantGlobalization>
10+
<StripSymbols>true</StripSymbols>
11+
<UseSystemResourceKeys>true</UseSystemResourceKeys>
12+
<IlcFoldIdenticalMethodBodies>true</IlcFoldIdenticalMethodBodies>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\src\MySqlConnector\MySqlConnector.csproj" />
17+
<ProjectReference Include="..\..\src\MySqlConnector.DependencyInjection\MySqlConnector.DependencyInjection.csproj" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using MySqlConnector;
2+
3+
var connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING") ?? "Server=localhost;Username=root;Password=pass";
4+
5+
await using var dataSource = new MySqlDataSourceBuilder(connectionString).Build();
6+
7+
await using var conn = dataSource.CreateConnection();
8+
await conn.OpenAsync();
9+
await using var cmd = new MySqlCommand("SELECT 'Hello World'", conn);
10+
await using var reader = await cmd.ExecuteReaderAsync();
11+
if (!await reader.ReadAsync())
12+
throw new Exception("ReadAsync returned false");
13+
14+
var value = reader.GetFieldValue<string>(0);
15+
if (value != "Hello World")
16+
throw new Exception($"Expected 'Hello World'; got '{value}'");
17+
18+
var schema = reader.GetColumnSchema();
19+
if (schema.Count != 1)
20+
throw new Exception($"Expected 1 column, got {schema.Count}");
21+
if (((MySqlDbColumn) schema[0]).ProviderType != MySqlDbType.VarChar)
22+
throw new Exception($"Expected column type to be MySqlDbType.VarChar, got {((MySqlDbColumn) schema[0]).ProviderType}");
23+
if (reader.GetFieldType(0) != typeof(string))
24+
throw new Exception($"Expected column type to be System.String, got {reader.GetFieldType(0)}");
25+
26+
Console.WriteLine("Success");

0 commit comments

Comments
 (0)