Skip to content

Commit

Permalink
Revert "setup the infra for test"
Browse files Browse the repository at this point in the history
This reverts commit 63fd1a9.
  • Loading branch information
DeagleGross committed Jul 21, 2024
1 parent 00b7025 commit acff97e
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 256 deletions.
2 changes: 1 addition & 1 deletion src/Dapper.AOT.Analyzers/InGeneration/DapperHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#if !DAPPERAOT_INTERNAL
file
#endif
static partial class DbStringHelpers
static class DbStringHelpers
{
public static void ConfigureDbStringDbParameter(
global::System.Data.Common.DbParameter dbParameter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\src\Dapper.AOT.Analyzers\AotGridReader.cs" Link="AotGridReader.cs" />
<Compile Remove="Accessors/Data/**/*.*.cs" />
<None Include="Accessors/Data/**/*.*" />
<None Update="Accessors/Data/**/*.*" CopyToOutputDirectory="PreserveNewest" />

<Compile Remove="Interceptors/**/*.*.cs" />
<None Include="Interceptors/**/*.*" />
<None Update="Interceptors/**/*.*" CopyToOutputDirectory="PreserveNewest" />

<Compile Remove="InterceptionExecutables/**/*.*.cs" />
<None Include="InterceptionExecutables/**/*.*" />
<None Update="InterceptionExecutables/**/*.*" CopyToOutputDirectory="PreserveNewest" />

<!-- <Compile Remove="InterceptionExecutables\Template.cs" />-->
<!-- <None Include="InterceptionExecutables\_Template.cs" />-->
<!-- <Compile Update="InterceptionExecutables\**\*.cs">-->
<!-- <CopyToOutputDirectory>Always</CopyToOutputDirectory>-->
<!-- </Compile>-->
<Compile Include="..\..\src\Dapper.AOT.Analyzers\AotGridReader.cs" Link="AotGridReader.cs" />
<Compile Include="..\..\src\Dapper.AOT.Analyzers\InGeneration\DapperHelpers.cs" Link="DapperHelpers.cs" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -50,7 +49,6 @@
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Analyzer.Testing.XUnit" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" />
<PackageReference Include="Testcontainers.PostgreSql" />
<ProjectReference Include="..\Dapper.AOT.Test\Dapper.AOT.Test.csproj" />
</ItemGroup>

</Project>
24 changes: 11 additions & 13 deletions test/Dapper.AOT.Test.Integration/DbStringTests.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
using System.Data;
using System.Threading.Tasks;
using System.Linq;
using Dapper.AOT.Test.Integration.Setup;
using Xunit;
using Xunit.Abstractions;

namespace Dapper.AOT.Test.Integration;

[Collection(SharedPostgresqlClient.Collection)]
public class DbStringTests : InterceptedCodeExecutionTestsBase
public class DbStringTests
{
public DbStringTests(PostgresqlFixture fixture, ITestOutputHelper log) : base(fixture, log)
private PostgresqlFixture _fixture;

public DbStringTests(PostgresqlFixture fixture)
{
Fixture.NpgsqlConnection.Execute("""
_fixture = fixture;
fixture.NpgsqlConnection.Execute("""
CREATE TABLE IF NOT EXISTS dbStringTable(
id integer PRIMARY KEY,
name varchar(40) NOT NULL CHECK (name <> '')
);
TRUNCATE dbStringTable;
""");
"""
);
}

[Fact]
[DapperAot]
public async Task Test()
public void ExecuteMulti()
{
var sourceCode = PrepareSourceCodeFromFile("DbString");
var executionResults = BuildAndExecuteInterceptedUserCode<int>(sourceCode, methodName: "ExecuteAsync");

// TODO DO THE CHECK HERE

}
}

This file was deleted.

This file was deleted.

This file was deleted.

25 changes: 8 additions & 17 deletions test/Dapper.AOT.Test/GeneratorTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,13 @@ protected GeneratorTestBase(ITestOutputHelper? log)
protected static string? GetOriginCodeLocation([CallerFilePath] string? path = null) => path;

// input from https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#unit-testing-of-generators

protected (Compilation? Compilation, GeneratorDriverRunResult Result, ImmutableArray<Diagnostic> Diagnostics, int ErrorCount) Execute<T>(string source,
StringBuilder? diagnosticsTo = null,
[CallerMemberName] string? name = null,
string? fileName = null,
Action<T>? initializer = null
) where T : class, IIncrementalGenerator, new()
{
// Create the 'input' compilation that the generator will act on
if (string.IsNullOrWhiteSpace(name)) name = "compilation";
if (string.IsNullOrWhiteSpace(fileName)) fileName = "input.cs";
var inputCompilation = RoslynTestHelpers.CreateCompilation(source, name!, fileName!);

return Execute(inputCompilation, diagnosticsTo, initializer);
}

protected (Compilation? Compilation, GeneratorDriverRunResult Result, ImmutableArray<Diagnostic> Diagnostics, int ErrorCount) Execute<T>(
Compilation inputCompilation,
StringBuilder? diagnosticsTo = null,
Action<T>? initializer = null
) where T : class, IIncrementalGenerator, new()
{
void OutputDiagnostic(Diagnostic d)
{
Expand All @@ -68,21 +54,26 @@ void Output(string message, bool force = false)
diagnosticsTo?.AppendLine(message.Replace('\\', '/')); // need to normalize paths
}
}
// Create the 'input' compilation that the generator will act on
if (string.IsNullOrWhiteSpace(name)) name = "compilation";
if (string.IsNullOrWhiteSpace(fileName)) fileName = "input.cs";
Compilation inputCompilation = RoslynTestHelpers.CreateCompilation(source, name!, fileName!);

// directly create an instance of the generator
// (Note: in the compiler this is loaded from an assembly, and created via reflection at runtime)
T generator = new();
initializer?.Invoke(generator);

ShowDiagnostics("Input code", inputCompilation, diagnosticsTo, "CS8795", "CS1701", "CS1702");

// Create the driver that will control the generation, passing in our generator
GeneratorDriver driver = CSharpGeneratorDriver.Create(new[] { generator.AsSourceGenerator() }, parseOptions: RoslynTestHelpers.ParseOptionsLatestLangVer);

// Run the generation pass
// (Note: the generator driver itself is immutable, and all calls return an updated version of the driver that you should use for subsequent calls)
driver = driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out var outputCompilation, out var diagnostics);
var runResult = driver.GetRunResult();

foreach (var result in runResult.Results)
{
if (result.Exception is not null) throw result.Exception;
Expand Down
39 changes: 3 additions & 36 deletions test/Dapper.AOT.Test/TestCommon/RoslynTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Dapper.TestCommon;

public static class RoslynTestHelpers
internal static class RoslynTestHelpers
{
internal static readonly CSharpParseOptions ParseOptionsLatestLangVer = CSharpParseOptions.Default
.WithLanguageVersion(LanguageVersion.Latest)
Expand Down Expand Up @@ -45,41 +45,8 @@ public static class RoslynTestHelpers
})
.WithFeatures(new[] { DapperInterceptorGenerator.FeatureKeys.InterceptorsPreviewNamespacePair });

public static Compilation CreateCompilation(string assemblyName, SyntaxTree[] syntaxTrees, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary)
=> CSharpCompilation.Create(assemblyName,
syntaxTrees: syntaxTrees,
references: new[] {
MetadataReference.CreateFromFile(typeof(Binder).Assembly.Location),
#if !NET48
MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Data").Location),
MetadataReference.CreateFromFile(Assembly.Load("netstandard").Location),
MetadataReference.CreateFromFile(Assembly.Load("System.Collections").Location),
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.Schema.ColumnAttribute).Assembly.Location),
#endif
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DbConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Data.SqlClient.SqlConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Microsoft.Data.SqlClient.SqlConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(OracleConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ValueTask<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Component).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DapperAotExtensions).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SqlMapper).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ImmutableList<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(ImmutableArray<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IAsyncEnumerable<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Span<int>).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IgnoreDataMemberAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SqlMapper).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DynamicAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(IValidatableObject).Assembly.Location),
},
options: new CSharpCompilationOptions(outputKind, allowUnsafe: true));

public static Compilation CreateCompilation(string source, string assemblyName, string fileName)
=> CSharpCompilation.Create(assemblyName,
public static Compilation CreateCompilation(string source, string name, string fileName)
=> CSharpCompilation.Create(name,
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, ParseOptionsLatestLangVer).WithFilePath(fileName) },
references: new[] {
MetadataReference.CreateFromFile(typeof(Binder).Assembly.Location),
Expand Down

0 comments on commit acff97e

Please sign in to comment.