Skip to content

Commit

Permalink
Make unit tests for DllImportGenerator build/run in CI (#60598)
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Oct 20, 2021
1 parent ee4a76d commit 3eecfcd
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Interop.Ancillary
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DllImportGenerator.Tests", "tests\DllImportGenerator.Tests\DllImportGenerator.Tests.csproj", "{57A1A6FD-9231-4DFB-8619-F0EDEDA208E3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DllImportGenerator.UnitTests", "tests\DllImportGenerator.UnitTests\DllImportGenerator.UnitTests.csproj", "{4B516949-4AD4-44D6-AF86-C2E6058608D5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DllImportGenerator.Unit.Tests", "tests\DllImportGenerator.UnitTests\DllImportGenerator.Unit.Tests.csproj", "{4B516949-4AD4-44D6-AF86-C2E6058608D5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Runtime.InteropServices.ComDisabled.Tests", "tests\System.Runtime.InteropServices.ComDisabled.UnitTests\System.Runtime.InteropServices.ComDisabled.Tests.csproj", "{25D66424-2EAF-464D-8460-10C04EDEF3C3}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AssemblyName>Microsoft.Interop.Ancillary</AssemblyName>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TargetFramework>$(NetCoreAppMinimum)</TargetFramework>
<RootNamespace>System.Runtime.InteropServices</RootNamespace>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace DllImportGenerator.UnitTests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/60650", TestRuntimes.Mono)]
public class ConvertToGeneratedDllImportAnalyzerTests
{
public static IEnumerable<object[]> MarshallingRequiredTypes() => new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace DllImportGenerator.UnitTests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/60650", TestRuntimes.Mono)]
public class ConvertToGeneratedDllImportFixerTests
{
[Theory]
Expand All @@ -29,7 +30,7 @@ partial class Test
public static extern int [|Method|](out int ret);
}}";
// Fixed source will have CS8795 (Partial method must have an implementation) without generator run
string fixedSource = usePreprocessorDefines
string fixedSource = usePreprocessorDefines
? @$"
using System.Runtime.InteropServices;
partial class Test
Expand All @@ -41,7 +42,7 @@ partial class Test
[DllImport(""DoesNotExist"")]
public static extern int Method(out int ret);
#endif
}}"
}}"
: @$"
using System.Runtime.InteropServices;
partial class Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(MicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="$(CompilerPlatformTestingVersion)" />
<PackageReference Include="coverlet.collector" Version="$(CoverletCollectorVersion)">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace DllImportGenerator.UnitTests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/60650", TestRuntimes.Mono)]
public class GeneratedDllImportAnalyzerTests
{
[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace DllImportGenerator.UnitTests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/60650", TestRuntimes.Mono)]
public class ManualTypeMarshallingAnalyzerTests
{
public static IEnumerable<object[]> NonBlittableTypeMarkedBlittable_ReportsDiagnostic_TestData {
Expand Down Expand Up @@ -421,7 +422,6 @@ await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(GetPinnableReferenceReturnTypeBlittableRule).WithLocation(0));
}


[Fact]
public async Task BlittableGetPinnableReferenceReturnType_DoesNotReportDiagnostic()
{
Expand Down Expand Up @@ -518,7 +518,7 @@ public Native(S s) : this()

await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task TypeWithGetPinnableReferenceByRefReturnType_ReportsDiagnostic()
{
Expand Down Expand Up @@ -852,7 +852,7 @@ struct Native

await VerifyCS.VerifyAnalyzerAsync(source);
}

[Fact]
public async Task NativeTypeWithOnlyStackallocConstructor_ReportsDiagnostic()
{
Expand Down Expand Up @@ -897,7 +897,7 @@ struct {|#1:Native|}
public Native(S s, Span<byte> buffer) {}
public IntPtr Value => IntPtr.Zero;
public const int StackBufferSize = 0x100;
}";

Expand Down Expand Up @@ -953,7 +953,7 @@ struct Native
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(ValuePropertyMustHaveSetterRule).WithLocation(0).WithArguments("Native"));
}

[Fact]
public async Task BlittableNativeTypeOnMarshalUsingParameter_DoesNotReportDiagnostic()
{
Expand Down Expand Up @@ -1142,7 +1142,6 @@ await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(NativeTypeMustBeBlittableRule).WithLocation(0).WithArguments("Native", "S"));
}


[Fact]
public async Task GenericNativeTypeWithValueTypeValueProperty_DoesNotReportDiagnostic()
{
Expand Down Expand Up @@ -1358,7 +1357,6 @@ unsafe struct S
[Fact]
public async Task BlittableGenericTypeInBlittableType_DoesNotReportDiagnostic()
{

var source = @"
using System.Runtime.InteropServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static async Task<Compilation> CreateCompilation(SyntaxTree[] sources, Ou

return CSharpCompilation.Create("compilation",
sources,
(await mdRefs.ResolveAsync(LanguageNames.CSharp, CancellationToken.None)).Add(ancillary),
(await ResolveReferenceAssemblies(mdRefs)).Add(ancillary),
new CSharpCompilationOptions(outputKind, allowUnsafe: allowUnsafe));
}

Expand Down Expand Up @@ -112,19 +112,13 @@ public static async Task<Compilation> CreateCompilationWithReferenceAssemblies(S
{
return CSharpCompilation.Create("compilation",
sources,
(await referenceAssemblies.ResolveAsync(LanguageNames.CSharp, CancellationToken.None)),
await ResolveReferenceAssemblies(referenceAssemblies),
new CSharpCompilationOptions(outputKind, allowUnsafe: allowUnsafe));
}

public static (ReferenceAssemblies, MetadataReference) GetReferenceAssemblies()
{
// TODO: When .NET 6.0 releases, we can simplify this.
var referenceAssemblies = new ReferenceAssemblies(
"net6.0",
new PackageIdentity(
"Microsoft.NETCore.App.Ref",
"6.0.0-preview.7.21377.19"),
Path.Combine("ref", "net6.0"))
var referenceAssemblies = ReferenceAssemblies.Net.Net60
.WithNuGetConfigFilePath(Path.Combine(Path.GetDirectoryName(typeof(TestUtils).Assembly.Location)!, "NuGet.config"));

// Include the assembly containing the new attribute and all of its references.
Expand Down Expand Up @@ -165,5 +159,20 @@ public static GeneratorDriver CreateDriver(Compilation c, AnalyzerConfigOptionsP
ImmutableArray.Create(generators.Select(gen => gen.AsSourceGenerator()).ToArray()),
parseOptions: (CSharpParseOptions)c.SyntaxTrees.First().Options,
optionsProvider: options);

private static async Task<ImmutableArray<MetadataReference>> ResolveReferenceAssemblies(ReferenceAssemblies referenceAssemblies)
{
const string envVar = "NUGET_PACKAGES";
try
{
// Set the NuGet package cache location to a subdirectory such that we should always have access to it
Environment.SetEnvironmentVariable(envVar, Path.Combine(Path.GetDirectoryName(typeof(TestUtils).Assembly.Location)!, "packages"));
return await referenceAssemblies.ResolveAsync(LanguageNames.CSharp, CancellationToken.None);
}
finally
{
Environment.SetEnvironmentVariable(envVar, null);
}
}
}
}
1 change: 1 addition & 0 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Net.WebSockets.Client\tests\wasm\System.Net.WebSockets.Client.Wasm.Tests.csproj" />

<!-- https://github.com/dotnet/runtime/issues/58226 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices\tests\DllImportGenerator.UnitTests\DllImportGenerator.Unit.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn3.11.Unit.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Text.Json\tests\System.Text.Json.SourceGeneration.Unit.Tests\System.Text.Json.SourceGeneration.Roslyn4.0.Unit.Tests.csproj" />

Expand Down

0 comments on commit 3eecfcd

Please sign in to comment.