Skip to content

Commit

Permalink
Add NativeAOT test. Fixes #1265
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Grainger <[email protected]>
  • Loading branch information
bgrainger committed Nov 3, 2024
1 parent 29d8e35 commit ae8b17e
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 3 deletions.
35 changes: 32 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ on:
branches: [ master, test ]

jobs:
build:
name: Build
component-detection:
name: Component Detection
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout repository
- name: Check out repository
uses: actions/checkout@v4

- name: Set up .NET
Expand All @@ -26,3 +26,32 @@ jobs:

- name: Component detection
uses: advanced-security/[email protected]

native-aot:
name: Native AOT
runs-on: ubuntu-latest
strategy:
matrix:
tfm: [ 'net8.0', 'net9.0' ]
services:
mysql:
image: mysql:9.1
env:
MYSQL_DATABASE: testdb
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
# Before continuing, verify the mysql container is reachable from the ubuntu host
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up .NET
uses: actions/setup-dotnet@v4

- name: Publish
run: dotnet publish -c Release -r linux-x64 -f ${{ matrix.tfm }} tests/MySqlConnector.NativeAot.Tests/MySqlConnector.NativeAot.Tests.csproj

- name: Run
run: ./artifacts/publish/MySqlConnector.NativeAot.Tests/release_${{ matrix.tfm }}_linux-x64/MySqlConnector.NativeAot.Tests
6 changes: 6 additions & 0 deletions MySqlConnector.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.DependencyIn
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.DependencyInjection.Tests", "tests\MySqlConnector.DependencyInjection.Tests\MySqlConnector.DependencyInjection.Tests.csproj", "{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MySqlConnector.NativeAot.Tests", "tests\MySqlConnector.NativeAot.Tests\MySqlConnector.NativeAot.Tests.csproj", "{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,6 +84,10 @@ Global
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E41AD8B7-2F67-444F-A8DC-51C3C8B1FD16}.Release|Any CPU.Build.0 = Release|Any CPU
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3A1E4DF-A5CC-4447-A0FC-C6971218EEDE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<Authors>Bradley Grainger</Authors>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>mysqlconnector;dependency injection;iservicecollection</PackageTags>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/MySqlConnector/MySqlConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ internal string GetConnectionString(bool includePassword)
/// Fills in <paramref name="propertyDescriptors"/> with information about the available properties on this object.
/// </summary>
/// <param name="propertyDescriptors">The collection of <see cref="PropertyDescriptor"/> objects to populate.</param>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("PropertyDescriptor's PropertyType cannot be statically discovered.")]
#endif
protected override void GetProperties(Hashtable propertyDescriptors)
{
base.GetProperties(propertyDescriptors);
Expand Down
1 change: 1 addition & 0 deletions src/MySqlConnector/MySqlConnector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Nullable>enable</Nullable>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/MySqlConnector/MySqlDataReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using MySqlConnector.Core;
using MySqlConnector.Logging;
Expand Down Expand Up @@ -245,6 +246,10 @@ public override long GetChars(int ordinal, long dataOffset, char[]? buffer, int
public override string GetDataTypeName(int ordinal) => GetResultSet().GetDataTypeName(ordinal);

public Type GetFieldType(string name) => GetFieldType(GetOrdinal(name));

#if NET6_0_OR_GREATER
[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.")]
#endif
public override Type GetFieldType(int ordinal) => GetResultSet().GetFieldType(ordinal);

public override object GetValue(int ordinal) => GetResultSet().GetCurrentRow().GetValue(ordinal);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
<StripSymbols>true</StripSymbols>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<IlcFoldIdenticalMethodBodies>true</IlcFoldIdenticalMethodBodies>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MySqlConnector\MySqlConnector.csproj" />
<ProjectReference Include="..\..\src\MySqlConnector.DependencyInjection\MySqlConnector.DependencyInjection.csproj" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions tests/MySqlConnector.NativeAot.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using MySqlConnector;

var connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING") ?? "Server=localhost;Username=root;Password=pass";

await using var dataSource = new MySqlDataSourceBuilder(connectionString).Build();

await using var conn = dataSource.CreateConnection();
await conn.OpenAsync();
await using var cmd = new MySqlCommand("SELECT 'Hello World'", conn);
await using var reader = await cmd.ExecuteReaderAsync();
if (!await reader.ReadAsync())
throw new Exception("ReadAsync returned false");

var value = reader.GetFieldValue<string>(0);
if (value != "Hello World")
throw new Exception($"Expected 'Hello World'; got '{value}'");

var schema = reader.GetColumnSchema();
if (schema.Count != 1)
throw new Exception($"Expected 1 column, got {schema.Count}");
if (((MySqlDbColumn) schema[0]).ProviderType != MySqlDbType.VarChar)
throw new Exception($"Expected column type to be MySqlDbType.VarChar, got {((MySqlDbColumn) schema[0]).ProviderType}");
if (reader.GetFieldType(0) != typeof(string))
throw new Exception($"Expected column type to be System.String, got {reader.GetFieldType(0)}");

0 comments on commit ae8b17e

Please sign in to comment.